Joedb 10.4.3
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
ids_h.cpp
Go to the documentation of this file.
3
4namespace joedb::generator
5{
6 ////////////////////////////////////////////////////////////////////////////
7 void ids_h::write_hashing(std::ostream &out)
8 ////////////////////////////////////////////////////////////////////////////
9 {
10 const Database_Schema &db = options.get_db();
11 const auto &tables = db.get_tables();
12
13 for (const auto &[tid, tname]: tables)
14 {
15 if (!(parent_options && parent_options->has_table(tname)))
16 {
17 out << " template<>\n";
18 out << " struct hash<";
20 out << "::id_of_" << tname << ">\n";
21 out << " {\n";
22 out << " size_t operator()(";
24 out << "::id_of_" << tname << " x) const noexcept\n";
25 out << " {\n";
26 out << " return hash<joedb::index_t>{}(x.get_id());\n";
27 out << " }\n";
28 out << " };\n";
29 }
30 }
31 }
32
33 ////////////////////////////////////////////////////////////////////////////
35 ////////////////////////////////////////////////////////////////////////////
36 (
37 const Compiler_Options &options,
38 const Compiler_Options *parent_options
39 ):
40 Generator(".", "ids.h", options),
41 parent_options(parent_options)
42 {
43 }
44
45 ////////////////////////////////////////////////////////////////////////////
46 void ids_h::write(std::ostream &out)
47 ////////////////////////////////////////////////////////////////////////////
48 {
49 const Database_Schema &db = options.get_db();
50 const auto &tables = db.get_tables();
51
53
54 if (parent_options)
55 out << R"RRR(
56#include "../../ids.h"
57
58)RRR";
59 else
60 out << R"RRR(
61#include "joedb/index_types.h"
62
63)RRR";
64
66
67 out << R"RRR(
68 using joedb::Record_Id;
69 using joedb::Table_Id;
70 using joedb::Field_Id;
71)RRR";
72
73 for (const auto &[tid, tname]: tables)
74 {
75 out << '\n';
76 if (parent_options && parent_options->has_table(tname))
77 {
78 out << " using id_of_" << tname << " = ";
79 namespace_write(out, parent_options->name_space);
80 out << "::id_of_" << tname << ";\n";
81 }
82 else
83 {
84 out << " /// Strongly-typed wrapper around an integer representing a row of the " << tname << " table\n";
85 out << " class id_of_" << tname << "\n {\n";
86 out << " private:\n";
87 out << " Record_Id id;\n";
88 out << "\n public:\n";
89 out << " constexpr explicit id_of_" << tname << "(joedb::index_t id): id(Record_Id(id)) {}\n";
90 out << " constexpr explicit id_of_" << tname << "(Record_Id id): id(id) {}\n";
91 out << " constexpr id_of_" << tname << "(): id(joedb::Record_Id::null) {}\n";
92 out << " constexpr bool is_null() const {return id.is_null();}\n";
93 out << " constexpr bool is_not_null() const {return id.is_not_null();}\n";
94 out << " constexpr auto get_id() const {return to_underlying(id);}\n";
95 out << " constexpr Record_Id get_record_id() const {return id;}\n";
96 out << " constexpr bool operator==(id_of_" << tname << " x) const {return id == x.id;}\n";
97 out << " constexpr bool operator!=(id_of_" << tname << " x) const {return id != x.id;}\n";
98 out << " constexpr bool operator<(id_of_" << tname << " x) const {return id < x.id;}\n";
99 out << " constexpr bool operator>(id_of_" << tname << " x) const {return id > x.id;}\n";
100 out << " constexpr bool operator<=(id_of_" << tname << " x) const {return id <= x.id;}\n";
101 out << " constexpr bool operator>=(id_of_" << tname << " x) const {return id >= x.id;}\n";
102 out << " constexpr id_of_" << tname << " operator[](size_t i) const {return id_of_" << tname << "(id + i);}\n";
103 out << " };\n";
104 }
105 }
106
108
109 out << "\n#include <functional>\n\n";
110 out << "namespace std\n{\n";
111 write_hashing(out);
112 out << "}\n\n";
113
115 }
116}
std::vector< std::string > name_space
const std::vector< std::string > & get_name_space() const
const Database & get_db() const
bool has_table(const std::string &table_name) const
const std::map< Table_Id, std::string > & get_tables() const override
const Compiler_Options & options
Definition Generator.h:16
ids_h(const Compiler_Options &options, const Compiler_Options *parent_options)
Definition ids_h.cpp:36
void write(std::ostream &out) override
Definition ids_h.cpp:46
void namespace_open(std::ostream &out, const std::vector< std::string > &n)
void namespace_close(std::ostream &out, const std::vector< std::string > &n)
void namespace_write(std::ostream &out, const std::vector< std::string > &n, const char *delimiter)
void namespace_include_guard_open(std::ostream &out, const char *name, const std::vector< std::string > &n)
void namespace_include_guard_close(std::ostream &out)
One code generator for each of the file generated by joedbc.
Definition Client_h.cpp:5