blob: 818e94a64dc64b7244bf178196d814053bdb56b8 [file] [log] [blame]
Thomas Psota0187a4e2020-04-30 15:12:54 +02001#include <BaSyx/submodel/map_v2/reference/Reference.h>
2
jwendelil425bb0e92020-06-15 12:09:42 +02003#include <BaSyx/submodel/enumerations/KeyType.h>
4#include <BaSyx/submodel/enumerations/KeyElements.h>
Thomas Psota0187a4e2020-04-30 15:12:54 +02005
6#include <vector>
7
8using namespace basyx::submodel;
9using namespace basyx::submodel::map;
10
11struct KeyPath
12{
13 static constexpr char IdType[] = "idType";
14 static constexpr char Type[] = "type";
15 static constexpr char Value[] = "value";
16 static constexpr char Local[] = "local";
17};
18
Thomas Psotad214dc02020-04-30 15:35:04 +020019constexpr char KeyPath::IdType[];
20constexpr char KeyPath::Type[];
21constexpr char KeyPath::Value[];
22constexpr char KeyPath::Local[];
23
Thomas Psota0187a4e2020-04-30 15:12:54 +020024Reference::Reference()
25 : vab::ElementMap{}
26{
27 this->map.insertKey("keys", basyx::object::make_object_list());
28}
29
30Reference::Reference(const simple::Key & key)
31 : Reference{}
32{
33 this->addKey(key);
34};
35
36Reference::Reference(const std::vector<simple::Key> & keys)
37 : Reference{}
38{
39 for (const auto & key : keys)
40 this->addKey(key);
41};
42
Thomas Psotaba6c39b2020-05-13 18:17:22 +020043Reference::Reference(const IReference & other)
44 : Reference{other.getKeys()}
jwendelil480f1c212020-06-04 15:30:19 +020045{};
46
47
48Reference::Reference(basyx::object &object)
49 : Reference(keyMapList_to_keyList(object.getProperty("keys").Get<object::object_list_t&>()))
50{}
Thomas Psotaba6c39b2020-05-13 18:17:22 +020051
52
53std::vector<simple::Key> Reference::getKeys() const
Thomas Psota0187a4e2020-04-30 15:12:54 +020054{
jwendelil480f1c212020-06-04 15:30:19 +020055 return this->keyMapList_to_keyList(this->map.getProperty("keys").Get<basyx::object::object_list_t&>());
Thomas Psota0187a4e2020-04-30 15:12:54 +020056};
57
58
59void Reference::addKey(const simple::Key & key)
60{
61 basyx::object keyMap = basyx::object::make_map();
jwendelil425bb0e92020-06-15 12:09:42 +020062 keyMap.insertKey(KeyPath::IdType, KeyType_::to_string(key.getIdType()));
63 keyMap.insertKey(KeyPath::Type, KeyElements_::to_string(key.getType()));
Thomas Psota0187a4e2020-04-30 15:12:54 +020064 keyMap.insertKey(KeyPath::Value, key.getValue());
65 keyMap.insertKey(KeyPath::Local, key.isLocal());
66 this->map.getProperty("keys").insert(keyMap);
Thomas Psotaba6c39b2020-05-13 18:17:22 +020067}
68
69Reference & Reference::operator=(const api::IReference & other)
70{
71 this->map.insertKey("keys", basyx::object::make_object_list());
72
73 for (const auto & key : other.getKeys())
74 this->addKey(key);
75
76 return *this;
77}
78
79
80bool Reference::empty() const
81{
82 return this->map.getProperty("keys").empty();
83}
jwendelil480f1c212020-06-04 15:30:19 +020084
85simple::Key Reference::keyMap_to_key(basyx::object &keyMap)
86{
87 return simple::Key
88 (
jwendelil425bb0e92020-06-15 12:09:42 +020089 KeyElements_::from_string(keyMap.getProperty(KeyPath::Type).Get<std::string&>()),
jwendelil480f1c212020-06-04 15:30:19 +020090 keyMap.getProperty(KeyPath::Local).Get<bool>(),
jwendelil425bb0e92020-06-15 12:09:42 +020091 KeyType_::from_string(keyMap.getProperty(KeyPath::IdType).Get<std::string&>()),
jwendelil480f1c212020-06-04 15:30:19 +020092 keyMap.getProperty(KeyPath::Value).Get<std::string&>()
93 );
94}
95
96std::vector<simple::Key> Reference::keyMapList_to_keyList(basyx::object::object_list_t &keyMapList)
97{
98 std::vector<simple::Key> keys;
99
100 for (auto & keyMap : keyMapList)
101 {
102 keys.emplace_back(keyMap_to_key(keyMap));
103 };
104
105 return keys;
106}
107
Constantin Ziesche02817f12020-08-04 21:40:43 +0200108bool Reference::operator!=(const Reference & other) const
109{
110 return this->getKeys() != other.getKeys();
111};
112
113bool Reference::operator!=(const api::IReference & other) const
114{
115 return this->getKeys() != other.getKeys();
116};
117