9
#include "simodo/variable/Variable.h"
11
#include "simodo/bormental/DrBormental.h"
12
#include "simodo/inout/format/fmt.h"
16
namespace simodo::variable
18
std::shared_ptr<Object> Value::getInternalParent() const
20
if (type() != ValueType::Function)
23
std::shared_ptr<Object> obj = getObject();
25
assert(!obj->variables().empty());
27
variable::Variable & function_core = obj->getVariableByIndex_mutable(0);
29
assert(function_core.type() == variable::ValueType::IntFunction);
31
return function_core.value().getIntFunctionParent();
34
Value Value::copy() const
38
case ValueType::Array:
39
if (std::holds_alternative<std::shared_ptr<Array>>(_variant))
40
return std::make_shared<Array>(getArray()->copy());
42
case ValueType::Function:
45
case ValueType::Object:
46
if (std::holds_alternative<std::shared_ptr<Object>>(_variant)) {
47
Value ret { _type, std::make_shared<Object>(getObject()->copy()) };
48
ret.checkInternalParents();
53
assert(getRef().origin().type() != ValueType::Ref);
54
return getRef().origin().value().copy();
60
void Value::checkInternalParents()
62
if (type() == ValueType::Object) {
63
std::shared_ptr<Object> object = getObject();
65
object->resetInternalParents(object);
69
void Value::setInternalParent(std::shared_ptr<Object> parent)
71
assert(type() == ValueType::Function);
73
std::shared_ptr<Object> function_object = getFunction();
75
assert(!function_object->variables().empty());
77
Variable & function_core = function_object->getVariableByIndex_mutable(0);
79
assert(function_core.type() != ValueType::Null);
81
if (function_core.type() == ValueType::IntFunction)
82
function_core.value().getIntFunctionParent() = parent;
85
void Value::setValueRecursively(const Value & value)
88
setValue(value.copy());
93
assert(_type == value.type());
97
case ValueType::Array:
98
if (std::holds_alternative<std::shared_ptr<Array>>(_variant)) {
99
std::shared_ptr<Array> target_array = getArray();
100
std::shared_ptr<Array> source_array = value.getArray();
103
for(; i < source_array->values().size() && i < target_array->values().size(); ++i) {
104
Value & target_element = target_array->values_mutable()[i];
105
const Value & source_element = source_array->values_mutable()[i];
107
if (target_element.type() == source_element.type())
108
target_element.setValueRecursively(source_element);
110
throw bormental::DrBormental("Value::setValueRecursively",
111
inout::fmt("The types of array elements do not match"));
114
for(; i < source_array->values().size(); ++i)
115
target_array->add(source_array->values());
118
setValue(value.copy());
120
case ValueType::Object:
121
if (std::holds_alternative<std::shared_ptr<Object>>(_variant)) {
122
std::shared_ptr<Object> target = getObject();
123
const std::shared_ptr<Object> source = value.getObject();
125
for(const Variable & source_element : source->variables()) {
126
Variable & target_element = target->getVariableByName_mutable(source_element.name());
128
if (target_element.name().empty())
129
target->add(source_element.copyVariable());
130
else if (target_element.value().isNull())
131
target_element.setValue(source_element.value().copy());
132
else if (target_element.type() == source_element.type())
133
target_element.value().setValueRecursively(source_element.value());
135
throw bormental::DrBormental("Value::setValueRecursively",
136
inout::fmt("The types of the variable '%1' of the object do not match")
137
.arg(source_element.name()));
141
setValue(value.copy());
144
setValue(value.copy());