mirror of
https://codeberg.org/vyn/rei-json.git
synced 2025-07-01 09:33:19 +00:00
127 lines
3.2 KiB
C++
127 lines
3.2 KiB
C++
#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();
|
|
}
|
|
}
|
|
|
|
|
|
}
|