Joedb 10.4.1
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 case Type::Type_Id::string:
60 if (return_type)
61 out << "const std::string &";
62 else if (setter_type)
63 out << "std::string_view";
64 else
65 out << "std::string";
66 break;
67
68 #define TYPE_MACRO(storage_tt, return_tt, type_id, read, write)\
69 case Type::Type_Id::type_id:\
70 if (return_type || setter_type)\
71 out << #return_tt;\
72 else\
73 out << #storage_tt;\
74 break;
75 #define TYPE_MACRO_NO_REFERENCE
76 #define TYPE_MACRO_NO_STRING
77 #include "joedb/TYPE_MACRO.h"
78 }
79 }
80
81 ////////////////////////////////////////////////////////////////////////////
83 ////////////////////////////////////////////////////////////////////////////
84 (
85 std::ostream &out,
86 const Compiler_Options::Index &index,
87 bool reference
88 )
89 {
90 if (index.field_ids.size() > 1)
91 {
92 out << "std::tuple<";
93
94 for (size_t i = 0; i < index.field_ids.size(); i++)
95 {
96 if (i > 0)
97 out << ", ";
98
99 const Type &type = options.db.get_field_type
100 (
101 index.table_id,
102 index.field_ids[i]
103 );
104
105 write_type(out, type, false, false);
106 }
107
108 out << ">";
109 }
110 else
111 {
112 if (!reference)
113 {
114 write_type
115 (
116 out,
117 options.db.get_field_type(index.table_id, index.field_ids[0]),
118 false,
119 false
120 );
121 }
122 }
123 }
124
125 ////////////////////////////////////////////////////////////////////////////
127 ////////////////////////////////////////////////////////////////////////////
128 (
129 std::ostream &out,
130 const Compiler_Options::Index &index
131 )
132 {
133 out << "std::";
134 if (index.unique)
135 out << "map";
136 else
137 out << "multimap";
138 out << '<';
139
140 write_tuple_type(out, index, false);
141
142 out << ", id_of_" << options.db.get_table_name(index.table_id) << ", std::less<>>";
143 }
144
145 ////////////////////////////////////////////////////////////////////////////
146 // type arrays
147 ////////////////////////////////////////////////////////////////////////////
148 #define STRINGIFY(X) #X
149 #define EXPAND_AND_STRINGIFY(X) STRINGIFY(X)
150
151 ////////////////////////////////////////////////////////////////////////////
153 ////////////////////////////////////////////////////////////////////////////
154 {
155 static constexpr char const * const types[] =
156 {
157 nullptr,
158 #define TYPE_MACRO(a, b, type_id, d, e) EXPAND_AND_STRINGIFY(type_id),
159 #include "joedb/TYPE_MACRO.h"
160 };
161
162 return types[int(type.get_type_id())];
163 }
164
165 ////////////////////////////////////////////////////////////////////////////
167 ////////////////////////////////////////////////////////////////////////////
168 {
169 static char const * const cpp_types[] =
170 {
171 nullptr,
172 #define TYPE_MACRO(a, type, c, d, e) EXPAND_AND_STRINGIFY(type),
173 #include "joedb/TYPE_MACRO.h"
174 };
175
176 return cpp_types[int(type.get_type_id())];
177 }
178
179 ////////////////////////////////////////////////////////////////////////////
181 ////////////////////////////////////////////////////////////////////////////
182 {
183 static char const * const storage_types[] =
184 {
185 nullptr,
186 #define TYPE_MACRO(storage, b, c, d, e) EXPAND_AND_STRINGIFY(storage),
187 #include "joedb/TYPE_MACRO.h"
188 };
189
190 return storage_types[int(type.get_type_id())];
191 }
192
193 #undef EXPAND_AND_STRINGIFY
194 #undef STRINGIFY
195
196 ////////////////////////////////////////////////////////////////////////////
198 ////////////////////////////////////////////////////////////////////////////
199 (
200 std::string dir_name,
201 std::string file_name,
202 const Compiler_Options &options
203 ):
204 dir_name(std::move(dir_name)),
205 file_name(std::move(file_name)),
206 options(options)
207 {
208 }
209
210 ////////////////////////////////////////////////////////////////////////////
212 ////////////////////////////////////////////////////////////////////////////
213 {
214 const std::string dir_string =
215 options.output_path + "/" +
217 std::string(dir_name);
218
219 write_source_code(dir_string, file_name, [&](std::ostream &out)
220 {
222 write(out);
223 });
224 }
225
226 ////////////////////////////////////////////////////////////////////////////
227 Generator::~Generator() = default;
228 ////////////////////////////////////////////////////////////////////////////
229}
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:84
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)