1
1
#include " cppjson/object.hpp"
2
2
#include < new>
3
3
#include < stdexcept>
4
+ #include < cstring>
5
+ #include < utility>
4
6
5
7
constexpr std::size_t DataStorageSize = std::max({sizeof (std::string), sizeof (cppjson::Object), sizeof (double ), sizeof (bool )});
6
8
7
9
cppjson::JsonObject::JsonObject () : _dataStorage(static_cast <std::byte*>(::operator new (DataStorageSize))) {}
8
10
11
+ cppjson::JsonObject::JsonObject (const cppjson::JsonObject& other)
12
+ {
13
+ if (other._dataStorage == nullptr ) return ;
14
+
15
+ this ->_dataType = other._dataType ;
16
+ this ->_dataStorage = static_cast <std::byte*>(::operator new (DataStorageSize));
17
+ std::memcpy (this ->_dataStorage , other._dataStorage , DataStorageSize);
18
+ }
19
+ cppjson::JsonObject::JsonObject (JsonObject&& other)
20
+ {
21
+ this ->_dataType = std::exchange (other._dataType , cppjson::JsonType::Null);
22
+ this ->_dataStorage = std::exchange (other._dataStorage , static_cast <std::byte*>(::operator new (DataStorageSize)));
23
+ }
24
+ cppjson::JsonObject& cppjson::JsonObject::operator =(const cppjson::JsonObject& other)
25
+ {
26
+ if (&other != this )
27
+ {
28
+ this ->_dataType = other._dataType ;
29
+ this ->_dataStorage = static_cast <std::byte*>(::operator new (DataStorageSize));
30
+ std::memcpy (this ->_dataStorage , other._dataStorage , DataStorageSize);
31
+ }
32
+ return *this ;
33
+ }
34
+ cppjson::JsonObject& cppjson::JsonObject::operator =(cppjson::JsonObject&& other)
35
+ {
36
+ if (&other != this )
37
+ {
38
+ this ->_dataType = std::exchange (other._dataType , cppjson::JsonType::Null);
39
+ this ->_dataStorage = std::exchange (other._dataStorage , static_cast <std::byte*>(::operator new (DataStorageSize)));
40
+ }
41
+ return *this ;
42
+ }
9
43
cppjson::JsonObject::~JsonObject ()
10
44
{
11
45
this ->Destroy ();
@@ -15,13 +49,16 @@ cppjson::JsonObject::~JsonObject()
15
49
void cppjson::JsonObject::Destroy (void )
16
50
{
17
51
using std::string;
52
+ using cppjson::Object;
18
53
19
54
switch (std::exchange (this ->_dataType , JsonType::Null))
20
55
{
21
56
case JsonType::Null:
22
57
case JsonType::Number:
23
58
case JsonType::Bool: break ;
24
- case JsonType::String: DangerousAs<std::string>().~string ();
59
+ case JsonType::String: DangerousAs<std::string>().~string (); break ;
60
+ case JsonType::Object: DangerousAs<cppjson::Object>().~Object (); break ;
61
+ // TODO: Array
25
62
}
26
63
}
27
64
0 commit comments