Joedb 10.3.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Generator.cpp
Go to the documentation of this file.
3#include "joedb/get_version.h"
5
6#include <iostream>
7
8namespace joedb::generator
9{
10 ////////////////////////////////////////////////////////////////////////////
12 ////////////////////////////////////////////////////////////////////////////
13 {
14 for (const auto &[tid, tname]: options.db.get_tables())
15 {
17 return true;
18 }
19 return false;
20 }
21
22 ////////////////////////////////////////////////////////////////////////////
23 void Generator::write_initial_comment(std::ostream &out)
24 ////////////////////////////////////////////////////////////////////////////
25 {
26 out << "/////////////////////////////////////////////////////////////////////////////\n";
27 out << "//\n";
28 out << "// This code was automatically generated by the joedb compiler\n";
29 out << "// https://www.joedb.org/\n";
30 out << "//\n";
31 out << "// Path to compiler: " << options.exe_path << '\n';
32 out << "// Version: " << joedb::get_version() << '\n';
33 out << "// joedbc compilation time: " << __DATE__ " " __TIME__ "\n";
34 out << "// Generation of this file: " << get_time_string_of_now() << '\n';
35 out << "//\n";
36 out << "/////////////////////////////////////////////////////////////////////////////\n";
37 }
38
39 ////////////////////////////////////////////////////////////////////////////
41 ////////////////////////////////////////////////////////////////////////////
42 (
43 std::ostream &out,
44 Type type,
45 bool return_type,
46 bool setter_type
47 )
48 {
49 switch (type.get_type_id())
50 {
52 out << "void";
53 break;
54
55 case Type::Type_Id::reference:
56 out << "id_of_" << options.db.get_table_name(type.get_table_id());
57 break;
58
59 #define TYPE_MACRO(storage_tt, return_tt, type_id, read, write)\
60 case Type::Type_Id::type_id:\
61 if (return_type || setter_type)\
62 out << #return_tt;\
63 else\
64 out << #storage_tt;\
65 break;
66 #define TYPE_MACRO_NO_REFERENCE
67 #include "joedb/TYPE_MACRO.h"
68 }
69 }
70
71 ////////////////////////////////////////////////////////////////////////////
73 ////////////////////////////////////////////////////////////////////////////
74 (
75 std::ostream &out,
76 const Compiler_Options::Index &index,
77 bool reference
78 )
79 {
80 if (index.field_ids.size() > 1)
81 {
82 out << "std::tuple<";
83
84 for (size_t i = 0; i < index.field_ids.size(); i++)
85 {
86 if (i > 0)
87 out << ", ";
88
89 const Type &type = options.db.get_field_type
90 (
91 index.table_id,
92 index.field_ids[i]
93 );
94
95 write_type(out, type, false, false);
96 }
97
98 out << ">";
99 }
100 else
101 {
102 if (!reference)
103 {
104 write_type
105 (
106 out,
107 options.db.get_field_type(index.table_id, index.field_ids[0]),
108 false,
109 false
110 );
111 }
112 }
113 }
114
115 ////////////////////////////////////////////////////////////////////////////
117 ////////////////////////////////////////////////////////////////////////////
118 (
119 std::ostream &out,
120 const Compiler_Options::Index &index
121 )
122 {
123 out << "std::";
124 if (index.unique)
125 out << "map";
126 else
127 out << "multimap";
128 out << '<';
129
130 write_tuple_type(out, index, false);
131
132 out << ", id_of_" << options.db.get_table_name(index.table_id) << ">";
133 }
134
135 ////////////////////////////////////////////////////////////////////////////
136 // type arrays
137 ////////////////////////////////////////////////////////////////////////////
138 #define STRINGIFY(X) #X
139 #define EXPAND_AND_STRINGIFY(X) STRINGIFY(X)
140
141 ////////////////////////////////////////////////////////////////////////////
143 ////////////////////////////////////////////////////////////////////////////
144 {
145 static constexpr char const * const types[] =
146 {
147 nullptr,
148 #define TYPE_MACRO(a, b, type_id, d, e) EXPAND_AND_STRINGIFY(type_id),
149 #include "joedb/TYPE_MACRO.h"
150 };
151
152 return types[int(type.get_type_id())];
153 }
154
155 ////////////////////////////////////////////////////////////////////////////
157 ////////////////////////////////////////////////////////////////////////////
158 {
159 static char const * const cpp_types[] =
160 {
161 nullptr,
162 #define TYPE_MACRO(a, type, c, d, e) EXPAND_AND_STRINGIFY(type),
163 #include "joedb/TYPE_MACRO.h"
164 };
165
166 return cpp_types[int(type.get_type_id())];
167 }
168
169 ////////////////////////////////////////////////////////////////////////////
171 ////////////////////////////////////////////////////////////////////////////
172 {
173 static char const * const storage_types[] =
174 {
175 nullptr,
176 #define TYPE_MACRO(storage, b, c, d, e) EXPAND_AND_STRINGIFY(storage),
177 #include "joedb/TYPE_MACRO.h"
178 };
179
180 return storage_types[int(type.get_type_id())];
181 }
182
183 #undef EXPAND_AND_STRINGIFY
184 #undef STRINGIFY
185
186 ////////////////////////////////////////////////////////////////////////////
188 ////////////////////////////////////////////////////////////////////////////
189 (
190 std::string dir_name,
191 std::string file_name,
192 const Compiler_Options &options
193 ):
194 dir_name(std::move(dir_name)),
195 file_name(std::move(file_name)),
196 options(options)
197 {
198 }
199
200 ////////////////////////////////////////////////////////////////////////////
202 ////////////////////////////////////////////////////////////////////////////
203 {
204 const std::string dir_string =
205 options.output_path + "/" +
207 std::string(dir_name);
208
209 write_source_code(dir_string, file_name, [&](std::ostream &out)
210 {
212 write(out);
213 });
214 }
215
216 ////////////////////////////////////////////////////////////////////////////
217 Generator::~Generator() = default;
218 ////////////////////////////////////////////////////////////////////////////
219}
const std::string & get_name_space_back() const
const Freedom_Keeper & get_freedom(Table_Id table_id) const override
const std::map< Table_Id, std::string > & get_tables() const override
Record_Id get_used_count() const
Type_Id get_type_id() const
Definition Type.h:41
static const char * get_storage_type_string(Type type)
static const char * get_type_string(Type type)
void write_index_type(std::ostream &out, const Compiler_Options::Index &index)
static const char * get_cpp_type_string(Type type)
const std::string file_name
Definition Generator.h:15
const Compiler_Options & options
Definition Generator.h:16
const std::string dir_name
Definition Generator.h:14
virtual void write(std::ostream &out)=0
void write_tuple_type(std::ostream &out, const Compiler_Options::Index &index, bool reference)
Definition Generator.cpp:74
void write_initial_comment(std::ostream &out)
Definition Generator.cpp:23
Generator(std::string dir_name, std::string file_name, const Compiler_Options &options)
void write_type(std::ostream &out, Type type, bool return_type, bool setter_type)
Definition Generator.cpp:42
constexpr const char * get_version()
Definition get_version.h:7
std::string get_time_string_of_now()
One code generator for each of the file generated by joedbc.
Definition Client_h.cpp:5
void write_source_code(const std::string &dir_name, const std::string &file_name, const std::function< void(std::ostream &)> &write)