MemoryContainerBase.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include "MemoryItem.h"
6 #include "iteration_mixins.h"
7 
8 
10 {
11  /**
12  * Provides default implmentations of `MemoryContainer`, as well as
13  * iterators (which requires a template).
14  */
15  template <class _ContainerT, class _DerivedT>
17  public MemoryItem
18  {
19  using Base = MemoryItem;
20 
21  public:
22 
23  using DerivedT = _DerivedT;
24  using ContainerT = _ContainerT;
25 
26 
27  public:
28 
30  {}
31  explicit MemoryContainerBase(const MemoryID& id) :
32  MemoryItem(id)
33  {
34  }
35 
36  MemoryContainerBase(const MemoryContainerBase& other) = default;
37  MemoryContainerBase(MemoryContainerBase&& other) = default;
38  MemoryContainerBase& operator=(const MemoryContainerBase& other) = default;
40 
41 
42  // Container methods
43 
44  bool empty() const
45  {
46  return _container.empty();
47  }
48  std::size_t size() const
49  {
50  return _container.size();
51  }
52  void clear()
53  {
54  return _container.clear();
55  }
56 
57 
58  // ITERATION
59 
60  /**
61  * @param func Function like: bool process(ChildT& provSeg)
62  */
63  template <class ChildFunctionT>
64  bool forEachChild(ChildFunctionT&& func)
65  {
66  return base::detail::forEachChild(this->_container, func);
67  }
68  /**
69  * @param func Function like: bool process(const ChildT& provSeg)
70  */
71  template <class ChildFunctionT>
72  bool forEachChild(ChildFunctionT&& func) const
73  {
74  return base::detail::forEachChild(this->_container, func);
75  }
76 
77 
78  [[deprecated("Direct container access is deprecated. Use forEach*() instead.")]]
79  typename ContainerT::const_iterator begin() const
80  {
81  return _container.begin();
82  }
83  [[deprecated("Direct container access is deprecated. Use forEach*() instead.")]]
84  typename ContainerT::iterator begin()
85  {
86  return _container.begin();
87  }
88  [[deprecated("Direct container access is deprecated. Use forEach*() instead.")]]
89  typename ContainerT::const_iterator end() const
90  {
91  return _container.end();
92  }
93  [[deprecated("Direct container access is deprecated. Use forEach*() instead.")]]
94  typename ContainerT::iterator end()
95  {
96  return _container.end();
97  }
98 
99 
100  protected:
101 
102  const ContainerT& container() const
103  {
104  return _container;
105  }
107  {
108  return _container;
109  }
110 
112  {
113  return static_cast<DerivedT&>(*this);
114  }
115  const DerivedT& _derived() const
116  {
117  return static_cast<DerivedT&>(*this);
118  }
119 
120 
121  /**
122  * @throw `armem::error::ContainerNameMismatch` if `gottenName` does not match `actualName`.
123  */
124  void _checkContainerName(const std::string& gottenName, const std::string& actualName,
125  bool emptyOk = true) const
126  {
127  if (!((emptyOk && gottenName.empty()) || gottenName == actualName))
128  {
130  gottenName, DerivedT::getLevelName(), actualName);
131  }
132  }
133 
134 
135  template <class ChildT, class KeyT, class ...ChildArgs>
136  ChildT& _addChild(const KeyT& key, ChildArgs... childArgs)
137  {
138  auto [it, inserted] = this->_container.try_emplace(key, childArgs...);
139  if (not inserted)
140  {
142  ChildT::getLevelName(), key, DerivedT::getLevelName(), this->_derived().name());
143  }
144  return it->second;
145  }
146 
147 
148  protected:
149 
151 
152  };
153 
154 }
armarx::armem::base::detail::MemoryContainerBase::_derived
const DerivedT & _derived() const
Definition: MemoryContainerBase.h:115
armarx::armem::base::detail::MemoryContainerBase::empty
bool empty() const
Definition: MemoryContainerBase.h:44
armarx::armem::base::detail::MemoryContainerBase::_derived
DerivedT & _derived()
Definition: MemoryContainerBase.h:111
armarx::armem::base::detail::forEachChild
bool forEachChild(ContainerT &container, FunctionT &&func)
Definition: iteration_mixins.h:102
armarx::armem::base::detail::MemoryItem
Base class of memory classes on different levels.
Definition: MemoryItem.h:14
armarx::armem::base::detail::MemoryItem::MemoryItem
MemoryItem()
Definition: MemoryItem.cpp:7
armarx::armem::base::detail::MemoryContainerBase::forEachChild
bool forEachChild(ChildFunctionT &&func)
Definition: MemoryContainerBase.h:64
armarx::armem::base::detail
Definition: AronTyped.cpp:6
armarx::armem::base::detail::MemoryContainerBase
Provides default implmentations of MemoryContainer, as well as iterators (which requires a template).
Definition: MemoryContainerBase.h:16
MemoryItem.h
armarx::armem::base::detail::MemoryContainerBase::size
std::size_t size() const
Definition: MemoryContainerBase.h:48
ArMemError.h
armarx::armem::base::detail::MemoryContainerBase::container
const ContainerT & container() const
Definition: MemoryContainerBase.h:102
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::base::detail::MemoryContainerBase::begin
ContainerT::const_iterator begin() const
Definition: MemoryContainerBase.h:79
armarx::armem::base::detail::MemoryContainerBase::end
ContainerT::const_iterator end() const
Definition: MemoryContainerBase.h:89
armarx::armem::base::detail::MemoryContainerBase::MemoryContainerBase
MemoryContainerBase()
Definition: MemoryContainerBase.h:29
iteration_mixins.h
armarx::armem::base::detail::MemoryContainerBase< std::map< std::string, Entity >, ProviderSegment >::DerivedT
ProviderSegment DerivedT
Definition: MemoryContainerBase.h:23
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
armarx::armem::base::detail::MemoryContainerBase< std::map< std::string, Entity >, ProviderSegment >::ContainerT
std::map< std::string, Entity > ContainerT
Definition: MemoryContainerBase.h:24
armarx::armem::base::detail::MemoryContainerBase::operator=
MemoryContainerBase & operator=(const MemoryContainerBase &other)=default
armarx::armem::base::detail::MemoryContainerBase::_addChild
ChildT & _addChild(const KeyT &key, ChildArgs... childArgs)
Definition: MemoryContainerBase.h:136
armarx::armem::base::detail::MemoryContainerBase::clear
void clear()
Definition: MemoryContainerBase.h:52
armarx::armem::error::ContainerNameMismatch
Indicates that a name in a given ID does not match a container's own name.
Definition: ArMemError.h:42
armarx::armem::base::detail::MemoryContainerBase::MemoryContainerBase
MemoryContainerBase(const MemoryID &id)
Definition: MemoryContainerBase.h:31
armarx::armem::base::detail::MemoryContainerBase::end
ContainerT::iterator end()
Definition: MemoryContainerBase.h:94
armarx::armem::error::ContainerEntryAlreadyExists
Indicates that a name in a given ID does not match a container's own name.
Definition: ArMemError.h:57
armarx::armem::base::detail::MemoryContainerBase::_container
ContainerT _container
Definition: MemoryContainerBase.h:150
armarx::armem::base::detail::MemoryContainerBase::_checkContainerName
void _checkContainerName(const std::string &gottenName, const std::string &actualName, bool emptyOk=true) const
Definition: MemoryContainerBase.h:124
armarx::armem::base::detail::MemoryContainerBase::begin
ContainerT::iterator begin()
Definition: MemoryContainerBase.h:84
armarx::armem::base::detail::MemoryContainerBase::forEachChild
bool forEachChild(ChildFunctionT &&func) const
Definition: MemoryContainerBase.h:72
armarx::armem::base::detail::MemoryContainerBase::container
ContainerT & container()
Definition: MemoryContainerBase.h:106