Skip to content

Commit f2b08bc

Browse files
committed
i think move semantics are starting to look right
1 parent 46d4cd2 commit f2b08bc

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

lib/include/cpp-json/value.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ class value {
112112
const array &as_array() const;
113113
array &as_array();
114114

115+
private:
116+
void destroy();
117+
115118
private:
116119
struct invalid_t {};
117120

lib/include/cpp-json/value.tcc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ namespace json {
88
// Name: ~value
99
//------------------------------------------------------------------------------
1010
inline value::~value() {
11+
destroy();
12+
}
13+
14+
//------------------------------------------------------------------------------
15+
// Name: ~value
16+
//------------------------------------------------------------------------------
17+
inline void value::destroy() {
1118
using std::string;
1219

1320
switch(type_) {
@@ -26,6 +33,8 @@ inline value::~value() {
2633
case value::type_invalid:
2734
break;
2835
}
36+
37+
type_ = type_invalid;
2938
}
3039

3140
//------------------------------------------------------------------------------
@@ -90,7 +99,7 @@ inline value::value(bool b) : type_(type_boolean) {
9099
//------------------------------------------------------------------------------
91100
inline value::value(value &&other) : type_(other.type_) {
92101

93-
other.type_ = type::type_invalid;
102+
using std::string;
94103

95104
// move from the other object
96105
switch(type_) {
@@ -109,6 +118,8 @@ inline value::value(value &&other) : type_(other.type_) {
109118
case value::type_invalid:
110119
break;
111120
}
121+
122+
other.destroy();
112123
}
113124

114125
//------------------------------------------------------------------------------

test/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ add_executable(example2 example2.cpp)
55
add_executable(example3 example3.cpp)
66
add_executable(example4 example4.cpp)
77

8-
target_link_libraries(example1 LINK_PUBLIC cpp-json)
9-
target_link_libraries(example2 LINK_PUBLIC cpp-json)
10-
target_link_libraries(example3 LINK_PUBLIC cpp-json)
11-
target_link_libraries(example4 LINK_PUBLIC cpp-json)
8+
target_link_libraries(example1 PUBLIC cpp-json)
9+
target_link_libraries(example2 PUBLIC cpp-json)
10+
target_link_libraries(example3 PUBLIC cpp-json)
11+
target_link_libraries(example4 PUBLIC cpp-json)
1212

1313
set_property(TARGET example1 PROPERTY CXX_STANDARD 11)
1414
set_property(TARGET example2 PROPERTY CXX_STANDARD 11)

0 commit comments

Comments
 (0)