-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrow.hpp
145 lines (118 loc) · 2.86 KB
/
row.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#pragma once
#include <type_traits>
#include <utility>
#include <vector>
#include "cexpr/string.hpp"
namespace sql
{
struct void_row
{
static constexpr std::size_t depth{ 0 };
};
template <typename Col, typename Next>
class row
{
public:
using column = Col;
using next = Next;
static constexpr std::size_t depth{ 1 + next::depth };
row() = default;
template <typename... ColTs>
row(column::type const& val, ColTs const&... vals) : value_{ val }, next_{ vals... }
{}
template <typename... ColTs>
row(column::type&& val, ColTs&&... vals) : value_{ std::forward<column::type>(val) }, next_{ std::forward<ColTs>(vals)... }
{}
inline constexpr next const& tail() const noexcept
{
return next_;
}
inline constexpr next& tail() noexcept
{
return next_;
}
inline constexpr column::type const& head() const noexcept
{
return value_;
}
inline constexpr column::type& head() noexcept
{
return value_;
}
private:
column::type value_;
next next_;
};
template <typename Col, typename... Cols>
struct variadic_row
{
private:
static inline constexpr auto resolve() noexcept
{
if constexpr (sizeof...(Cols) != 0)
{
return typename variadic_row<Cols...>::row_type{};
}
else
{
return void_row{};
}
}
public:
using row_type = row<Col, decltype(resolve())>;
};
// user function to query row elements by column name
template <cexpr::string Name, typename Row>
constexpr auto const& get(Row const& r) noexcept
{
static_assert(!std::is_same_v<Row, sql::void_row>, "Name does not match a column name.");
if constexpr (Row::column::name == Name)
{
return r.head();
}
else
{
return get<Name>(r.tail());
}
}
// compiler function used by structured binding declaration
template <std::size_t Pos, typename Row>
constexpr auto const& get(Row const& r) noexcept
{
static_assert(Pos < Row::depth, "Position is larger than number of row columns.");
if constexpr (Pos == 0)
{
return r.head();
}
else
{
return get<Pos - 1>(r.tail());
}
}
// function to assign a value to a column's value in a row
template <cexpr::string Name, typename Row, typename Type>
constexpr void set(Row& r, Type const& value)
{
static_assert(!std::is_same_v<Row, sql::void_row>, "Name does not match a column name.");
if constexpr (Row::column::name == Name)
{
r.head() = value;
}
else
{
set<Name>(r.tail(), value);
}
}
} // namespace sql
// STL injections to allow row to be used in structured binding declarations
namespace std
{
template <typename Col, typename Next>
class tuple_size<sql::row<Col, Next>> : public integral_constant<size_t, sql::row<Col, Next>::depth>
{};
template <size_t Index, typename Col, typename Next>
struct tuple_element<Index, sql::row<Col, Next>>
{
using type = decltype(sql::get<Index>(sql::row<Col, Next>{}));
};
} // namespace std