loom
1/*
2MIT License
3
4Copyright (c) 2022 МГТУ им. Н.Э. Баумана, кафедра ИУ-6, Михаил Фетисов,
5
6https://bmstu.codes/lsx/simodo/loom
7*/
8
9#include "simodo/variable/Variable.h"
10#include "simodo/bormental/DrBormental.h"
11
12#include "simodo/inout/convert/functions.h"
13
14#include <cassert>
15
16namespace simodo::variable
17{
18const std::shared_ptr<Object> Specification::getObjectPtr() const
19{
20if (!isObject()) {
21assert(isNull());
22const_cast<Specification *>(this)->setValue(std::make_shared<Object>());
23}
24
25assert(isObject());
26return getObject();
27}
28
29void Specification::clone(const Specification & other)
30{
31setValue(other.object()->copy());
32}
33
34const Value & Specification::find(const std::u16string & spec_name) const
35{
36static const Value null = {};
37
38if (!isObject())
39return null;
40
41auto it = std::find_if(object()->variables().begin(), object()->variables().end(),
42[spec_name](const Variable & v){
43return v.name() == spec_name;
44});
45
46if (it == object()->variables().end())
47return null;
48
49return it->value();
50}
51
52void Specification::set(const std::u16string & name, const Value & value)
53{
54assert(!name.empty());
55
56Variable & v = object()->getVariableByName_mutable(name);
57
58if (v.name().empty())
59object()->add(Variable{name, value});
60else
61v.setValue(value);
62}
63
64}