util.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @author Fabian Reister ( fabian dot reister at kit dot edu )
17 * @date 2021
18 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19 * GNU General Public License
20 */
21
22#pragma once
23
24#include <optional>
25#include <vector>
26
28
32
33namespace armarx::armem
34{
35
36 /**
37 * @brief Tries to cast a armem::EntityInstance to AronClass
38 *
39 * @tparam AronClass class name. Needs to be derived from armarx::aron::cppcodegenerator::AronCppClass
40 * @param item
41 * @return std::optional<AronClass>
42 */
43 template <typename AronClass>
44 std::optional<AronClass>
46 {
47 static_assert(std::is_base_of<armarx::aron::cpp::AronGeneratedClass, AronClass>::value);
48
49
50 if (!item.data())
51 {
52 return std::nullopt;
53 }
54
55#if 0
56 // item.data() is by definition a DictNavigator
57 if (item.data()->getDescriptor() != aron::data::Descriptor::DICT)
58 {
59 return std::nullopt;
60 }
61#endif
62
63 try
64 {
65 return AronClass::FromAron(item.data());
66 }
68 {
69 return std::nullopt;
70 }
71 }
72
73 /**
74 * @brief Returns all entities that can be cast to AronClass
75 *
76 * @tparam AronClass class name. Needs to be derived from armarx::aron::cppcodegenerator::AronCppClass
77 * @param entities collection of entities
78 * @return std::vector<AronClass>
79 */
80 template <typename AronClass>
81 std::vector<AronClass>
82 allOfType(const std::map<std::string, wm::Entity>& entities)
83 {
84 static_assert(std::is_base_of<armarx::aron::cpp::AronGeneratedClass, AronClass>::value);
85
86 std::vector<AronClass> outV;
87
88 // loop over all entities and their snapshots
89 for (const auto& [s, entity] : entities)
90 {
91 entity.forEachInstance(
92 [&outV](const wm::EntityInstance& entityInstance)
93 {
94 const auto o = tryCast<AronClass>(entityInstance);
95
96 if (o)
97 {
98 outV.push_back(*o);
99 }
100 });
101 }
102
103 return outV;
104 }
105
106 /**
107 * @brief filter + transform for entities.
108 *
109 * Can be used instead of
110 *
111 * std::vector<Bar> ret;
112 *
113 * const auto allOf = allOfType<Foo>(entities);
114 * std::transform(allOf.begin(), allOf.end(), std::back_inserter(ret), pred)
115 *
116 * This function has the benefit that the transform function will be applied directly.
117 * No intermediate vector has to be created (e.g. "allOf" in the example above).
118 *
119 * @tparam AronClass class name. Needs to be derived from armarx::aron::cppcodegenerator::AronCppClass
120 * @param entities collection of entities
121 * @param pred binary predicate function, applied to all entity instances
122 * @return vector of "pred"-transformed elements that can be cast to AronClass
123 */
124 template <typename AronClass>
125 auto
126 transformAllOfType(const std::map<std::string, wm::Entity>& entities, auto pred)
127 -> std::vector<decltype(pred(AronClass()))>
128 {
129 static_assert(std::is_base_of<armarx::aron::cpp::AronGeneratedClass, AronClass>::value);
130
131 std::vector<decltype(pred(AronClass()))> outV;
132
133 if (entities.empty())
134 {
135 ARMARX_WARNING << "No entities!";
136 }
137
138 // loop over all entities and their snapshots
139 for (const auto& [s, entity] : entities)
140 {
141 entity.forEachInstance(
142 [&outV, &pred](const wm::EntityInstance& entityInstance)
143 {
144 const auto o = tryCast<AronClass>(entityInstance);
145
146 if (o)
147 {
148 outV.push_back(pred(*o));
149 }
150 });
151 }
152
153 return outV;
154 }
155
156 /**
157 * @brief resolve a single MemoryID with the given MemoryNameSystem.
158 *
159 * Returns a Memory containing only the desired segment / entity if it was
160 * successfully queried. If the query was unsuccessful (the MNS could not
161 * resolve the memory server, the MemoryID does not exist, etc.),
162 * nothing is returned.
163 *
164 * @param mns the MemoryNameSystem to use for the query
165 * @param memoryID the MemoryID to query for
166 * @return memory containing the object referenced by the MemoryID if available
167 */
168 std::optional<armarx::armem::wm::Memory> resolveID(armarx::armem::client::MemoryNameSystem& mns,
169 const armarx::armem::MemoryID& memoryID);
170
171
172 /**
173 * @brief get the data and type of the given MemoryID in the given Memory.
174 *
175 * Returns the data and the type of the latest instance corresponding to
176 * the given MemoryID (whatever `memory.findLatestInstance(memoryID)` returns).
177 * If no such instance exists in the memory or no type for the instance is available,
178 * nothing is returned.
179 *
180 * @param memory the Memory to extract the data and type from
181 * @param memoryID the MemoryID of the instance to extract
182 * @return pair of data and type for the instance if available
183 */
184 std::optional<std::pair<armarx::aron::data::DictPtr, armarx::aron::type::ObjectPtr>>
186 const armarx::armem::MemoryID& memoryID);
187
188} // namespace armarx::armem
The memory name system (MNS) client.
Client-side working memory.
A base class for aron exceptions.
Definition Exception.h:37
Brief description of class memory.
Definition memory.h:39
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
armem::wm::EntityInstance EntityInstance
auto transformAllOfType(const std::map< std::string, wm::Entity > &entities, auto pred) -> std::vector< decltype(pred(AronClass()))>
filter + transform for entities.
Definition util.h:126
std::vector< AronClass > allOfType(const std::map< std::string, wm::Entity > &entities)
Returns all entities that can be cast to AronClass.
Definition util.h:82
std::optional< std::pair< armarx::aron::data::DictPtr, armarx::aron::type::ObjectPtr > > extractDataAndType(const armarx::armem::wm::Memory &memory, const armarx::armem::MemoryID &memoryID)
get the data and type of the given MemoryID in the given Memory.
Definition util.cpp:34
std::optional< armarx::armem::wm::Memory > resolveID(armarx::armem::client::MemoryNameSystem &mns, const armarx::armem::MemoryID &memoryID)
resolve a single MemoryID with the given MemoryNameSystem.
Definition util.cpp:10
std::optional< AronClass > tryCast(const wm::EntityInstance &item)
Tries to cast a armem::EntityInstance to AronClass.
Definition util.h:45