From c70469c02861b8df66f3fc9df323e8a5c29127cb Mon Sep 17 00:00:00 2001 From: Tymianek Date: Mon, 19 May 2025 08:14:41 +0200 Subject: [PATCH 1/7] Add declarations for JsonObject's special functions --- cppjson/include/cppjson/object.hpp | 4 ++++ 1 file changed, 4 insertions(+) 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 From 862440863c56afd3daeee88bd2d2169dca5fe8e6 Mon Sep 17 00:00:00 2001 From: Tymianek Date: Mon, 19 May 2025 08:31:34 +0200 Subject: [PATCH 2/7] Define copy/move constructors --- cppjson/src/object.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cppjson/src/object.cpp b/cppjson/src/object.cpp index 8641b9d..d1c0622 100644 --- a/cppjson/src/object.cpp +++ b/cppjson/src/object.cpp @@ -6,6 +6,20 @@ constexpr std::size_t DataStorageSize = std::max({sizeof(std::string), sizeof(cp 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 = ::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, ::operator new(DataStorageSize)); +} + cppjson::JsonObject::~JsonObject() { this->Destroy(); From bf75bb332cd33465c332115547479888e1848a86 Mon Sep 17 00:00:00 2001 From: Tymianek Date: Mon, 19 May 2025 08:35:37 +0200 Subject: [PATCH 3/7] Define copy/move assignment operators --- cppjson/src/object.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/cppjson/src/object.cpp b/cppjson/src/object.cpp index d1c0622..b6b92cd 100644 --- a/cppjson/src/object.cpp +++ b/cppjson/src/object.cpp @@ -19,7 +19,25 @@ cppjson::JsonObject::JsonObject(JsonObject&& other) this->_dataType = std::exchange(other._dataType, cppjson::JsonType::Null); this->_dataStorage = std::exchange(other._dataStorage, ::operator new(DataStorageSize)); } - +cppjson::JsonObject& cppjson::JsonObject::operator=(const cppjson::JsonObject& other) +{ + if (&other != this) + { + this->_dataType = other._dataType; + this->_dataStorage = ::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, ::operator new(DataStorageSize)); + } + return *this; +} cppjson::JsonObject::~JsonObject() { this->Destroy(); From 28c94811eba3f0b7c13b5a000276dce479ae72f8 Mon Sep 17 00:00:00 2001 From: Tymianek Date: Mon, 19 May 2025 08:38:48 +0200 Subject: [PATCH 4/7] Implicitly create bytes --- cppjson/src/object.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cppjson/src/object.cpp b/cppjson/src/object.cpp index b6b92cd..538eef6 100644 --- a/cppjson/src/object.cpp +++ b/cppjson/src/object.cpp @@ -1,6 +1,7 @@ #include "cppjson/object.hpp" #include #include +#include constexpr std::size_t DataStorageSize = std::max({sizeof(std::string), sizeof(cppjson::Object), sizeof(double), sizeof(bool)}); @@ -11,7 +12,7 @@ cppjson::JsonObject::JsonObject(const cppjson::JsonObject& other) if (other._dataStorage == nullptr) return; this->_dataType = other._dataType; - this->_dataStorage = ::operator new(DataStorageSize); + this->_dataStorage = static_cast(::operator new(DataStorageSize)); std::memcpy(this->_dataStorage, other._dataStorage, DataStorageSize); } cppjson::JsonObject::JsonObject(JsonObject&& other) @@ -24,7 +25,7 @@ cppjson::JsonObject& cppjson::JsonObject::operator=(const cppjson::JsonObject& o if (&other != this) { this->_dataType = other._dataType; - this->_dataStorage = ::operator new(DataStorageSize); + this->_dataStorage = static_cast(::operator new(DataStorageSize)); std::memcpy(this->_dataStorage, other._dataStorage, DataStorageSize); } return *this; From 07f2d6d818cc0605a284fc2a6b1987466e2957f7 Mon Sep 17 00:00:00 2001 From: Tymianek Date: Mon, 19 May 2025 08:39:56 +0200 Subject: [PATCH 5/7] Implicitly create bytes in exchange() --- cppjson/src/object.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cppjson/src/object.cpp b/cppjson/src/object.cpp index 538eef6..c0d6fd6 100644 --- a/cppjson/src/object.cpp +++ b/cppjson/src/object.cpp @@ -18,7 +18,7 @@ cppjson::JsonObject::JsonObject(const cppjson::JsonObject& other) cppjson::JsonObject::JsonObject(JsonObject&& other) { this->_dataType = std::exchange(other._dataType, cppjson::JsonType::Null); - this->_dataStorage = std::exchange(other._dataStorage, ::operator new(DataStorageSize)); + this->_dataStorage = std::exchange(other._dataStorage, static_cast(::operator new(DataStorageSize))); } cppjson::JsonObject& cppjson::JsonObject::operator=(const cppjson::JsonObject& other) { @@ -35,7 +35,7 @@ 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, ::operator new(DataStorageSize)); + this->_dataStorage = std::exchange(other._dataStorage, static_cast(::operator new(DataStorageSize))); } return *this; } From fe08e1dd1b9306f4524f6b1bdf0085d31def95f2 Mon Sep 17 00:00:00 2001 From: Tymianek Date: Mon, 19 May 2025 08:42:12 +0200 Subject: [PATCH 6/7] Fix includes lol (utility, cstring) --- cppjson/src/object.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cppjson/src/object.cpp b/cppjson/src/object.cpp index c0d6fd6..b43f149 100644 --- a/cppjson/src/object.cpp +++ b/cppjson/src/object.cpp @@ -1,7 +1,8 @@ #include "cppjson/object.hpp" #include #include -#include +#include +#include constexpr std::size_t DataStorageSize = std::max({sizeof(std::string), sizeof(cppjson::Object), sizeof(double), sizeof(bool)}); From 98ce28583afa5f8166586bbab9661f50716e0e14 Mon Sep 17 00:00:00 2001 From: Tymianek Date: Mon, 19 May 2025 08:45:16 +0200 Subject: [PATCH 7/7] Destroy() objects --- cppjson/src/object.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cppjson/src/object.cpp b/cppjson/src/object.cpp index b43f149..ea50613 100644 --- a/cppjson/src/object.cpp +++ b/cppjson/src/object.cpp @@ -49,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 } }