Joedb 10.4.6
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 {
16 if (options.db.get_freedom(tid).get_used_count() > Record_Id{0})
17 return true;
18 }
19 return false;
20 }
21
22 ////////////////////////////////////////////////////////////////////////////
24 ////////////////////////////////////////////////////////////////////////////
25 {
26 for (const auto &[fid, fname]: options.db.get_fields(table_id))
27 if (options.db.get_field_type(table_id, fid).get_type_id() == type_id)
28 return true;
29 return false;
30 }
31
32 ////////////////////////////////////////////////////////////////////////////
33 void Generator::write_initial_comment(std::ostream &out)
34 ////////////////////////////////////////////////////////////////////////////
35 {
36 out << "/////////////////////////////////////////////////////////////////////////////\n";
37 out << "//\n";
38 out << "// This code was automatically generated by the joedb compiler\n";
39 out << "// https://www.joedb.org/\n";
40 out << "//\n";
41 out << "// Path to compiler: " << options.exe_path << '\n';
42 out << "// Version: " << joedb::get_version() << '\n';
43 out << "// joedbc compilation time: " << __DATE__ " " __TIME__ "\n";
44 out << "// Generation of this file: " << get_time_string_of_now() << '\n';
45 out << "//\n";
46 out << "/////////////////////////////////////////////////////////////////////////////\n";
47 }
48
49 ////////////////////////////////////////////////////////////////////////////
51 ////////////////////////////////////////////////////////////////////////////
52 (
53 std::ostream &out,
54 Type type,
55 bool return_type,
56 bool setter_type
57 )
58 {
59 switch (type.get_type_id())
60 {
62 out << "void";
63 break;
64
66 out << "id_of_" << options.db.get_table_name(type.get_table_id());
67 break;
68
70 if (return_type)
71 out << "const std::string &";
72 else if (setter_type)
73 out << "std::string_view";
74 else
75 out << "std::string";
76 break;
77
78 #define TYPE_MACRO(storage_tt, return_tt, type_id, read, write)\
79 case Type::Type_Id::type_id:\
80 if (return_type || setter_type)\
81 out << #return_tt;\
82 else\
83 out << #storage_tt;\
84 break;
85 #define TYPE_MACRO_NO_REFERENCE
86 #define TYPE_MACRO_NO_STRING
87 #include "joedb/TYPE_MACRO.h"
88 }
89 }
90
91 ////////////////////////////////////////////////////////////////////////////
93 ////////////////////////////////////////////////////////////////////////////
94 (
95 std::ostream &out,
96 const Compiler_Options::Index &index,
97 bool reference
98 )
99 {
100 if (index.field_ids.size() > 1)
101 {
102 out << "std::tuple<";
103
104 for (size_t i = 0; i < index.field_ids.size(); i++)
105 {
106 if (i > 0)
107 out << ", ";
108
109 const Type &type = options.db.get_field_type
110 (
111 index.table_id,
112 index.field_ids[i]
113 );
114
115 write_type(out, type, false, false);
116 }
117
118 out << ">";
119 }
120 else
121 {
122 if (!reference)
123 {
125 (
126 out,
127 options.db.get_field_type(index.table_id, index.field_ids[0]),
128 false,
129 false
130 );
131 }
132 }
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,
193 ):
194 dir_name(std::move(dir_name)),
195 file_name(std::move(file_name)),
197 {
198 }
199
200 ////////////////////////////////////////////////////////////////////////////
202 ////////////////////////////////////////////////////////////////////////////
203 {
204 const std::string dir_string =
205 options.output_path + "/" +
206 options.get_name_space_back() + "/" +
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}
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)
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:94
void write_initial_comment(std::ostream &out)
Definition Generator.cpp:33
bool table_has_field_type(Table_Id table_id, Type::Type_Id type_id) const
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:52
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)
Definition ids.h:50