Skip to content

Commit 46d4cd2

Browse files
committed
adding move semantics
1 parent e2c91e7 commit 46d4cd2

2 files changed

Lines changed: 40 additions & 9 deletions

File tree

lib/include/cpp-json/value.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class value {
7171
value(const std::nullptr_t &);
7272

7373
public:
74-
value();
74+
value() = default;
7575
~value();
7676

7777
public:
@@ -80,7 +80,9 @@ class value {
8080

8181
public:
8282
value(const value &other);
83+
value(value &&other);
8384
value &operator=(const value &rhs);
85+
value &operator=(value &&rhs);
8486

8587
public:
8688
void swap(value &other);
@@ -123,7 +125,7 @@ class value {
123125
};
124126

125127
storage_type value_;
126-
type type_;
128+
type type_ = type_invalid;
127129
};
128130

129131
bool operator==(const value &lhs, const value &rhs);

lib/include/cpp-json/value.tcc

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ inline value::~value() {
2828
}
2929
}
3030

31-
//------------------------------------------------------------------------------
32-
// Name: value
33-
//------------------------------------------------------------------------------
34-
inline value::value() : type_(type_invalid) {
35-
36-
}
37-
3831
//------------------------------------------------------------------------------
3932
// Name: value
4033
//------------------------------------------------------------------------------
@@ -92,6 +85,42 @@ inline value::value(bool b) : type_(type_boolean) {
9285
new (&value_) std::string(b ? "true" : "false");
9386
}
9487

88+
//------------------------------------------------------------------------------
89+
// Name: value
90+
//------------------------------------------------------------------------------
91+
inline value::value(value &&other) : type_(other.type_) {
92+
93+
other.type_ = type::type_invalid;
94+
95+
// move from the other object
96+
switch(type_) {
97+
case value::type_string:
98+
case value::type_number:
99+
case value::type_null:
100+
case value::type_boolean:
101+
new (&value_) std::string(std::move(*reinterpret_cast<std::string *>(&other.value_)));
102+
break;
103+
case value::type_array:
104+
new (&value_) array_pointer(std::move(*reinterpret_cast<array_pointer *>(&other.value_)));
105+
break;
106+
case value::type_object:
107+
new (&value_) object_pointer(std::move(*reinterpret_cast<object_pointer *>(&other.value_)));
108+
break;
109+
case value::type_invalid:
110+
break;
111+
}
112+
}
113+
114+
//------------------------------------------------------------------------------
115+
// Name: operator=
116+
//------------------------------------------------------------------------------
117+
inline value &value::operator=(value &&rhs) {
118+
if(this != &rhs) {
119+
value(std::move(rhs)).swap(*this);
120+
}
121+
return *this;
122+
}
123+
95124
//------------------------------------------------------------------------------
96125
// Name: value
97126
//------------------------------------------------------------------------------

0 commit comments

Comments
 (0)