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 |
|
---|
16 | namespace hessian {
|
---|
17 |
|
---|
18 | struct hash;
|
---|
19 |
|
---|
20 | using null_t = boost::blank;
|
---|
21 | using bool_t = bool;
|
---|
22 | using int_t = std::int32_t;
|
---|
23 | using long_t = std::int64_t;
|
---|
24 | using double_t = double;
|
---|
25 | using date_t = boost::posix_time::ptime;
|
---|
26 | using string_t = std::string;
|
---|
27 | using binary_t = std::basic_string<std::uint8_t>;
|
---|
28 |
|
---|
29 | template <typename T>
|
---|
30 | using basic_list_t = std::vector<T>;
|
---|
31 |
|
---|
32 | template <typename T>
|
---|
33 | using basic_map_t = std::unordered_map<T, T, hash>;
|
---|
34 |
|
---|
35 | template <typename T>
|
---|
36 | using basic_object_t = std::unordered_map<string_t, T>;
|
---|
37 |
|
---|
38 | using 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 |
|
---|
53 | using list_t = basic_list_t<value_t>;
|
---|
54 | using map_t = basic_map_t<value_t>;
|
---|
55 | using object_t = basic_object_t<value_t>;
|
---|
56 |
|
---|
57 | struct 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 |
|
---|
66 | namespace boost {
|
---|
67 |
|
---|
68 | size_t
|
---|
69 | hash_value(const hessian::null_t& value) noexcept;
|
---|
70 |
|
---|
71 | size_t
|
---|
72 | hash_value(const hessian::date_t& value) noexcept;
|
---|
73 |
|
---|
74 | size_t
|
---|
75 | hash_value(const hessian::map_t& value) noexcept;
|
---|
76 |
|
---|
77 | size_t
|
---|
78 | hash_value(const hessian::object_t& value) noexcept;
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | #include <boost/functional/hash.hpp>
|
---|
83 |
|
---|
84 | namespace boost {
|
---|
85 |
|
---|
86 | inline size_t
|
---|
87 | hash_value(const hessian::null_t& value) noexcept
|
---|
88 | {
|
---|
89 | return 0xAAAAAAAAAAAAAAAA;
|
---|
90 | }
|
---|
91 |
|
---|
92 | inline size_t
|
---|
93 | hash_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 |
|
---|
101 | inline size_t
|
---|
102 | hash_value(const hessian::map_t& value) noexcept
|
---|
103 | {
|
---|
104 | return hash_range(value.begin(), value.end());
|
---|
105 | }
|
---|
106 |
|
---|
107 | inline size_t
|
---|
108 | hash_value(const hessian::object_t& value) noexcept
|
---|
109 | {
|
---|
110 | return hash_range(value.begin(), value.end());
|
---|
111 | }
|
---|
112 |
|
---|
113 | }
|
---|
114 |
|
---|
115 | namespace hessian {
|
---|
116 |
|
---|
117 | inline size_t
|
---|
118 | hash::operator()(const value_t& value) const noexcept
|
---|
119 | {
|
---|
120 | return boost::hash_value(value);
|
---|
121 | }
|
---|
122 |
|
---|
123 | }
|
---|
124 |
|
---|
125 | int 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 | }
|
---|