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