File tree Expand file tree Collapse file tree 3 files changed +22
-25
lines changed Expand file tree Collapse file tree 3 files changed +22
-25
lines changed Original file line number Diff line number Diff line change @@ -64,11 +64,8 @@ inline bool has_key(const object &o, const std::string &key);
6464template <class In >
6565inline value parse (In first, In last);
6666inline value parse (std::istream &is);
67- inline value parse (std::wistream &is);
6867inline value parse (std::istream &&is);
69- inline value parse (std::wistream &&is);
7068inline value parse (const std::string &s);
71- inline value parse (const std::wstring &s);
7269
7370// convert a value to a JSON string
7471enum {
Original file line number Diff line number Diff line change @@ -129,26 +129,14 @@ inline value parse(std::istream &&is) {
129129 return parse (is);
130130}
131131
132- inline value parse (std::wistream &&is) {
133- return parse (is);
134- }
135-
136132inline value parse (std::istream &is) {
137133 return parse ((std::istreambuf_iterator<char >(is)), std::istreambuf_iterator<char >());
138134}
139135
140- inline value parse (std::wistream &is) {
141- return parse ((std::istreambuf_iterator<wchar_t >(is)), std::istreambuf_iterator<wchar_t >());
142- }
143-
144136inline value parse (const std::string &s) {
145137 return parse (s.begin (), s.end ());
146138}
147139
148- inline value parse (const std::wstring &s) {
149- return parse (s.begin (), s.end ());
150- }
151-
152140inline bool is_string (const value &v) { return (v.type_ == value::type_string); }
153141inline bool is_bool (const value &v) { return (v.type_ == value::type_boolean); }
154142inline bool is_number (const value &v) { return (v.type_ == value::type_number); }
Original file line number Diff line number Diff line change @@ -7,6 +7,24 @@ namespace json {
77class array ;
88class object ;
99
10+ namespace detail {
11+ template <class T >
12+ constexpr T static_max (T n) {
13+ return n;
14+ }
15+
16+ template <class T , class ... Args>
17+ constexpr T static_max (T n, Args ... args) {
18+ return n > static_max (args...) ? n : static_max (args...);
19+ }
20+
21+ template <class ... Types>
22+ struct aligned_traits {
23+ static constexpr std::size_t alignment_value = static_max(alignof (Types)...);
24+ static constexpr std::size_t size_value = static_max(sizeof (Types)...);
25+ };
26+ }
27+
1028class value {
1129 friend bool is_string (const value &v);
1230 friend bool is_bool (const value &v);
@@ -98,18 +116,12 @@ class value {
98116 // I would love to use std::aligned_union, but it doesn't seem widely supported
99117 // so instead, we kinda make our own, first we need a type which has the correct
100118 // size and alignment requirements based on the types we want to store
101- union storage_union {
102- invalid_t invalid;
103- object_pointer object;
104- array_pointer array;
105- std::string string;
106- };
107-
108- // then we create a type which is generic, and has that same alignement and size
119+ using Tr = detail::aligned_traits<invalid_t , object_pointer, array_pointer, std::string>;
120+
109121 typedef struct {
110- alignas (alignof (storage_union)) uint8_t data[sizeof (storage_union) ];
122+ alignas (Tr::alignment_value) uint8_t data[Tr::size_value ];
111123 } storage_type;
112-
124+
113125 storage_type value_;
114126 type type_;
115127};
You can’t perform that action at this time.
0 commit comments