MRPT  1.9.9
CMHPropertiesValuesList.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
13 #include <cstdio>
14 #include <cstring>
15 #include <sstream>
16 
17 namespace mrpt::hmtslam
18 {
19 /** Internal triplet for each property in utils::CMHPropertiesValuesList */
21 {
22  TPropertyValueIDTriplet() = default;
23  std::string name, basic_value;
25  int64_t ID{0};
26 };
27 
28 /** An arbitrary list of "annotations", or named attributes, each being an
29  * instance of any CSerializable object (Multi-hypotheses version).
30  * For each named annotation (or attribute), several values may exist, each
31  * associated to a given hypothesis ID.
32  * A non multi-hypotheses version exists in CPropertiesValuesList.
33  * \sa CSerializable, CPropertiesValuesList
34  * \ingroup mrpt_base_grp
35  */
37 {
39 
40  private:
41  std::vector<TPropertyValueIDTriplet> m_properties;
42 
43  public:
44  /** Default constructor
45  */
47 
48  /** Copy constructor
49  */
51 
52  /** Copy operator
53  */
55 
56  /** Destructor
57  */
58  ~CMHPropertiesValuesList() override;
59 
60  /** Clears the list and frees all object's memory.
61  */
62  void clear();
63 
64  /** Returns the value of the property (case insensitive) for some given
65  * hypothesis ID, or a nullptr smart pointer if it does not exist.
66  */
67  CSerializable::Ptr get(
68  const char* propertyName, const int64_t& hypothesis_ID) const;
69 
70  /** Returns the value of the property (case insensitive) for some given
71  * hypothesis ID checking its class in runtime, or a nullptr smart pointer
72  * if it does not exist.
73  */
74  template <typename T>
75  typename T::Ptr getAs(
76  const char* propertyName, const int64_t& hypothesis_ID,
77  bool allowNullPointer = true) const
78  {
80  CSerializable::Ptr obj = get(propertyName, hypothesis_ID);
81  if (!obj)
82  {
83  if (allowNullPointer)
84  return typename T::Ptr();
85  else
86  THROW_EXCEPTION("Null pointer");
87  }
88  const mrpt::rtti::TRuntimeClassId* class_ID =
89  &T::GetRuntimeClassIdStatic();
90  ASSERT_(class_ID == obj->GetRuntimeClass());
91  return std::dynamic_pointer_cast<T>(obj);
92  MRPT_END
93  }
94 
95  /** Returns the value of the property (case insensitive) for the first
96  * hypothesis ID found, or nullptr if it does not exist.
97  */
98  CSerializable::Ptr getAnyHypothesis(const char* propertyName) const;
99 
100  /** Sets/change the value of the property (case insensitive) for the given
101  * hypothesis ID, making a copy of the object (or setting it to nullptr if
102  * it is the passed value)
103  * \sa setMemoryReference
104  */
105  void set(
106  const char* propertyName, const CSerializable::Ptr& obj,
107  const int64_t& hypothesis_ID);
108 
109  /** Sets/change the value of the property (case insensitive) for the given
110  * hypothesis ID, directly replacing the pointer instead of making a copy of
111  * the object.
112  * \sa set
113  */
114  void setMemoryReference(
115  const char* propertyName, const CSerializable::Ptr& obj,
116  const int64_t& hypothesis_ID);
117 
118  /** Remove a given property, if it exists.
119  */
120  void remove(const char* propertyName, const int64_t& hypothesis_ID);
121 
122  /** Remove all the properties for the given hypothesis.
123  */
124  void removeAll(const int64_t& hypothesis_ID);
125 
126  /** Sets/change the value of a property (case insensitive) for the given
127  * hypothesis ID, from an elemental data type.
128  */
129  template <class T>
131  const char* propertyName, const T& data, const int64_t& hypothesis_ID)
132  {
133  MRPT_START
134 
135  std::string basic_value;
136  basic_value.resize(sizeof(T));
137  std::memcpy(&basic_value[0], &data, sizeof(T));
138 
139  for (auto& m_propertie : m_properties)
140  {
141  if (m_propertie.ID == hypothesis_ID &&
142  mrpt::system::strCmpI(propertyName, m_propertie.name))
143  {
144  // Delete current contents &
145  // Copy new value:
146  m_propertie.basic_value = basic_value;
147  return;
148  }
149  }
150 
151  // Insert as new:
152  TPropertyValueIDTriplet newPair;
153  newPair.name = std::string(propertyName);
154  newPair.basic_value = basic_value;
155  newPair.ID = hypothesis_ID;
156  m_properties.push_back(newPair);
157 
159  printf("Exception while setting annotation '%s'", propertyName););
160  }
161 
162  /** Gets the value of a property (case insensitive) for the given hypothesis
163  * ID, retrieves it as an elemental data type (types must coincide, basic
164  * size check is performed).
165  * \return false if the property does not exist for the given hypothesis.
166  */
167  template <class T>
169  const char* propertyName, T& out_data, const int64_t& hypothesis_ID,
170  bool raiseExceptionIfNotFound = false) const
171  {
172  MRPT_START
173  for (const auto& m_propertie : m_properties)
174  {
175  if (mrpt::system::strCmpI(propertyName, m_propertie.name) &&
176  m_propertie.ID == hypothesis_ID)
177  {
178  if (m_propertie.basic_value.size() != sizeof(out_data))
179  THROW_EXCEPTION("Data sizes do not match.");
180  out_data =
181  *reinterpret_cast<const T*>(&m_propertie.basic_value[0]);
182  return true;
183  }
184  }
185  // Not found:
186  if (raiseExceptionIfNotFound)
187  THROW_EXCEPTION_FMT("Property '%s' not found", propertyName);
188  return false;
189  MRPT_END
190  }
191 
192  /** Returns the name of all properties in the list
193  */
194  std::vector<std::string> getPropertyNames() const;
195 
196  using iterator = std::vector<TPropertyValueIDTriplet>::iterator;
197  using const_iterator = std::vector<TPropertyValueIDTriplet>::const_iterator;
198 
199  iterator begin() { return m_properties.begin(); }
200  const_iterator begin() const { return m_properties.begin(); }
201  iterator end() { return m_properties.end(); }
202  const_iterator end() const { return m_properties.end(); }
203  size_t size() const { return m_properties.size(); }
204 }; // End of class def.
205 } // namespace mrpt::hmtslam
#define MRPT_START
Definition: exceptions.h:241
T::Ptr getAs(const char *propertyName, const int64_t &hypothesis_ID, bool allowNullPointer=true) const
Returns the value of the property (case insensitive) for some given hypothesis ID checking its class ...
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
Classes related to the implementation of Hybrid Metric Topological (HMT) SLAM.
A structure that holds runtime class type information.
Definition: CObject.h:31
#define MRPT_END_WITH_CLEAN_UP(stuff)
Definition: exceptions.h:247
void setElemental(const char *propertyName, const T &data, const int64_t &hypothesis_ID)
Sets/change the value of a property (case insensitive) for the given hypothesis ID, from an elemental data type.
void removeAll(const int64_t &hypothesis_ID)
Remove all the properties for the given hypothesis.
CMHPropertiesValuesList()
Default constructor.
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
CSerializable::Ptr getAnyHypothesis(const char *propertyName) const
Returns the value of the property (case insensitive) for the first hypothesis ID found, or nullptr if it does not exist.
void clear()
Clears the list and frees all object&#39;s memory.
std::vector< TPropertyValueIDTriplet >::const_iterator const_iterator
std::vector< TPropertyValueIDTriplet >::iterator iterator
CMHPropertiesValuesList & operator=(const CMHPropertiesValuesList &o)
Copy operator.
#define MRPT_END
Definition: exceptions.h:245
Internal triplet for each property in utils::CMHPropertiesValuesList.
mrpt::serialization::CSerializable::Ptr value
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
An arbitrary list of "annotations", or named attributes, each being an instance of any CSerializable ...
void setMemoryReference(const char *propertyName, const CSerializable::Ptr &obj, const int64_t &hypothesis_ID)
Sets/change the value of the property (case insensitive) for the given hypothesis ID...
std::vector< std::string > getPropertyNames() const
Returns the name of all properties in the list.
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
bool getElemental(const char *propertyName, T &out_data, const int64_t &hypothesis_ID, bool raiseExceptionIfNotFound=false) const
Gets the value of a property (case insensitive) for the given hypothesis ID, retrieves it as an eleme...
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
bool strCmpI(const std::string &s1, const std::string &s2)
Return true if the two strings are equal (case insensitive)
std::vector< TPropertyValueIDTriplet > m_properties
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
static struct FontData data
Definition: gltext.cpp:144



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020