MemoryID.h
Go to the documentation of this file.
1#pragma once
2
3#include <functional> // for std::hash
4#include <string>
5#include <vector>
6
7#include "Time.h"
8
9namespace armarx::armem
10{
11
12 /**
13 * @brief A memory ID.
14 *
15 * A memory ID is an index into the hierarchical memory structure.
16 * It specifies the keys for the different levels, starting from the
17 * memory name and ending at the instance index.
18 *
19 * A memory ID need not be complete, e.g. it may specify only the memory
20 * and core segment names (thus representing a core segment ID).
21 * A memory ID that fully identifies a level starting from the memory is
22 * called well-defined.
23 * @see `isWellDefined()`
24 *
25 * Memory IDs can be encoded in strings using a delimiter:
26 * - Structure: "MemoryName/CoreSegmentName/ProviderSegmentName/EntityName/Timestamp/InstanceIndex"
27 * - Example: "Vision/RGBImages/Primesense/image/1245321323/0"
28 * @see `str()`
29 *
30 * If an ID does not specify the lower levels, these parts can be omitted.
31 * Thus, an entity ID could look like:
32 * - Structure: "MemoryName/CoreSegmentName/ProviderSegmentName/EntityName"
33 * - Example: "Vision/RGBImages/Primesense/image"
34 *
35 * If a name contains a "/", it will be escaped:
36 * - Example: "Vision/RGBImages/Primesense/my\/entity\/with\/slashes"
37 *
38 * Memory IDs may be not well-defined. This can occur e.g. when preparing
39 * an entity instance ID which is still pending the timestamp.
40 * It could look like (note the missing timestamp):
41 * - Example: "Vision/RGBImages/Primesense/image//0"
42 *
43 * These IDs are still valid and can be handled (encoded as string etc.).
44 * However, some operations may not be well-defined for non-well-defined IDs
45 * (such as `contains()`).
46 */
48 {
49 public:
50 std::string memoryName = "";
51 std::string coreSegmentName = "";
52 std::string providerSegmentName = "";
53 std::string entityName = "";
55 int instanceIndex = -1;
56
57
58 public:
59 /// Construct a default (empty) memory ID.
60 MemoryID();
61 /// (Re-)Construct a memory ID from a string representation as returned by `str()`.
62 explicit MemoryID(const std::string& string);
63
64 MemoryID(const std::string& memoryName,
65 const std::string& coreSegmentName,
66 const std::string& providerSegmentName = "",
67 const std::string& entityName = "",
69 int instanceIndex = -1);
70
71
72 /// Alias for constructor from string.
73 static MemoryID fromString(const std::string& string);
74
75 /// Constructor memory ID from items as returned by getItems().
76 static MemoryID fromItems(const std::vector<std::string>& items);
77
78
79 /**
80 * @brief Indicate whether this ID is well-defined.
81 *
82 * A well-defined ID has no specified level after a non-specified level (i.e., no gaps).
83 *
84 * Well-defined examples:
85 * - "" (empty, but well-defined)
86 * - "Memory" (a memory ID)
87 * - "Memory/Core" (a core segment ID)
88 * - "Memory/Core/Provider" (a provider segment ID)
89 *
90 * Non-well-defined examples:
91 * - "Memory//Provider" (no core segment name)
92 * - "/Core" (no memory name)
93 * - "Mem/Core/Prov/entity//0" (no timestamp)
94 * - "///entity//0" (no memory, core segment and provider segment names)
95 *
96 * @return True if `*this` is a well-defined memory ID.
97 */
98 bool isWellDefined() const;
99
100 // Checks whether a specific level is specified.
101
102 bool
104 {
105 return not memoryName.empty();
106 }
107
108 bool
110 {
111 return not coreSegmentName.empty();
112 }
113
114 bool
116 {
117 return not providerSegmentName.empty();
118 }
119
120 bool
122 {
123 return not entityName.empty();
124 }
125
126 bool
128 {
129 return timestamp.isValid();
130 }
131
132 void
134 {
136 }
137
138 bool
140 {
141 return instanceIndex >= 0;
142 }
143
144 void
146 {
147 instanceIndex = -1;
148 }
149
150 // Slice getters: Get upper part of the ID.
151
152 MemoryID getMemoryID() const;
155 MemoryID getEntityID() const;
158
159 // Slice getter: remove the last set name
160 MemoryID removeLeafItem() const;
161
162
163 // Slice setters: Set part of the ID.
164
165 void setMemoryID(const MemoryID& id);
166 void setCoreSegmentID(const MemoryID& id);
167 void setProviderSegmentID(const MemoryID& id);
168 void setEntityID(const MemoryID& id);
169 void setEntitySnapshotID(const MemoryID& id);
170 void setEntityInstanceID(const MemoryID& id);
171
172 // Return a modified copy.
173
174 MemoryID withMemoryName(const std::string& name) const;
175 MemoryID withCoreSegmentName(const std::string& name) const;
176 MemoryID withProviderSegmentName(const std::string& name) const;
177 MemoryID withEntityName(const std::string& name) const;
178 MemoryID withTimestamp(Time time) const;
180
181
182 // String conversion
183
184 /**
185 * @brief Get a string representation of this memory ID.
186 *
187 * Items are separated by a delimiter. If `escapeDelimiter` is true,
188 * delimiters occuring inside names are escaped with backward slashes.
189 * This allows to reconstruct the memory ID from the result of `str()`
190 * in these cases.
191 *
192 * @param escapeDelimiter If true, escape delimiters inside names
193 * @return A string representation of this MemoryID.
194 */
195 std::string str(bool escapeDelimiters = true) const;
196
197 /// Get the timestamp as string.
198 std::string timestampStr() const;
199 /// Get the instance index as string.
200 std::string instanceIndexStr() const;
201
202 /// Reconstruct a timestamp from a string as returned by `timestampStr()`.
203 static Time timestampFromStr(const std::string& timestamp);
204 /// Reconstruct an instance index from a string as returned by `instanceIndexStr()`.
205 static int instanceIndexFromStr(const std::string& index);
206
207
208 /// Get all levels as strings.
209 std::vector<std::string> getAllItems(bool escapeDelimiters = false) const;
210 /// Get the levels from root to first not defined level (excluding).
211 std::vector<std::string> getItems(bool escapeDelimiters = false) const;
212
213
214 // Other utility.
215
216 /// Indicate whether this ID has a gap such as in 'Memory//MyProvider' (no core segment name).
217 bool hasGap() const;
218 /// Get the lowest defined level (or empty string if there is none).
219 std::string getLeafItem() const;
220
221 MemoryID cleanID() const;
222
223
224 // Operators
225
226 bool operator==(const MemoryID& other) const;
227
228 inline bool
229 operator!=(const MemoryID& other) const
230 {
231 return not(*this == other);
232 }
233
234 bool operator<(const MemoryID& rhs) const;
235
236 inline bool
237 operator>(const MemoryID& rhs) const
238 {
239 return rhs < (*this);
240 }
241
242 inline bool
243 operator<=(const MemoryID& rhs) const
244 {
245 return not operator>(rhs);
246 }
247
248 inline bool
249 operator>=(const MemoryID& rhs) const
250 {
251 return not operator<(rhs);
252 }
253
254 friend std::ostream& operator<<(std::ostream& os, const MemoryID id);
255
256
257 private:
258 static long long parseInteger(const std::string& string, const std::string& semanticName);
259 static std::string escapeDelimiter(const std::string& name);
260 static std::string escape(const std::string& name, bool escapeDelimiters);
261
262 static const std::string delimiter;
263
264
265 // Do not allow specifying the delimiter from outside.
266 std::string str(const std::string& delimiter, bool escapeDelimiter) const;
267 };
268
269 /**
270 * @brief Indicates whether `general` is "less specific" than, or equal to, `specific`,
271 * i.e. `general` "contains" `specific`.
272 *
273 * A memory ID A is said to be less specific than B, if B the same values as A
274 * for all levels specified in A, but potentially also specifies the lower levels.
275 *
276 * Examples:
277 * - "" contains ""
278 * - "m" contains "m" and "m/c", but not "n" and "n/c"
279 * - "m/c" contains "m/c" and "m/c/p", but not "m/d" and "m/c/q"
280 *
281 * If a memory ID has a gap (`@see MemoryID::hasGap()`), such as "m//p",
282 * the levels after the gap are ignored.
283 * - "m//p" contains "m", "m/c" and "m/c/p".
284 *
285 * @param general The less specific memory ID
286 * @param specific The more specific memory ID
287 * @return True if `general` is less specific than `specific`.
288 */
289 bool contains(const MemoryID& general, const MemoryID& specific);
290
291} // namespace armarx::armem
292
293namespace std
294{
295
296 template <>
298 {
299 std::size_t
301 {
302 const std::string sid = id.str();
303 return std::hash<string>()(sid);
304 }
305 };
306
307} // namespace std
std::string timestamp()
MemoryID getEntitySnapshotID() const
Definition MemoryID.cpp:318
static int instanceIndexFromStr(const std::string &index)
Reconstruct an instance index from a string as returned by instanceIndexStr().
Definition MemoryID.cpp:467
std::string coreSegmentName
Definition MemoryID.h:51
bool operator>=(const MemoryID &rhs) const
Definition MemoryID.h:249
MemoryID withProviderSegmentName(const std::string &name) const
Definition MemoryID.cpp:417
void setProviderSegmentID(const MemoryID &id)
Definition MemoryID.cpp:373
static MemoryID fromItems(const std::vector< std::string > &items)
Constructor memory ID from items as returned by getItems().
Definition MemoryID.cpp:194
bool operator<=(const MemoryID &rhs) const
Definition MemoryID.h:243
MemoryID getProviderSegmentID() const
Definition MemoryID.cpp:302
MemoryID withCoreSegmentName(const std::string &name) const
Definition MemoryID.cpp:409
std::vector< std::string > getAllItems(bool escapeDelimiters=false) const
Get all levels as strings.
Definition MemoryID.cpp:273
static Time timestampFromStr(const std::string &timestamp)
Reconstruct a timestamp from a string as returned by timestampStr().
Definition MemoryID.cpp:461
MemoryID withMemoryName(const std::string &name) const
Definition MemoryID.cpp:401
bool operator<(const MemoryID &rhs) const
Definition MemoryID.cpp:482
MemoryID getMemoryID() const
Definition MemoryID.cpp:286
bool hasProviderSegmentName() const
Definition MemoryID.h:115
void setMemoryID(const MemoryID &id)
Definition MemoryID.cpp:360
bool hasGap() const
Indicate whether this ID has a gap such as in 'Memory//MyProvider' (no core segment name).
Definition MemoryID.cpp:163
MemoryID cleanID() const
Definition MemoryID.cpp:133
std::string getLeafItem() const
Get the lowest defined level (or empty string if there is none).
Definition MemoryID.cpp:119
void setEntitySnapshotID(const MemoryID &id)
Definition MemoryID.cpp:387
bool isWellDefined() const
Indicate whether this ID is well-defined.
Definition MemoryID.cpp:182
MemoryID getCoreSegmentID() const
Definition MemoryID.cpp:294
bool hasEntityName() const
Definition MemoryID.h:121
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition MemoryID.cpp:102
bool operator>(const MemoryID &rhs) const
Definition MemoryID.h:237
bool hasInstanceIndex() const
Definition MemoryID.h:139
bool hasMemoryName() const
Definition MemoryID.h:103
MemoryID removeLeafItem() const
Definition MemoryID.cpp:334
MemoryID getEntityInstanceID() const
Definition MemoryID.cpp:326
void setCoreSegmentID(const MemoryID &id)
Definition MemoryID.cpp:366
MemoryID withEntityName(const std::string &name) const
Definition MemoryID.cpp:425
bool hasCoreSegmentName() const
Definition MemoryID.h:109
bool operator!=(const MemoryID &other) const
Definition MemoryID.h:229
std::string entityName
Definition MemoryID.h:53
bool operator==(const MemoryID &other) const
Definition MemoryID.cpp:473
MemoryID withTimestamp(Time time) const
Definition MemoryID.cpp:433
friend std::ostream & operator<<(std::ostream &os, const MemoryID id)
Definition MemoryID.cpp:557
bool hasTimestamp() const
Definition MemoryID.h:127
void setEntityInstanceID(const MemoryID &id)
Definition MemoryID.cpp:394
std::vector< std::string > getItems(bool escapeDelimiters=false) const
Get the levels from root to first not defined level (excluding).
Definition MemoryID.cpp:233
std::string memoryName
Definition MemoryID.h:50
std::string instanceIndexStr() const
Get the instance index as string.
Definition MemoryID.cpp:455
std::string timestampStr() const
Get the timestamp as string.
Definition MemoryID.cpp:449
MemoryID withInstanceIndex(int index) const
Definition MemoryID.cpp:441
static MemoryID fromString(const std::string &string)
Alias for constructor from string.
Definition MemoryID.cpp:188
std::string providerSegmentName
Definition MemoryID.h:52
MemoryID()
Construct a default (empty) memory ID.
Definition MemoryID.cpp:20
void setEntityID(const MemoryID &id)
Definition MemoryID.cpp:380
MemoryID getEntityID() const
Definition MemoryID.cpp:310
static DateTime Invalid()
Definition DateTime.cpp:57
bool contains(const MemoryID &general, const MemoryID &specific)
Indicates whether general is "less specific" than, or equal to, specific, i.e.
Definition MemoryID.cpp:563
armarx::core::time::DateTime Time
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::size_t operator()(const armarx::armem::MemoryID &id) const
Definition MemoryID.h:300