Thomas Psota | 0187a4e | 2020-04-30 15:12:54 +0200 | [diff] [blame] | 1 | #include <BaSyx/submodel/map_v2/reference/Reference.h> |
| 2 | |
jwendelil4 | 25bb0e9 | 2020-06-15 12:09:42 +0200 | [diff] [blame] | 3 | #include <BaSyx/submodel/enumerations/KeyType.h> |
| 4 | #include <BaSyx/submodel/enumerations/KeyElements.h> |
Thomas Psota | 0187a4e | 2020-04-30 15:12:54 +0200 | [diff] [blame] | 5 | |
| 6 | #include <vector> |
| 7 | |
| 8 | using namespace basyx::submodel; |
| 9 | using namespace basyx::submodel::map; |
| 10 | |
| 11 | struct 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 Psota | d214dc0 | 2020-04-30 15:35:04 +0200 | [diff] [blame] | 19 | constexpr char KeyPath::IdType[]; |
| 20 | constexpr char KeyPath::Type[]; |
| 21 | constexpr char KeyPath::Value[]; |
| 22 | constexpr char KeyPath::Local[]; |
| 23 | |
Thomas Psota | 0187a4e | 2020-04-30 15:12:54 +0200 | [diff] [blame] | 24 | Reference::Reference() |
| 25 | : vab::ElementMap{} |
| 26 | { |
| 27 | this->map.insertKey("keys", basyx::object::make_object_list()); |
| 28 | } |
| 29 | |
| 30 | Reference::Reference(const simple::Key & key) |
| 31 | : Reference{} |
| 32 | { |
| 33 | this->addKey(key); |
| 34 | }; |
| 35 | |
| 36 | Reference::Reference(const std::vector<simple::Key> & keys) |
| 37 | : Reference{} |
| 38 | { |
| 39 | for (const auto & key : keys) |
| 40 | this->addKey(key); |
| 41 | }; |
| 42 | |
Thomas Psota | ba6c39b | 2020-05-13 18:17:22 +0200 | [diff] [blame] | 43 | Reference::Reference(const IReference & other) |
| 44 | : Reference{other.getKeys()} |
jwendelil4 | 80f1c21 | 2020-06-04 15:30:19 +0200 | [diff] [blame] | 45 | {}; |
| 46 | |
| 47 | |
| 48 | Reference::Reference(basyx::object &object) |
| 49 | : Reference(keyMapList_to_keyList(object.getProperty("keys").Get<object::object_list_t&>())) |
| 50 | {} |
Thomas Psota | ba6c39b | 2020-05-13 18:17:22 +0200 | [diff] [blame] | 51 | |
| 52 | |
| 53 | std::vector<simple::Key> Reference::getKeys() const |
Thomas Psota | 0187a4e | 2020-04-30 15:12:54 +0200 | [diff] [blame] | 54 | { |
jwendelil4 | 80f1c21 | 2020-06-04 15:30:19 +0200 | [diff] [blame] | 55 | return this->keyMapList_to_keyList(this->map.getProperty("keys").Get<basyx::object::object_list_t&>()); |
Thomas Psota | 0187a4e | 2020-04-30 15:12:54 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | |
| 59 | void Reference::addKey(const simple::Key & key) |
| 60 | { |
| 61 | basyx::object keyMap = basyx::object::make_map(); |
jwendelil4 | 25bb0e9 | 2020-06-15 12:09:42 +0200 | [diff] [blame] | 62 | keyMap.insertKey(KeyPath::IdType, KeyType_::to_string(key.getIdType())); |
| 63 | keyMap.insertKey(KeyPath::Type, KeyElements_::to_string(key.getType())); |
Thomas Psota | 0187a4e | 2020-04-30 15:12:54 +0200 | [diff] [blame] | 64 | keyMap.insertKey(KeyPath::Value, key.getValue()); |
| 65 | keyMap.insertKey(KeyPath::Local, key.isLocal()); |
| 66 | this->map.getProperty("keys").insert(keyMap); |
Thomas Psota | ba6c39b | 2020-05-13 18:17:22 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | Reference & 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 | |
| 80 | bool Reference::empty() const |
| 81 | { |
| 82 | return this->map.getProperty("keys").empty(); |
| 83 | } |
jwendelil4 | 80f1c21 | 2020-06-04 15:30:19 +0200 | [diff] [blame] | 84 | |
| 85 | simple::Key Reference::keyMap_to_key(basyx::object &keyMap) |
| 86 | { |
| 87 | return simple::Key |
| 88 | ( |
jwendelil4 | 25bb0e9 | 2020-06-15 12:09:42 +0200 | [diff] [blame] | 89 | KeyElements_::from_string(keyMap.getProperty(KeyPath::Type).Get<std::string&>()), |
jwendelil4 | 80f1c21 | 2020-06-04 15:30:19 +0200 | [diff] [blame] | 90 | keyMap.getProperty(KeyPath::Local).Get<bool>(), |
jwendelil4 | 25bb0e9 | 2020-06-15 12:09:42 +0200 | [diff] [blame] | 91 | KeyType_::from_string(keyMap.getProperty(KeyPath::IdType).Get<std::string&>()), |
jwendelil4 | 80f1c21 | 2020-06-04 15:30:19 +0200 | [diff] [blame] | 92 | keyMap.getProperty(KeyPath::Value).Get<std::string&>() |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | std::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 Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 108 | bool Reference::operator!=(const Reference & other) const |
| 109 | { |
| 110 | return this->getKeys() != other.getKeys(); |
| 111 | }; |
| 112 | |
| 113 | bool Reference::operator!=(const api::IReference & other) const |
| 114 | { |
| 115 | return this->getKeys() != other.getKeys(); |
| 116 | }; |
| 117 | |