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 
9 namespace 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  */
47  class MemoryID
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
127  hasTimestamp() const
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;
153  MemoryID getCoreSegmentID() 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;
179  MemoryID withInstanceIndex(int index) 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 
293 namespace std
294 {
295 
296  template <>
297  struct hash<armarx::armem::MemoryID>
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
armarx::armem::MemoryID::isWellDefined
bool isWellDefined() const
Indicate whether this ID is well-defined.
Definition: MemoryID.cpp:177
armarx::armem::MemoryID::timestamp
Time timestamp
Definition: MemoryID.h:54
armarx::armem::MemoryID::setCoreSegmentID
void setCoreSegmentID(const MemoryID &id)
Definition: MemoryID.cpp:361
armarx::armem::MemoryID::getEntityInstanceID
MemoryID getEntityInstanceID() const
Definition: MemoryID.cpp:321
armarx::core::time::DateTime::isValid
bool isValid() const
Definition: DateTime.cpp:135
armarx::armem::MemoryID::timestampFromStr
static Time timestampFromStr(const std::string &timestamp)
Reconstruct a timestamp from a string as returned by timestampStr().
Definition: MemoryID.cpp:456
armarx::armem::MemoryID::operator>
bool operator>(const MemoryID &rhs) const
Definition: MemoryID.h:237
armarx::armem::MemoryID::operator<=
bool operator<=(const MemoryID &rhs) const
Definition: MemoryID.h:243
armarx::armem::MemoryID::providerSegmentName
std::string providerSegmentName
Definition: MemoryID.h:52
armarx::armem::MemoryID::removeLeafItem
MemoryID removeLeafItem() const
Definition: MemoryID.cpp:329
armarx::armem::MemoryID::withMemoryName
MemoryID withMemoryName(const std::string &name) const
Definition: MemoryID.cpp:396
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::armem::MemoryID::operator!=
bool operator!=(const MemoryID &other) const
Definition: MemoryID.h:229
armarx::armem::MemoryID::str
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition: MemoryID.cpp:102
armarx::armem
Definition: LegacyRobotStateMemoryAdapter.cpp:31
armarx::armem::MemoryID::instanceIndex
int instanceIndex
Definition: MemoryID.h:55
armarx::armem::contains
bool contains(const MemoryID &general, const MemoryID &specific)
Indicates whether general is "less specific" than, or equal to, specific, i.e.
Definition: MemoryID.cpp:558
armarx::armem::MemoryID::operator==
bool operator==(const MemoryID &other) const
Definition: MemoryID.cpp:468
armarx::armem::MemoryID::coreSegmentName
std::string coreSegmentName
Definition: MemoryID.h:51
armarx::armem::MemoryID::withProviderSegmentName
MemoryID withProviderSegmentName(const std::string &name) const
Definition: MemoryID.cpp:412
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::MemoryID::withCoreSegmentName
MemoryID withCoreSegmentName(const std::string &name) const
Definition: MemoryID.cpp:404
armarx::armem::MemoryID::operator<
bool operator<(const MemoryID &rhs) const
Definition: MemoryID.cpp:477
armarx::armem::MemoryID::getEntitySnapshotID
MemoryID getEntitySnapshotID() const
Definition: MemoryID.cpp:313
armarx::armem::MemoryID::setProviderSegmentID
void setProviderSegmentID(const MemoryID &id)
Definition: MemoryID.cpp:368
armarx::armem::MemoryID::fromItems
static MemoryID fromItems(const std::vector< std::string > &items)
Constructor memory ID from items as returned by getItems().
Definition: MemoryID.cpp:189
armarx::armem::MemoryID::MemoryID
MemoryID()
Construct a default (empty) memory ID.
Definition: MemoryID.cpp:20
armarx::armem::MemoryID::hasTimestamp
bool hasTimestamp() const
Definition: MemoryID.h:127
armarx::armem::MemoryID::setEntitySnapshotID
void setEntitySnapshotID(const MemoryID &id)
Definition: MemoryID.cpp:382
armarx::armem::MemoryID::hasInstanceIndex
bool hasInstanceIndex() const
Definition: MemoryID.h:139
armarx::armem::MemoryID::getProviderSegmentID
MemoryID getProviderSegmentID() const
Definition: MemoryID.cpp:297
armarx::armem::MemoryID::hasGap
bool hasGap() const
Indicate whether this ID has a gap such as in 'Memory//MyProvider' (no core segment name).
Definition: MemoryID.cpp:158
armarx::armem::MemoryID::operator<<
friend std::ostream & operator<<(std::ostream &os, const MemoryID id)
Definition: MemoryID.cpp:552
armarx::armem::MemoryID::getAllItems
std::vector< std::string > getAllItems(bool escapeDelimiters=false) const
Get all levels as strings.
Definition: MemoryID.cpp:268
armarx::armem::MemoryID::operator>=
bool operator>=(const MemoryID &rhs) const
Definition: MemoryID.h:249
armarx::armem::MemoryID::entityName
std::string entityName
Definition: MemoryID.h:53
armarx::armem::MemoryID::setMemoryID
void setMemoryID(const MemoryID &id)
Definition: MemoryID.cpp:355
armarx::armem::MemoryID::hasEntityName
bool hasEntityName() const
Definition: MemoryID.h:121
armarx::armem::MemoryID::memoryName
std::string memoryName
Definition: MemoryID.h:50
armarx::armem::MemoryID::fromString
static MemoryID fromString(const std::string &string)
Alias for constructor from string.
Definition: MemoryID.cpp:183
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::armem::MemoryID::clearTimestamp
void clearTimestamp()
Definition: MemoryID.h:133
armarx::armem::MemoryID::getCoreSegmentID
MemoryID getCoreSegmentID() const
Definition: MemoryID.cpp:289
armarx::armem::MemoryID::getEntityID
MemoryID getEntityID() const
Definition: MemoryID.cpp:305
armarx::armem::MemoryID::setEntityInstanceID
void setEntityInstanceID(const MemoryID &id)
Definition: MemoryID.cpp:389
armarx::armem::MemoryID::clearInstanceIndex
void clearInstanceIndex()
Definition: MemoryID.h:145
armarx::armem::MemoryID::withEntityName
MemoryID withEntityName(const std::string &name) const
Definition: MemoryID.cpp:420
armarx::armem::MemoryID::cleanID
MemoryID cleanID() const
Definition: MemoryID.cpp:132
armarx::armem::MemoryID::getMemoryID
MemoryID getMemoryID() const
Definition: MemoryID.cpp:281
std
Definition: Application.h:66
armarx::armem::MemoryID::hasProviderSegmentName
bool hasProviderSegmentName() const
Definition: MemoryID.h:115
armarx::armem::MemoryID::hasCoreSegmentName
bool hasCoreSegmentName() const
Definition: MemoryID.h:109
armarx::armem::MemoryID::hasMemoryName
bool hasMemoryName() const
Definition: MemoryID.h:103
Time.h
armarx::armem::MemoryID::withTimestamp
MemoryID withTimestamp(Time time) const
Definition: MemoryID.cpp:428
armarx::armem::MemoryID::getItems
std::vector< std::string > getItems(bool escapeDelimiters=false) const
Get the levels from root to first not defined level (excluding).
Definition: MemoryID.cpp:228
armarx::armem::MemoryID::withInstanceIndex
MemoryID withInstanceIndex(int index) const
Definition: MemoryID.cpp:436
armarx::armem::MemoryID::setEntityID
void setEntityID(const MemoryID &id)
Definition: MemoryID.cpp:375
std::hash< armarx::armem::MemoryID >::operator()
std::size_t operator()(const armarx::armem::MemoryID &id) const
Definition: MemoryID.h:300
armarx::armem::MemoryID::getLeafItem
std::string getLeafItem() const
Get the lowest defined level (or empty string if there is none).
Definition: MemoryID.cpp:119
armarx::core::time::DateTime::Invalid
static DateTime Invalid()
Definition: DateTime.cpp:60
armarx::armem::MemoryID::instanceIndexStr
std::string instanceIndexStr() const
Get the instance index as string.
Definition: MemoryID.cpp:450
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::armem::MemoryID::timestampStr
std::string timestampStr() const
Get the timestamp as string.
Definition: MemoryID.cpp:444
armarx::armem::MemoryID::instanceIndexFromStr
static int instanceIndexFromStr(const std::string &index)
Reconstruct an instance index from a string as returned by instanceIndexStr().
Definition: MemoryID.cpp:462
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29