Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cppjson/include/cppjson/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
Expand Down
39 changes: 38 additions & 1 deletion cppjson/src/object.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
#include "cppjson/object.hpp"
#include <new>
#include <stdexcept>
#include <cstring>
#include <utility>

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

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

cppjson::JsonObject::JsonObject(const cppjson::JsonObject& other)
{
if (other._dataStorage == nullptr) return;

this->_dataType = other._dataType;
this->_dataStorage = static_cast<std::byte*>(::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<std::byte*>(::operator new(DataStorageSize)));
}
cppjson::JsonObject& cppjson::JsonObject::operator=(const cppjson::JsonObject& other)
{
if (&other != this)
{
this->_dataType = other._dataType;
this->_dataStorage = static_cast<std::byte*>(::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<std::byte*>(::operator new(DataStorageSize)));
}
return *this;
}
cppjson::JsonObject::~JsonObject()
{
this->Destroy();
Expand All @@ -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<std::string>().~string();
case JsonType::String: DangerousAs<std::string>().~string(); break;
case JsonType::Object: DangerousAs<cppjson::Object>().~Object(); break;
// TODO: Array
}
}

Expand Down
Loading