Skip to content

Commit c653ca8

Browse files
committed
added a default constructor to json::value (which creates an "invalid" value, not a null one).
this allowed the code to made much simpler
1 parent 215a332 commit c653ca8

File tree

2 files changed

+25
-51
lines changed

2 files changed

+25
-51
lines changed

include/cpp-json/value.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class value {
5555
value(const std::nullptr_t &);
5656

5757
public:
58+
value();
5859
~value();
5960

6061
public:
@@ -95,9 +96,6 @@ class value {
9596
const array &as_array() const;
9697
array &as_array();
9798

98-
private:
99-
void destroy();
100-
10199
private:
102100
struct invalid_t {};
103101

include/cpp-json/value.tcc

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,45 @@ namespace json {
88
// Name: ~value
99
//------------------------------------------------------------------------------
1010
inline value::~value() {
11-
destroy();
12-
}
13-
14-
//------------------------------------------------------------------------------
15-
// Name: value
16-
//------------------------------------------------------------------------------
17-
inline value::value(const std::nullptr_t &): type_(type_null) {
18-
new (&value_) std::string("null");
19-
}
20-
21-
//------------------------------------------------------------------------------
22-
// Name: value
23-
//------------------------------------------------------------------------------
24-
inline value::value(value &&other) : type_(other.type_) {
11+
using std::string;
2512

26-
// move from the other object
2713
switch(type_) {
2814
case value::type_string:
2915
case value::type_number:
3016
case value::type_null:
3117
case value::type_boolean:
32-
new (&value_) std::string(std::move(*reinterpret_cast<std::string *>(&other.value_)));
18+
reinterpret_cast<std::string *>(&value_)->~string();
3319
break;
3420
case value::type_array:
35-
new (&value_) array_pointer(std::move(*reinterpret_cast<array_pointer *>(&other.value_)));
21+
reinterpret_cast<array_pointer *>(&value_)->~array_pointer();
3622
break;
3723
case value::type_object:
38-
new (&value_) object_pointer(std::move(*reinterpret_cast<object_pointer *>(&other.value_)));
24+
reinterpret_cast<object_pointer *>(&value_)->~object_pointer();
3925
break;
4026
case value::type_invalid:
4127
break;
4228
}
43-
44-
// destroy the other guys values
45-
other.destroy();
29+
}
30+
31+
//------------------------------------------------------------------------------
32+
// Name: value
33+
//------------------------------------------------------------------------------
34+
inline value::value() : type_(type_invalid) {
4635

47-
// now set it to the invalid type
48-
other.type_ = type_invalid;
36+
}
37+
38+
//------------------------------------------------------------------------------
39+
// Name: value
40+
//------------------------------------------------------------------------------
41+
inline value::value(const std::nullptr_t &): type_(type_null) {
42+
new (&value_) std::string("null");
43+
}
44+
45+
//------------------------------------------------------------------------------
46+
// Name: value
47+
//------------------------------------------------------------------------------
48+
inline value::value(value &&other) : value() {
49+
other.swap(*this);
4950
}
5051

5152
//------------------------------------------------------------------------------
@@ -317,31 +318,6 @@ array &value::as_array() {
317318
return **reinterpret_cast<array_pointer *>(&value_);
318319
}
319320

320-
//------------------------------------------------------------------------------
321-
// Name: destroy
322-
//------------------------------------------------------------------------------
323-
void value::destroy() {
324-
325-
using std::string;
326-
327-
switch(type_) {
328-
case value::type_string:
329-
case value::type_number:
330-
case value::type_null:
331-
case value::type_boolean:
332-
reinterpret_cast<std::string *>(&value_)->~string();
333-
break;
334-
case value::type_array:
335-
reinterpret_cast<array_pointer *>(&value_)->~array_pointer();
336-
break;
337-
case value::type_object:
338-
reinterpret_cast<object_pointer *>(&value_)->~object_pointer();
339-
break;
340-
case value::type_invalid:
341-
break;
342-
}
343-
}
344-
345321
}
346322

347323
#endif

0 commit comments

Comments
 (0)