4
Copyright (c) 2022 МГТУ им. Н.Э. Баумана, кафедра ИУ-6, Михаил Фетисов,
6
https://bmstu.codes/lsx/simodo/loom
9
#include "simodo/variable/Variable.h"
10
#include "simodo/bormental/DrBormental.h"
11
#include "simodo/variable/FunctionWrapper.h"
12
#include "simodo/inout/format/fmt.h"
16
namespace simodo::variable
18
void Object::setFlag(ObjectFlag flag)
23
Object Object::copy() const
27
for(const Variable & member : _variables)
28
record.add(member.copyVariable());
33
void Object::swap(Object & other)
35
_variables.swap(other._variables);
38
bool Object::exists(const std::u16string & name) const
40
auto it = find_if(_variables.begin(), _variables.end(), [name](const Variable & v){
41
return v.name() == name;
44
return it != _variables.end();
47
const Value & Object::find(const std::u16string & name) const
49
static const Value undef {};
51
auto it = find_if(_variables.begin(), _variables.end(), [name](const Variable & v){
52
return v.name() == name;
55
if (it == _variables.end())
61
Value & Object::find_mutable(const std::u16string & name)
63
static Value undef {};
65
auto it = find_if(_variables.begin(), _variables.end(), [name](const Variable & v){
66
return v.name() == name;
69
if (it == _variables.end())
75
void Object::reserve(size_t capacity)
77
_variables.reserve(capacity);
80
void Object::add(const Variable & variable)
82
auto it = std::find_if(_variables.begin(), _variables.end(),
83
[variable](const Variable & v){
84
return v.name() == variable.name();
87
if (it != _variables.end())
88
throw bormental::DrBormental("Object::add",
89
inout::fmt("The element named '%1' already exists")
90
.arg(variable.name()));
92
_variables.push_back(variable);
95
void Object::set(const Variable & variable)
97
auto it = std::find_if(_variables.begin(), _variables.end(),
98
[variable](const Variable & v){
99
return v.name() == variable.name();
102
if (it != _variables.end())
103
it->setValue(variable.value());
105
_variables.push_back(variable);
108
void Object::remove(const std::u16string & name)
110
auto it = std::find_if(_variables.begin(), _variables.end(),
111
[name](const variable::Variable & v)
113
return v.name() == name;
116
if (it == _variables.end())
117
_variables.erase(it);
120
const Variable & Object::getVariableByName(const std::u16string & name) const
122
static const Variable undef {};
124
auto it = find_if(_variables.begin(), _variables.end(), [name](const Variable & v){
125
return v.name() == name;
128
if (it == _variables.end())
134
Variable & Object::getVariableByName_mutable(const std::u16string & name)
136
return const_cast<Variable &>(getVariableByName(name));
139
const Variable & Object::getVariableByIndex(index_t index) const
141
if (index >= _variables.size())
142
throw bormental::DrBormental("Object::getVariableByIndex", inout::fmt("Out of index"));
144
return _variables[index];
147
Variable & Object::getVariableByIndex_mutable(index_t index)
149
return const_cast<Variable &>(getVariableByIndex(index));
152
void Object::resetInternalParents(std::shared_ptr<Object> parent)
154
assert(parent.get() == this);
156
for(Variable & v : _variables)
157
if (v.type() == ValueType::Function)
158
v.value().setInternalParent(parent);
161
std::shared_ptr<Object> Object::getInternalParent() const
163
assert(!_variables.empty());
165
const variable::Variable & function_core = getVariableByIndex(0);
167
assert(function_core.type() == variable::ValueType::IntFunction);
169
return function_core.value().getInternalParent();
172
Value Object::invoke(const std::u16string & method_name, const VariableSet_t & arguments)
174
const Variable & function = getVariableByName(method_name);
175
if (function.type() == variable::ValueType::Null)
178
if (function.type() != variable::ValueType::Function)
183
FunctionWrapper func(function);
184
VariableSet_t args_mutable = arguments;
185
VariableSetWrapper_mutable args(args_mutable);
187
return func.invoke(args);
189
catch(const std::exception & e)