Ticket #12508: test.cpp

File test.cpp, 2.5 KB (added by mike.gresens@…, 6 years ago)

sample code

Line 
1/*
2 * test.cpp
3 *
4 * Created on: 08.10.2016
5 * Author: mike
6 */
7
8#include <cstdint>
9#include <string>
10#include <vector>
11#include <unordered_map>
12#include <boost/blank.hpp>
13#include <boost/date_time/posix_time/ptime.hpp>
14#include <boost/variant.hpp>
15
16namespace hessian {
17
18struct hash;
19
20using null_t = boost::blank;
21using bool_t = bool;
22using int_t = std::int32_t;
23using long_t = std::int64_t;
24using double_t = double;
25using date_t = boost::posix_time::ptime;
26using string_t = std::string;
27using binary_t = std::basic_string<std::uint8_t>;
28
29template <typename T>
30using basic_list_t = std::vector<T>;
31
32template <typename T>
33using basic_map_t = std::unordered_map<T, T, hash>;
34
35template <typename T>
36using basic_object_t = std::unordered_map<string_t, T>;
37
38using value_t = boost::make_recursive_variant
39<
40 null_t,
41 bool_t,
42 int_t,
43 long_t,
44 double_t,
45 date_t,
46 string_t,
47 binary_t,
48 basic_list_t<boost::recursive_variant_>,
49 basic_map_t<boost::recursive_variant_>,
50 basic_object_t<boost::recursive_variant_>
51>::type;
52
53using list_t = basic_list_t<value_t>;
54using map_t = basic_map_t<value_t>;
55using object_t = basic_object_t<value_t>;
56
57struct hash
58{
59 size_t operator()(const value_t& value) const noexcept;
60};
61
62}
63
64#include <boost/variant/detail/hash_variant.hpp>
65
66namespace boost {
67
68size_t
69hash_value(const hessian::null_t& value) noexcept;
70
71size_t
72hash_value(const hessian::date_t& value) noexcept;
73
74size_t
75hash_value(const hessian::map_t& value) noexcept;
76
77size_t
78hash_value(const hessian::object_t& value) noexcept;
79
80}
81
82#include <boost/functional/hash.hpp>
83
84namespace boost {
85
86inline size_t
87hash_value(const hessian::null_t& value) noexcept
88{
89 return 0xAAAAAAAAAAAAAAAA;
90}
91
92inline size_t
93hash_value(const hessian::date_t& value) noexcept
94{
95 size_t seed = 0;
96 hash_combine(seed, value.date().day_count().as_number());
97 hash_combine(seed, value.time_of_day().total_milliseconds());
98 return seed;
99}
100
101inline size_t
102hash_value(const hessian::map_t& value) noexcept
103{
104 return hash_range(value.begin(), value.end());
105}
106
107inline size_t
108hash_value(const hessian::object_t& value) noexcept
109{
110 return hash_range(value.begin(), value.end());
111}
112
113}
114
115namespace hessian {
116
117inline size_t
118hash::operator()(const value_t& value) const noexcept
119{
120 return boost::hash_value(value);
121}
122
123}
124
125int main()
126{
127 using namespace hessian;
128 using namespace std::string_literals;
129
130 value_t value;
131
132 value = null_t();
133 value = map_t
134 {
135 {int_t(1), bool_t(true)},
136 {long_t(2), double_t(3.14)}
137 };
138 value = object_t
139 {
140 {"foo"s, bool_t(true)},
141 {"bar"s, double_t(3.14)}
142 };
143
144 return 0;
145}