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