ProviderSegmentBase.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <string>
5 
7 
8 #include "EntityBase.h"
9 #include "detail/AronTyped.h"
11 #include "detail/Predictive.h"
13 #include "detail/lookup_mixins.h"
14 
15 namespace armarx::armem::base
16 {
17 
18  /**
19  * @brief Data of a provider segment containing multiple entities.
20  */
21  template <class _EntityT, class _Derived>
23  public detail::MemoryContainerBase<std::map<std::string, _EntityT>, _Derived>,
24  public detail::AronTyped,
25  public detail::Predictive<_Derived>,
26  public detail::ForEachEntityInstanceMixin<_Derived>,
27  public detail::ForEachEntitySnapshotMixin<_Derived>,
28  public detail::GetFindInstanceMixin<_Derived>,
29  public detail::GetFindSnapshotMixin<_Derived>
30  {
32 
33  public:
34  using typename Base::ContainerT;
35  using typename Base::DerivedT;
36 
37  using EntityT = _EntityT;
38  using EntitySnapshotT = typename EntityT::EntitySnapshotT;
39  using EntityInstanceT = typename EntitySnapshotT::EntityInstanceT;
40 
41  using ChildT = EntityT;
42 
43  struct UpdateResult
44  {
48  std::vector<EntitySnapshotT> removedSnapshots;
49 
50  UpdateResult() = default;
51 
52  UpdateResult(const typename EntityT::UpdateResult& c) :
54  {
55  }
56  };
57 
58 
59  public:
61  {
62  }
63 
64  explicit ProviderSegmentBase(const std::string& name,
66  const std::vector<PredictionEngine>& predictionEngines = {}) :
68  {
69  }
70 
71  explicit ProviderSegmentBase(const std::string& name,
72  const MemoryID parentID,
74  const std::vector<PredictionEngine>& predictionEngines = {}) :
76  {
77  }
78 
79  explicit ProviderSegmentBase(const MemoryID id,
81  const std::vector<PredictionEngine>& predictionEngines = {}) :
83  {
84  }
85 
86  ProviderSegmentBase(const ProviderSegmentBase& other) = default;
87  ProviderSegmentBase(ProviderSegmentBase&& other) = default;
88  ProviderSegmentBase& operator=(const ProviderSegmentBase& other) = default;
90 
91  // READ ACCESS
92 
93  // Get key
94  inline std::string&
95  name()
96  {
97  return this->id().providerSegmentName;
98  }
99 
100  inline const std::string&
101  name() const
102  {
103  return this->id().providerSegmentName;
104  }
105 
106  // Has child by key
107  bool
108  hasEntity(const std::string& name) const
109  {
110  return this->findEntity(name) != nullptr;
111  }
112 
113  // Has child by ID
114  bool
115  hasEntity(const MemoryID& entityID) const
116  {
117  return this->findEntity(entityID) != nullptr;
118  }
119 
120  // Find child by key
121  EntityT*
122  findEntity(const std::string& name)
123  {
124  return detail::findChildByKey(name, this->_container);
125  }
126 
127  const EntityT*
128  findEntity(const std::string& name) const
129  {
130  return detail::findChildByKey(name, this->_container);
131  }
132 
133  // Get child by key
134  EntityT&
135  getEntity(const std::string& name)
136  {
137  return detail::getChildByKey(name, this->_container, *this);
138  }
139 
140  const EntityT&
141  getEntity(const std::string& name) const
142  {
143  return detail::getChildByKey(name, this->_container, *this);
144  }
145 
146  // Find child by MemoryID
147  EntityT*
148  findEntity(const MemoryID& entityID)
149  {
150  detail::checkHasEntityName(entityID);
151  return this->findEntity(entityID.entityName);
152  }
153 
154  const EntityT*
155  findEntity(const MemoryID& entityID) const
156  {
157  detail::checkHasEntityName(entityID);
158  return this->findEntity(entityID.entityName);
159  }
160 
161  // Get child by MemoryID
162  EntityT&
163  getEntity(const MemoryID& entityID)
164  {
165  detail::checkHasEntityName(entityID);
166  return this->getEntity(entityID.entityName);
167  }
168 
169  const EntityT&
170  getEntity(const MemoryID& entityID) const
171  {
172  detail::checkHasEntityName(entityID);
173  return this->getEntity(entityID.entityName);
174  }
175 
176  // get/findInstance are provided by GetFindInstanceMixin
177  // get/findSnapshot are provided by GetFindSnapshotMixin
178 
179 
180  // ITERATION
181 
182  // All functors are taken as universal reference (F&&).
183 
184  /**
185  * @param func Function like: bool process(EntityT& entity)
186  */
187  template <class EntityFunctionT>
188  bool
189  forEachEntity(EntityFunctionT&& func)
190  {
191  return this->forEachChild(func);
192  }
193 
194  /**
195  * @param func Function like: bool process(const EntityT& entity)
196  */
197  template <class EntityFunctionT>
198  bool
199  forEachEntity(EntityFunctionT&& func) const
200  {
201  return this->forEachChild(func);
202  }
203 
204  // forEachSnapshot() is provided by ForEachEntitySnapshotMixin
205  // forEachInstance() is provided by ForEachEntityInstanceMixin
206 
207  /**
208  * @param func Function like void process(EntityInstanceT& instance)>
209  */
210  template <class InstanceFunctionT>
211  bool
212  forEachInstanceIn(const MemoryID& id, InstanceFunctionT&& func)
213  {
214  return detail::forEachInstanceIn(id,
215  func,
216  *this,
217  id.hasEntityName(),
218  id.hasEntityName() ? this->findEntity(id.entityName)
219  : nullptr);
220  }
221 
222  /**
223  * @param func Function like void process(EntityInstanceT& instance)>
224  */
225  template <class InstanceFunctionT>
226  bool
227  forEachInstanceIn(const MemoryID& id, InstanceFunctionT&& func) const
228  {
229  return detail::forEachInstanceIn(id,
230  func,
231  *this,
232  id.hasEntityName(),
233  id.hasEntityName() ? this->findEntity(id.entityName)
234  : nullptr);
235  }
236 
237  // Get child keys
238  std::vector<std::string>
240  {
241  return simox::alg::get_keys(this->_container);
242  }
243 
244  // MODIFICATION
245 
246  /**
247  * @brief Updates an entity's history.
248  *
249  * Missing entity entries are added before updating.
250  */
251  UpdateResult
253  {
254  this->_checkContainerName(update.entityID.providerSegmentName, this->name());
255 
256  EntityT* entity;
257  UpdateType updateType = UpdateType::InsertedNew;
258 
259  auto it = this->_container.find(update.entityID.entityName);
260  if (it == this->_container.end())
261  {
262  // Add entity entry.
263  entity = &addEntity(update.entityID.entityName);
264  updateType = UpdateType::InsertedNew;
265  }
266  else
267  {
268  entity = &it->second;
269  updateType = UpdateType::UpdatedExisting;
270  }
271  // Update entity.
272  UpdateResult ret(entity->update(update));
273  ret.providerSegmentUpdateType = updateType;
274  return ret;
275  }
276 
277  template <class OtherDerivedT>
278  void
279  append(const OtherDerivedT& other)
280  {
281  other.forEachEntity(
282  [this](const auto& entity)
283  {
284  auto it = this->_container.find(entity.name());
285  if (it == this->_container.end())
286  {
287  it = this->_container
288  .emplace(entity.name(), this->id().withEntityName(entity.name()))
289  .first;
290  }
291  it->second.append(entity);
292  return true;
293  });
294  }
295 
296  /// Add an empty entity with the given name.
297  EntityT&
298  addEntity(const std::string& name)
299  {
300  return this->_derived().addEntity(name, name);
301  }
302 
303  /// Copy and insert an entity.
304  EntityT&
305  addEntity(const EntityT& entity)
306  {
307  return this->_derived().addEntity(entity.name(), EntityT(entity));
308  }
309 
310  /// Move and insert an entity.
311  EntityT&
312  addEntity(EntityT&& entity)
313  {
314  const std::string name = entity.name(); // Copy before move.
315  return this->_derived().addEntity(name, std::move(entity));
316  }
317 
318  /// Insert an entity in-place.
319  template <class... Args>
320  EntityT&
321  addEntity(const std::string& name, Args... args)
322  {
323  ChildT& child = this->template _addChild<ChildT>(name, args...);
324  child.id() = this->id().withEntityName(name);
325  return child;
326  }
327 
328  // MISC
329 
330  bool
331  equalsDeep(const DerivedT& other) const
332  {
333  if (this->size() != other.size())
334  {
335  return false;
336  }
337  for (const auto& [key, value] : this->_container)
338  {
339  if (not other.hasEntity(key))
340  {
341  return false;
342  }
343 
344  if (not value.equalsDeep(other.getEntity(key)))
345  {
346  return false;
347  }
348  }
349  return true;
350  }
351 
352  static std::string
354  {
355  return "provider segment";
356  }
357 
358  std::string
359  getKeyString() const
360  {
361  return this->name();
362  }
363  };
364 
365 } // namespace armarx::armem::base
armarx::armem::base::detail::GetFindInstanceMixin
Definition: lookup_mixins.h:81
armarx::armem::base::ProviderSegmentBase
Data of a provider segment containing multiple entities.
Definition: ProviderSegmentBase.h:22
armarx::armem::base
Definition: CoreSegmentBase.h:15
armarx::armem::base::detail::GetFindSnapshotMixin
Definition: lookup_mixins.h:224
armarx::armem::base::detail::MemoryContainerBase< std::map< std::string, _EntityT >, _Derived >::_derived
DerivedT & _derived()
Definition: MemoryContainerBase.h:111
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::armem::base::ProviderSegmentBase::UpdateResult
Definition: ProviderSegmentBase.h:43
MemoryContainerBase.h
armarx::armem::MemoryID::providerSegmentName
std::string providerSegmentName
Definition: MemoryID.h:52
armarx::armem::base::detail::AronTyped::AronTyped
AronTyped(aron::type::ObjectPtr aronType=nullptr)
Definition: AronTyped.cpp:9
armarx::armem::base::ProviderSegmentBase::addEntity
EntityT & addEntity(const EntityT &entity)
Copy and insert an entity.
Definition: ProviderSegmentBase.h:305
armarx::armem::base::ProviderSegmentBase< Entity, ProviderSegment >::EntityInstanceT
typename EntitySnapshotT::EntityInstanceT EntityInstanceT
Definition: ProviderSegmentBase.h:39
armarx::armem::base::ProviderSegmentBase::UpdateResult::removedSnapshots
std::vector< EntitySnapshotT > removedSnapshots
Definition: ProviderSegmentBase.h:48
armarx::armem::base::ProviderSegmentBase::forEachInstanceIn
bool forEachInstanceIn(const MemoryID &id, InstanceFunctionT &&func)
Definition: ProviderSegmentBase.h:212
armarx::armem::base::detail::MemoryContainerBase< std::map< std::string, _EntityT >, _Derived >::forEachChild
bool forEachChild(ChildFunctionT &&func)
Definition: MemoryContainerBase.h:64
Prediction.h
armarx::armem::base::detail::AronTyped::aronType
aron::type::ObjectPtr & aronType()
Definition: AronTyped.cpp:18
armarx::armem::base::ProviderSegmentBase::forEachEntity
bool forEachEntity(EntityFunctionT &&func) const
Definition: ProviderSegmentBase.h:199
armarx::armem::base::detail::MemoryContainerBase
Provides default implmentations of MemoryContainer, as well as iterators (which requires a template).
Definition: MemoryContainerBase.h:16
armarx::armem::base::ProviderSegmentBase::name
std::string & name()
Definition: ProviderSegmentBase.h:95
armarx::armem::base::ProviderSegmentBase::findEntity
EntityT * findEntity(const MemoryID &entityID)
Definition: ProviderSegmentBase.h:148
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::armem::base::ProviderSegmentBase::equalsDeep
bool equalsDeep(const DerivedT &other) const
Definition: ProviderSegmentBase.h:331
armarx::armem::base::ProviderSegmentBase::UpdateResult::providerSegmentUpdateType
armarx::armem::UpdateType providerSegmentUpdateType
Definition: ProviderSegmentBase.h:45
armarx::armem::base::ProviderSegmentBase::UpdateResult::UpdateResult
UpdateResult()=default
armarx::armem::base::ProviderSegmentBase< Entity, ProviderSegment >::EntitySnapshotT
typename EntityT::EntitySnapshotT EntitySnapshotT
Definition: ProviderSegmentBase.h:38
AronTyped.h
lookup_mixins.h
armarx::armem::base::ProviderSegmentBase::ProviderSegmentBase
ProviderSegmentBase(const std::string &name, aron::type::ObjectPtr aronType=nullptr, const std::vector< PredictionEngine > &predictionEngines={})
Definition: ProviderSegmentBase.h:64
armarx::armem::base::ProviderSegmentBase::operator=
ProviderSegmentBase & operator=(const ProviderSegmentBase &other)=default
armarx::armem::base::ProviderSegmentBase::addEntity
EntityT & addEntity(EntityT &&entity)
Move and insert an entity.
Definition: ProviderSegmentBase.h:312
armarx::armem::UpdateType::UpdatedExisting
@ UpdatedExisting
armarx::armem::base::detail::MemoryContainerBase< std::map< std::string, _EntityT >, _Derived >::size
std::size_t size() const
Definition: MemoryContainerBase.h:48
armarx::armem::base::ProviderSegmentBase::append
void append(const OtherDerivedT &other)
Definition: ProviderSegmentBase.h:279
armarx::armem::base::ProviderSegmentBase::UpdateResult::entityUpdateType
armarx::armem::UpdateType entityUpdateType
Definition: ProviderSegmentBase.h:46
armarx::armem::base::ProviderSegmentBase< Entity, ProviderSegment >::ChildT
EntityT ChildT
Definition: ProviderSegmentBase.h:41
armarx::armem::base::ProviderSegmentBase::getLevelName
static std::string getLevelName()
Definition: ProviderSegmentBase.h:353
armarx::armem::base::ProviderSegmentBase::findEntity
const EntityT * findEntity(const MemoryID &entityID) const
Definition: ProviderSegmentBase.h:155
armarx::armem::base::ProviderSegmentBase::getEntityNames
std::vector< std::string > getEntityNames() const
Definition: ProviderSegmentBase.h:239
armarx::armem::base::ProviderSegmentBase::getKeyString
std::string getKeyString() const
Definition: ProviderSegmentBase.h:359
armarx::armem::MemoryID::withProviderSegmentName
MemoryID withProviderSegmentName(const std::string &name) const
Definition: MemoryID.cpp:412
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::base::ProviderSegmentBase::UpdateResult::UpdateResult
UpdateResult(const typename EntityT::UpdateResult &c)
Definition: ProviderSegmentBase.h:52
armarx::armem::base::ProviderSegmentBase::hasEntity
bool hasEntity(const MemoryID &entityID) const
Definition: ProviderSegmentBase.h:115
armarx::armem::UpdateType
UpdateType
The type of an update.
Definition: Commit.h:18
if
if(!yyvaluep)
Definition: Grammar.cpp:724
armarx::armem::base::ProviderSegmentBase::hasEntity
bool hasEntity(const std::string &name) const
Definition: ProviderSegmentBase.h:108
armarx::armem::base::detail::findChildByKey
auto * findChildByKey(const KeyT &key, ContainerT &&container)
Find a child in a container by its key.
Definition: lookup_mixins.h:32
armarx::armem::base::detail::checkHasEntityName
void checkHasEntityName(const MemoryID &entityID)
Throw armem::error::InvalidMemoryID if the given ID has no entity name.
Definition: lookup_mixins.cpp:27
armarx::armem::base::ProviderSegmentBase::getEntity
const EntityT & getEntity(const MemoryID &entityID) const
Definition: ProviderSegmentBase.h:170
EntityBase.h
armarx::armem::base::ProviderSegmentBase::UpdateResult::id
MemoryID id
Definition: ProviderSegmentBase.h:47
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:27
armarx::armem::base::ProviderSegmentBase::forEachInstanceIn
bool forEachInstanceIn(const MemoryID &id, InstanceFunctionT &&func) const
Definition: ProviderSegmentBase.h:227
iteration_mixins.h
armarx::armem::base::ProviderSegmentBase::getEntity
EntityT & getEntity(const MemoryID &entityID)
Definition: ProviderSegmentBase.h:163
armarx::armem::base::detail::getChildByKey
auto & getChildByKey(const KeyT &key, ContainerT &&container, const ParentT &owner, KeyStringFn &&keyStringFn)
Retrieve a child in a container by its key.
Definition: lookup_mixins.h:49
armarx::armem::base::detail::MemoryContainerBase< std::map< std::string, _EntityT >, _Derived >::DerivedT
_Derived DerivedT
Definition: MemoryContainerBase.h:23
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
armarx::armem::base::ProviderSegmentBase::getEntity
const EntityT & getEntity(const std::string &name) const
Definition: ProviderSegmentBase.h:141
armarx::armem::base::detail::forEachInstanceIn
bool forEachInstanceIn(const MemoryID &id, FunctionT &&func, ParentT &parent, bool single, ChildT *child)
Definition: iteration_mixins.h:116
armarx::armem::MemoryID::entityName
std::string entityName
Definition: MemoryID.h:53
armarx::armem::base::detail::MemoryContainerBase< std::map< std::string, _EntityT >, _Derived >::ContainerT
std::map< std::string, _EntityT > ContainerT
Definition: MemoryContainerBase.h:24
armarx::armem::base::detail::ForEachEntitySnapshotMixin
Definition: iteration_mixins.h:225
armarx::armem::base::ProviderSegmentBase::ProviderSegmentBase
ProviderSegmentBase(const std::string &name, const MemoryID parentID, aron::type::ObjectPtr aronType=nullptr, const std::vector< PredictionEngine > &predictionEngines={})
Definition: ProviderSegmentBase.h:71
armarx::armem::base::detail::AronTyped
Something with a specific ARON type.
Definition: AronTyped.h:12
armarx::armem::MemoryID::withEntityName
MemoryID withEntityName(const std::string &name) const
Definition: MemoryID.cpp:420
armarx::armem::base::ProviderSegmentBase::ProviderSegmentBase
ProviderSegmentBase(const MemoryID id, aron::type::ObjectPtr aronType=nullptr, const std::vector< PredictionEngine > &predictionEngines={})
Definition: ProviderSegmentBase.h:79
armarx::armem::base::ProviderSegmentBase::forEachEntity
bool forEachEntity(EntityFunctionT &&func)
Definition: ProviderSegmentBase.h:189
armarx::armem::base::detail::ForEachEntityInstanceMixin
Definition: iteration_mixins.h:137
armarx::armem::UpdateType::InsertedNew
@ InsertedNew
armarx::armem::base::ProviderSegmentBase::addEntity
EntityT & addEntity(const std::string &name, Args... args)
Insert an entity in-place.
Definition: ProviderSegmentBase.h:321
armarx::armem::base::ProviderSegmentBase::ProviderSegmentBase
ProviderSegmentBase()
Definition: ProviderSegmentBase.h:60
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
armarx::armem::base::detail::Predictive< _Derived >::predictionEngines
const std::vector< PredictionEngine > & predictionEngines() const
Definition: Predictive.h:48
armarx::armem::base::ProviderSegmentBase::addEntity
EntityT & addEntity(const std::string &name)
Add an empty entity with the given name.
Definition: ProviderSegmentBase.h:298
armarx::armem::base::detail::MemoryContainerBase< std::map< std::string, _EntityT >, _Derived >::_container
ContainerT _container
Definition: MemoryContainerBase.h:150
armarx::armem::base::ProviderSegmentBase::getEntity
EntityT & getEntity(const std::string &name)
Definition: ProviderSegmentBase.h:135
armarx::armem::base::ProviderSegmentBase::findEntity
const EntityT * findEntity(const std::string &name) const
Definition: ProviderSegmentBase.h:128
armarx::armem::base::ProviderSegmentBase::findEntity
EntityT * findEntity(const std::string &name)
Definition: ProviderSegmentBase.h:122
armarx::armem::EntityUpdate::entityID
MemoryID entityID
The entity's ID.
Definition: Commit.h:30
armarx::armem::base::ProviderSegmentBase::update
UpdateResult update(const EntityUpdate &update)
Updates an entity's history.
Definition: ProviderSegmentBase.h:252
armarx::armem::base::ProviderSegmentBase< Entity, ProviderSegment >::EntityT
Entity EntityT
Definition: ProviderSegmentBase.h:37
armarx::armem::base::ProviderSegmentBase::name
const std::string & name() const
Definition: ProviderSegmentBase.h:101
armarx::armem::base::detail::MemoryContainerBase< std::map< std::string, _EntityT >, _Derived >::_checkContainerName
void _checkContainerName(const std::string &gottenName, const std::string &actualName, bool emptyOk=true) const
Definition: MemoryContainerBase.h:124
Predictive.h
armarx::armem::base::detail::Predictive
Something that supports a set of prediction engines.
Definition: Predictive.h:38
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29