first commit

This commit is contained in:
Vyn 2024-11-11 16:36:37 +01:00
commit eb5191a722
15 changed files with 1075 additions and 0 deletions

127
src/Field.cpp Normal file
View file

@ -0,0 +1,127 @@
#include "rei-json/Field.h"
#include <algorithm>
#include <ostream>
#include <print>
#include <utility>
namespace rei::json {
FieldValue::FieldValue(const FieldValue& other) noexcept {
//std::println("I'm being copied ! (constructor) {} {}", static_cast<int>(other.type), other.number);
_copyValues(std::move(other));
}
FieldValue& FieldValue::operator=(const FieldValue& other) noexcept {
//std::println("I'm being copied ! (operator) {}", static_cast<int>(other.type));
_destroy();
_copyValues(std::move(other));
return *this;
}
FieldValue& FieldValue::operator=(FieldValue&& other) noexcept {
//std::println("I'm being moved ! (operator) {} {}", static_cast<int>(other.type), other.number);
_destroy();
_moveValues(std::move(other));
return *this;
}
FieldValue::FieldValue(FieldValue&& other) noexcept {
//std::println("I'm being moved ! (constructor) {} {}", static_cast<int>(other.type), other.number);
_moveValues(std::move(other));
}
void FieldValue::_copyValues(const FieldValue& other) {
switch (other.type) {
case rei::json::FieldType::Number:
number = other.number;
break;
case rei::json::FieldType::Bool:
boolean = other.boolean;
break;
case rei::json::FieldType::String:
new (&string) std::string(other.string);
break;
case rei::json::FieldType::Object:
new (&object) JsonObject(other.object);
break;
case rei::json::FieldType::Array:
new (&array) JsonArray(other.array);
break;
}
type = other.type;
}
void FieldValue::_moveValues(FieldValue&& other) {
switch (other.type) {
case rei::json::FieldType::Number:
number = other.number;
break;
case rei::json::FieldType::Bool:
boolean = other.boolean;
break;
case rei::json::FieldType::String:
new (&string) std::string(std::move(other.string));
break;
case rei::json::FieldType::Object:
new (&object) JsonObject(std::move(other.object));
break;
case rei::json::FieldType::Array:
new (&array) JsonArray(std::move(other.array));
break;
}
type = std::move(other.type);
}
FieldValue::FieldValue(FieldType type) : number(0), type(type) {
}
FieldValue::FieldValue(int value) : number(value), type(FieldType::Number) {
}
FieldValue::FieldValue(bool value) : boolean(value), type(FieldType::Bool) {
}
FieldValue::FieldValue(const std::string& value) : string(value), type(FieldType::String) {
}
FieldValue::FieldValue(std::string&& value) : string(std::move(value)), type(FieldType::String) {
}
FieldValue::FieldValue(const JsonObject& value) : object(value), type(FieldType::Object) {
}
FieldValue::FieldValue(JsonObject&& value) : object(std::move(value)), type(FieldType::Object) {
}
FieldValue::FieldValue(const JsonArray& value) : array(value), type(FieldType::Array) {
//std::println("Array ! copy");
}
FieldValue::FieldValue(JsonArray&& value) noexcept : array(std::move(value)), type(FieldType::Array) {
//std::println("Array ! move");
}
FieldValue::~FieldValue() {
_destroy();
}
void FieldValue::_destroy() {
if (isString()) {
(&string)->std::string::~string();
} else if (isObject()) {
object.~JsonObject();
} else if (isArray()) {
array.~JsonArray();
}
}
}