-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmodel.hpp
58 lines (46 loc) · 1.45 KB
/
model.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
#ifndef TEST_MODEL_INCLUDED
#define TEST_MODEL_INCLUDED
#include <variant>
namespace test
{
struct context : std::unordered_map<std::string, bustache::format>
{
using unordered_map::unordered_map;
bustache::format const* operator()(std::string const& key) const
{
auto it = find(key);
return it == end() ? nullptr : &it->second;
}
};
struct value;
struct object : std::vector<std::pair<std::string, value>>
{
using vector::vector;
using mapped_type = value;
const_iterator find(std::string const& key) const;
};
using array = std::vector<value>;
using lazy_value = std::function<value(bustache::ast::view const*)>;
using lazy_format = std::function<bustache::format(bustache::ast::view const*)>;
struct value : std::variant<bool, int, double, std::string, object, array, lazy_value, lazy_format>
{
using variant::variant;
value(char const* str) : variant(std::string(str)) {}
};
object::const_iterator object::find(std::string const& key) const
{
return std::find_if(begin(), end(), [&key](value_type const& pair)
{
return pair.first == key;
});
}
}
template<>
struct bustache::impl_compatible<test::value>
{
static value_ptr get_value_ptr(test::value::variant const& self)
{
return std::visit([](auto const& val) { return value_ptr(&val); }, self);
}
};
#endif