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-2019, 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 <sstream>
15 
16 namespace mrpt::hmtslam
17 {
18 /** Internal triplet for each property in utils::CMHPropertiesValuesList */
20 {
21  TPropertyValueIDTriplet() = default;
24  int64_t ID{0};
25 };
26 
27 /** An arbitrary list of "annotations", or named attributes, each being an
28  * instance of any CSerializable object (Multi-hypotheses version).
29  * For each named annotation (or attribute), several values may exist, each
30  * associated to a given hypothesis ID.
31  * A non multi-hypotheses version exists in CPropertiesValuesList.
32  * \sa CSerializable, CPropertiesValuesList
33  * \ingroup mrpt_base_grp
34  */
36 {
38 
39  private:
40  std::vector<TPropertyValueIDTriplet> m_properties;
41 
42  public:
43  /** Default constructor
44  */
46 
47  /** Copy constructor
48  */
50 
51  /** Copy operator
52  */
54 
55  /** Destructor
56  */
57  ~CMHPropertiesValuesList() override;
58 
59  /** Clears the list and frees all object's memory.
60  */
61  void clear();
62 
63  /** Returns the value of the property (case insensitive) for some given
64  * hypothesis ID, or a nullptr smart pointer if it does not exist.
65  */
66  CSerializable::Ptr get(
67  const char* propertyName, const int64_t& hypothesis_ID) const;
68 
69  /** Returns the value of the property (case insensitive) for some given
70  * hypothesis ID checking its class in runtime, or a nullptr smart pointer
71  * if it does not exist.
72  */
73  template <typename T>
74  typename T::Ptr getAs(
75  const char* propertyName, const int64_t& hypothesis_ID,
76  bool allowNullPointer = true) const
77  {
79  CSerializable::Ptr obj = get(propertyName, hypothesis_ID);
80  if (!obj)
81  {
82  if (allowNullPointer)
83  return typename T::Ptr();
84  else
85  THROW_EXCEPTION("Null pointer");
86  }
87  const mrpt::rtti::TRuntimeClassId* class_ID =
88  &T::GetRuntimeClassIdStatic();
89  ASSERT_(class_ID == obj->GetRuntimeClass());
90  return std::dynamic_pointer_cast<T>(obj);
91  MRPT_END
92  }
93 
94  /** Returns the value of the property (case insensitive) for the first
95  * hypothesis ID found, or nullptr if it does not exist.
96  */
97  CSerializable::Ptr getAnyHypothesis(const char* propertyName) const;
98 
99  /** Sets/change the value of the property (case insensitive) for the given
100  * hypothesis ID, making a copy of the object (or setting it to nullptr if
101  * it is the passed value)
102  * \sa setMemoryReference
103  */
104  void set(
105  const char* propertyName, const CSerializable::Ptr& obj,
106  const int64_t& hypothesis_ID);
107 
108  /** Sets/change the value of the property (case insensitive) for the given
109  * hypothesis ID, directly replacing the pointer instead of making a copy of
110  * the object.
111  * \sa set
112  */
113  void setMemoryReference(
114  const char* propertyName, const CSerializable::Ptr& obj,
115  const int64_t& hypothesis_ID);
116 
117  /** Remove a given property, if it exists.
118  */
119  void remove(const char* propertyName, const int64_t& hypothesis_ID);
120 
121  /** Remove all the properties for the given hypothesis.
122  */
123  void removeAll(const int64_t& hypothesis_ID);
124 
125  /** Sets/change the value of a property (case insensitive) for the given
126  * hypothesis ID, from an elemental data type.
127  */
128  template <class T>
130  const char* propertyName, const T& data, const int64_t& hypothesis_ID)
131  {
132  MRPT_START
133 
134  std::string basic_value;
135  basic_value.resize(sizeof(T));
136  ::memcpy(&basic_value[0], &data, sizeof(T));
137 
138  for (auto& m_propertie : m_properties)
139  {
140  if (m_propertie.ID == hypothesis_ID &&
141  mrpt::system::strCmpI(propertyName, m_propertie.name))
142  {
143  // Delete current contents &
144  // Copy new value:
145  m_propertie.basic_value = basic_value;
146  return;
147  }
148  }
149 
150  // Insert as new:
151  TPropertyValueIDTriplet newPair;
152  newPair.name = std::string(propertyName);
153  newPair.basic_value = basic_value;
154  newPair.ID = hypothesis_ID;
155  m_properties.push_back(newPair);
156 
158  printf("Exception while setting annotation '%s'", propertyName););
159  }
160 
161  /** Gets the value of a property (case insensitive) for the given hypothesis
162  * ID, retrieves it as an elemental data type (types must coincide, basic
163  * size check is performed).
164  * \return false if the property does not exist for the given hypothesis.
165  */
166  template <class T>
168  const char* propertyName, T& out_data, const int64_t& hypothesis_ID,
169  bool raiseExceptionIfNotFound = false) const
170  {
171  MRPT_START
172  for (const auto& m_propertie : m_properties)
173  {
174  if (mrpt::system::strCmpI(propertyName, m_propertie.name) &&
175  m_propertie.ID == hypothesis_ID)
176  {
177  if (m_propertie.basic_value.size() != sizeof(out_data))
178  THROW_EXCEPTION("Data sizes do not match.");
179  out_data =
180  *reinterpret_cast<const T*>(&m_propertie.basic_value[0]);
181  return true;
182  }
183  }
184  // Not found:
185  if (raiseExceptionIfNotFound)
186  THROW_EXCEPTION_FMT("Property '%s' not found", propertyName);
187  return false;
188  MRPT_END
189  }
190 
191  /** Returns the name of all properties in the list
192  */
193  std::vector<std::string> getPropertyNames() const;
194 
195  using iterator = std::vector<TPropertyValueIDTriplet>::iterator;
196  using const_iterator = std::vector<TPropertyValueIDTriplet>::const_iterator;
197 
198  iterator begin() { return m_properties.begin(); }
199  const_iterator begin() const { return m_properties.begin(); }
200  iterator end() { return m_properties.end(); }
201  const_iterator end() const { return m_properties.end(); }
202  size_t size() const { return m_properties.size(); }
203 }; // End of class def.
204 } // 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
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
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
__int64 int64_t
Definition: rptypes.h:52
std::vector< TPropertyValueIDTriplet >::iterator iterator
GLsizei const GLchar ** string
Definition: glext.h:4116
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
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.
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)
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3550
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".
Definition: os.cpp:358



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019