Skip to content

Commit 1770090

Browse files
authored
Fix violation of rule of 5 in JsonObject (#4)
* Add declarations for JsonObject's special functions * Define copy/move constructors * Define copy/move assignment operators * Implicitly create bytes * Implicitly create bytes in exchange() * Fix includes lol (utility, cstring) * Destroy() objects
1 parent 38b32ab commit 1770090

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

cppjson/include/cppjson/object.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ namespace cppjson
2525
{
2626
public:
2727
explicit JsonObject();
28+
JsonObject(const JsonObject& other);
29+
JsonObject(JsonObject&& other);
30+
JsonObject& operator=(const JsonObject& other);
31+
JsonObject& operator=(JsonObject&& other);
2832
~JsonObject();
2933

3034
template <typename T>

cppjson/src/object.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,45 @@
11
#include "cppjson/object.hpp"
22
#include <new>
33
#include <stdexcept>
4+
#include <cstring>
5+
#include <utility>
46

57
constexpr std::size_t DataStorageSize = std::max({sizeof(std::string), sizeof(cppjson::Object), sizeof(double), sizeof(bool)});
68

79
cppjson::JsonObject::JsonObject() : _dataStorage(static_cast<std::byte*>(::operator new(DataStorageSize))) {}
810

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+
}
943
cppjson::JsonObject::~JsonObject()
1044
{
1145
this->Destroy();
@@ -15,13 +49,16 @@ cppjson::JsonObject::~JsonObject()
1549
void cppjson::JsonObject::Destroy(void)
1650
{
1751
using std::string;
52+
using cppjson::Object;
1853

1954
switch (std::exchange(this->_dataType, JsonType::Null))
2055
{
2156
case JsonType::Null:
2257
case JsonType::Number:
2358
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
2562
}
2663
}
2764

0 commit comments

Comments
 (0)