diff --git a/cppjson/include/cppjson/object.hpp b/cppjson/include/cppjson/object.hpp index 8c2cf57..1599134 100644 --- a/cppjson/include/cppjson/object.hpp +++ b/cppjson/include/cppjson/object.hpp @@ -25,6 +25,10 @@ namespace cppjson { public: explicit JsonObject(); + JsonObject(const JsonObject& other); + JsonObject(JsonObject&& other); + JsonObject& operator=(const JsonObject& other); + JsonObject& operator=(JsonObject&& other); ~JsonObject(); template diff --git a/cppjson/src/object.cpp b/cppjson/src/object.cpp index 8641b9d..ea50613 100644 --- a/cppjson/src/object.cpp +++ b/cppjson/src/object.cpp @@ -1,11 +1,45 @@ #include "cppjson/object.hpp" #include #include +#include +#include constexpr std::size_t DataStorageSize = std::max({sizeof(std::string), sizeof(cppjson::Object), sizeof(double), sizeof(bool)}); cppjson::JsonObject::JsonObject() : _dataStorage(static_cast(::operator new(DataStorageSize))) {} +cppjson::JsonObject::JsonObject(const cppjson::JsonObject& other) +{ + if (other._dataStorage == nullptr) return; + + this->_dataType = other._dataType; + this->_dataStorage = static_cast(::operator new(DataStorageSize)); + std::memcpy(this->_dataStorage, other._dataStorage, DataStorageSize); +} +cppjson::JsonObject::JsonObject(JsonObject&& other) +{ + this->_dataType = std::exchange(other._dataType, cppjson::JsonType::Null); + this->_dataStorage = std::exchange(other._dataStorage, static_cast(::operator new(DataStorageSize))); +} +cppjson::JsonObject& cppjson::JsonObject::operator=(const cppjson::JsonObject& other) +{ + if (&other != this) + { + this->_dataType = other._dataType; + this->_dataStorage = static_cast(::operator new(DataStorageSize)); + std::memcpy(this->_dataStorage, other._dataStorage, DataStorageSize); + } + return *this; +} +cppjson::JsonObject& cppjson::JsonObject::operator=(cppjson::JsonObject&& other) +{ + if (&other != this) + { + this->_dataType = std::exchange(other._dataType, cppjson::JsonType::Null); + this->_dataStorage = std::exchange(other._dataStorage, static_cast(::operator new(DataStorageSize))); + } + return *this; +} cppjson::JsonObject::~JsonObject() { this->Destroy(); @@ -15,13 +49,16 @@ cppjson::JsonObject::~JsonObject() void cppjson::JsonObject::Destroy(void) { using std::string; + using cppjson::Object; switch (std::exchange(this->_dataType, JsonType::Null)) { case JsonType::Null: case JsonType::Number: case JsonType::Bool: break; - case JsonType::String: DangerousAs().~string(); + case JsonType::String: DangerousAs().~string(); break; + case JsonType::Object: DangerousAs().~Object(); break; + // TODO: Array } }