-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrelation.hpp
67 lines (55 loc) · 1.28 KB
/
relation.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
#pragma once
#include <exception>
#include <type_traits>
namespace ra
{
struct data_end : std::exception
{};
// Id template parameter allows unique ra::relation types to be instantiated from
// a single sql::schema type (for queries referencing a schema multiple times).
template <typename Schema, std::size_t Id>
class relation
{
public:
using output_type = Schema::row_type&;
static auto& next()
{
if (curr != end)
{
return *curr++;
}
else
{
throw ra::data_end{};
}
}
template <typename Input, typename... Inputs>
static void seed(Input const& r, Inputs const&... rs) noexcept
{
if constexpr (std::is_same_v<Input, Schema>)
{
curr = r.begin();
begin = r.begin();
end = r.end();
}
else
{
seed(rs...);
}
}
static inline void reset() noexcept
{
curr = begin;
}
private:
static Schema::const_iterator curr;
static Schema::const_iterator begin;
static Schema::const_iterator end;
};
template <typename Schema, std::size_t Id>
Schema::const_iterator relation<Schema, Id>::curr{};
template <typename Schema, std::size_t Id>
Schema::const_iterator relation<Schema, Id>::begin{};
template <typename Schema, std::size_t Id>
Schema::const_iterator relation<Schema, Id>::end{};
} // namespace ra