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
29const Value & Specification::find(const std::u16string & spec_name) const
30{
31static const Value null = {};
32
33if (!isObject())
34return null;
35
36auto it = std::find_if(object()->variables().begin(), object()->variables().end(),
37[spec_name](const Variable & v){
38return v.name() == spec_name;
39});
40
41if (it == object()->variables().end())
42return null;
43
44return it->value();
45}
46
47void Specification::set(const std::u16string & name, const Value & value)
48{
49auto it = std::find_if(object()->variables().begin(), object()->variables().end(),
50[name](const Variable & v){
51return v.name() == name;
52});
53
54if (it == object()->variables().end())
55object()->variables().push_back(Variable{name, value});
56else
57it->value() = value;
58}
59
60}