Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Generator.cpp
Go to the documentation of this file.
2#include "joedb/get_version.h"
4
5#include <iostream>
6#include <filesystem>
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).size() > 0)
17 return true;
18 }
19 return false;
20 }
21
22 ////////////////////////////////////////////////////////////////////////////
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 Type type,
44 bool return_type,
45 bool setter_type
46 )
47 {
48 switch (type.get_type_id())
49 {
51 out << "void";
52 break;
53
54 case Type::Type_Id::reference:
55 out << "id_of_" << options.db.get_table_name(type.get_table_id());
56 break;
57
58 #define TYPE_MACRO(storage_tt, return_tt, type_id, read, write)\
59 case Type::Type_Id::type_id:\
60 if (return_type || setter_type)\
61 out << #return_tt;\
62 else\
63 out << #storage_tt;\
64 break;
65 #define TYPE_MACRO_NO_REFERENCE
66 #include "joedb/TYPE_MACRO.h"
67 }
68 }
69
70 ////////////////////////////////////////////////////////////////////////////
72 ////////////////////////////////////////////////////////////////////////////
73 {
74 if (index.field_ids.size() > 1)
75 {
76 out << "std::tuple<";
77
78 for (size_t i = 0; i < index.field_ids.size(); i++)
79 {
80 if (i > 0)
81 out << ", ";
82
83 const Type &type = options.db.get_field_type
84 (
85 index.table_id,
86 index.field_ids[i]
87 );
88
89 write_type(type, false, false);
90 }
91
92 out << ">";
93 }
94 else
95 {
97 (
99 false,
100 false
101 );
102 }
103 }
104
105 ////////////////////////////////////////////////////////////////////////////
107 ////////////////////////////////////////////////////////////////////////////
108 {
109 out << "std::";
110 if (index.unique)
111 out << "map";
112 else
113 out << "multimap";
114 out << '<';
115
116 write_tuple_type(index);
117
118 out << ", id_of_" << options.db.get_table_name(index.table_id) << ">";
119 }
120
121 ////////////////////////////////////////////////////////////////////////////
122 // type arrays
123 ////////////////////////////////////////////////////////////////////////////
124 #define STRINGIFY(X) #X
125 #define EXPAND_AND_STRINGIFY(X) STRINGIFY(X)
126
127 ////////////////////////////////////////////////////////////////////////////
129 ////////////////////////////////////////////////////////////////////////////
130 {
131 static constexpr char const * const types[] =
132 {
133 nullptr,
134 #define TYPE_MACRO(a, b, type_id, d, e) EXPAND_AND_STRINGIFY(type_id),
135 #include "joedb/TYPE_MACRO.h"
136 };
137
138 return types[int(type.get_type_id())];
139 }
140
141 ////////////////////////////////////////////////////////////////////////////
143 ////////////////////////////////////////////////////////////////////////////
144 {
145 static char const * const cpp_types[] =
146 {
147 nullptr,
148 #define TYPE_MACRO(a, type, c, d, e) EXPAND_AND_STRINGIFY(type),
149 #include "joedb/TYPE_MACRO.h"
150 };
151
152 return cpp_types[int(type.get_type_id())];
153 }
154
155 ////////////////////////////////////////////////////////////////////////////
157 ////////////////////////////////////////////////////////////////////////////
158 {
159 static char const * const storage_types[] =
160 {
161 nullptr,
162 #define TYPE_MACRO(storage, b, c, d, e) EXPAND_AND_STRINGIFY(storage),
163 #include "joedb/TYPE_MACRO.h"
164 };
165
166 return storage_types[int(type.get_type_id())];
167 }
168
169 #undef EXPAND_AND_STRINGIFY
170 #undef STRINGIFY
171
172 ////////////////////////////////////////////////////////////////////////////
174 ////////////////////////////////////////////////////////////////////////////
175 (
176 const char *dir_name,
177 const char *file_name,
178 const Compiler_Options &options
179 ):
180 options(options)
181 {
182 std::filesystem::create_directory(options.get_name_space().back());
183 std::string dir_string = options.get_name_space().back() + "/" + std::string(dir_name);
184 std::filesystem::create_directory(dir_string);
185 std::string file_string = dir_string + "/" + std::string(file_name);
186
187 out.exceptions(std::ios::badbit | std::ios::failbit);
188 out.open(file_string, std::ios::trunc);
190 }
191
192 ////////////////////////////////////////////////////////////////////////////
193 Generator::~Generator() = default;
194 ////////////////////////////////////////////////////////////////////////////
195}
const std::vector< std::string > & get_name_space() const
const Compact_Freedom_Keeper & get_freedom(Table_Id table_id) const override
const std::map< Table_Id, std::string > & get_tables() const override
const Type & get_field_type(Table_Id table_id, Field_Id field_id) const override
const std::string & get_table_name(Table_Id table_id) const
Definition Readable.cpp:38
Type_Id get_type_id() const
Definition Type.h:40
void write_index_type(const Compiler_Options::Index &index)
static const char * get_storage_type_string(Type type)
static const char * get_type_string(Type type)
void write_tuple_type(const Compiler_Options::Index &index)
Definition Generator.cpp:71
void write_type(Type type, bool return_type, bool setter_type)
Definition Generator.cpp:42
static const char * get_cpp_type_string(Type type)
const Compiler_Options & options
Definition Generator.h:14
Generator(const char *dir_name, const char *file_name, const Compiler_Options &options)
std::string get_time_string_of_now()
One code generator for each of the file generated by joedbc.
Definition Client_h.cpp:5
constexpr const char * get_version()
Definition get_version.h:6
std::vector< Field_Id > field_ids