Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
CompileCdiMain.cxx
1#include <stdio.h>
2#include <string>
3using std::string;
4
6#include "config.hxx"
7
8#include "utils/StringPrintf.cxx"
9#include "utils/FileUtils.cxx"
10
11bool raw_render = false;
12
13// openlcb::ConfigDef def(0);
14
15RENDER_CDI(openlcb, ConfigDef, "CDI", 1);
16
17template <int N> void render_all_cdi()
18{
19 printf("// skipping config %d\n", N);
20 render_all_cdi<N - 1>();
21}
22
23template <typename CdiType>
24void render_cdi_helper(const CdiType &t, string ns, string name)
25{
26 string payload;
27 t.config_renderer().render_cdi(&payload);
28 if (raw_render)
29 {
30 // Adds trailing zero to the file written.
31 payload.push_back(0);
32 // Writes the file.
33 string filename = name + ".xmlout";
34 printf("Writing %d bytes to %s\n", (int)payload.size(),
35 filename.c_str());
36 write_string_to_file(filename, payload);
37 }
38 else
39 {
40 printf("namespace %s {\n\nextern const char %s_DATA[];\n", ns.c_str(),
41 name.c_str());
42 printf("// This is a C++11 raw string.\n");
43 printf("const char %s_DATA[] = R\"xmlpayload(%s)xmlpayload\";\n",
44 name.c_str(), payload.c_str());
45 printf("extern const size_t %s_SIZE;\n", name.c_str());
46 printf("extern const size_t %s_SIZE = sizeof(%s_DATA);\n", name.c_str(),
47 name.c_str());
48 printf("extern const size_t %s_END_OFFSET = %u;\n", name.c_str(),
49 (unsigned)t.end_offset());
50 printf("\n} // namespace %s\n\n", ns.c_str());
51 }
52}
53
54int main(int argc, char *argv[])
55{
56 if (argc > 1 && string(argv[1]) == "-r")
57 {
58 raw_render = true;
59 }
60 else
61 {
62 printf(R"(
63/* Generated code based off of config.hxx */
64
65#include <cstdint>
66#include <unistd.h>
67
68)");
69 }
70
71 // Internally calls all smaller numbered instances all the way down to 1.
72 render_all_cdi<20>();
73
74 std::vector<unsigned> event_offsets;
75 openlcb::ConfigDef def(0);
76 def.handle_events([&event_offsets](unsigned o)
77 {
78 event_offsets.push_back(o);
79 });
80 printf("namespace openlcb {\nextern const uint16_t CDI_EVENT_OFFSETS[] = {\n ");
81 for (unsigned o : event_offsets)
82 {
83 printf("%u, ", o);
84 }
85 printf("0};\n} // namespace openlcb\n");
86 return 0;
87}
#define RENDER_CDI(NS, TYPE, NAME, N)
Use this macro if additional CDI entries need to be rendered, in addition to the openlcb::ConfigDef.
void write_string_to_file(const string &filename, const string &data)
Opens (or creates) a file, truncates it and overwrites the contents with what is given in a string.