Joedb 10.0.1
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 {
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 ////////////////////////////////////////////////////////////////////////////
71 void Generator::write_tuple_type(const Compiler_Options::Index &index, bool reference)
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 {
96 if (!reference)
97 {
99 (
100 options.db.get_field_type(index.table_id, index.field_ids[0]),
101 false,
102 false
103 );
104 }
105 }
106 }
107
108 ////////////////////////////////////////////////////////////////////////////
110 ////////////////////////////////////////////////////////////////////////////
111 {
112 out << "std::";
113 if (index.unique)
114 out << "map";
115 else
116 out << "multimap";
117 out << '<';
118
119 write_tuple_type(index, false);
120
121 out << ", id_of_" << options.db.get_table_name(index.table_id) << ">";
122 }
123
124 ////////////////////////////////////////////////////////////////////////////
125 // type arrays
126 ////////////////////////////////////////////////////////////////////////////
127 #define STRINGIFY(X) #X
128 #define EXPAND_AND_STRINGIFY(X) STRINGIFY(X)
129
130 ////////////////////////////////////////////////////////////////////////////
132 ////////////////////////////////////////////////////////////////////////////
133 {
134 static constexpr char const * const types[] =
135 {
136 nullptr,
137 #define TYPE_MACRO(a, b, type_id, d, e) EXPAND_AND_STRINGIFY(type_id),
138 #include "joedb/TYPE_MACRO.h"
139 };
140
141 return types[int(type.get_type_id())];
142 }
143
144 ////////////////////////////////////////////////////////////////////////////
146 ////////////////////////////////////////////////////////////////////////////
147 {
148 static char const * const cpp_types[] =
149 {
150 nullptr,
151 #define TYPE_MACRO(a, type, c, d, e) EXPAND_AND_STRINGIFY(type),
152 #include "joedb/TYPE_MACRO.h"
153 };
154
155 return cpp_types[int(type.get_type_id())];
156 }
157
158 ////////////////////////////////////////////////////////////////////////////
160 ////////////////////////////////////////////////////////////////////////////
161 {
162 static char const * const storage_types[] =
163 {
164 nullptr,
165 #define TYPE_MACRO(storage, b, c, d, e) EXPAND_AND_STRINGIFY(storage),
166 #include "joedb/TYPE_MACRO.h"
167 };
168
169 return storage_types[int(type.get_type_id())];
170 }
171
172 #undef EXPAND_AND_STRINGIFY
173 #undef STRINGIFY
174
175 ////////////////////////////////////////////////////////////////////////////
177 ////////////////////////////////////////////////////////////////////////////
178 (
179 const char *dir_name,
180 const char *file_name,
181 const Compiler_Options &options
182 ):
183 options(options)
184 {
185 std::filesystem::create_directory(options.get_name_space_back());
186 std::string dir_string = options.get_name_space_back() + "/" + std::string(dir_name);
187 std::filesystem::create_directory(dir_string);
188 std::string file_string = dir_string + "/" + std::string(file_name);
189
190 out.exceptions(std::ios::badbit | std::ios::failbit);
191 out.open(file_string, std::ios::trunc);
193 }
194
195 ////////////////////////////////////////////////////////////////////////////
196 Generator::~Generator() = default;
197 ////////////////////////////////////////////////////////////////////////////
198}
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
const Type & get_field_type(Table_Id table_id, Field_Id field_id) const override
Record_Id get_used_count() const
const std::string & get_table_name(Table_Id table_id) const
Definition Readable.cpp:38
Type_Id get_type_id() const
Definition Type.h:41
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_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
void write_tuple_type(const Compiler_Options::Index &index, bool reference)
Definition Generator.cpp:71
Generator(const char *dir_name, const char *file_name, const Compiler_Options &options)
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
std::vector< Field_Id > field_ids