MRPT  1.9.9
CMHPropertiesValuesList.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef CMHPropertiesValuesList_H
10 #define CMHPropertiesValuesList_H
11 
14 #include <cstdio>
15 #include <sstream>
16 
17 namespace mrpt::hmtslam
18 {
19 /** Internal triplet for each property in utils::CMHPropertiesValuesList */
21 {
22  TPropertyValueIDTriplet() : name(), value(nullptr), 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  virtual ~CMHPropertiesValuesList();
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  ::memcpy(&basic_value[0], &data, sizeof(T));
138 
139  for (auto it = m_properties.begin(); it != m_properties.end(); ++it)
140  {
141  if (it->ID == hypothesis_ID &&
142  mrpt::system::strCmpI(propertyName, it->name))
143  {
144  // Delete current contents &
145  // Copy new value:
146  it->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 (auto it = m_properties.begin(); it != m_properties.end(); ++it)
174  {
175  if (mrpt::system::strCmpI(propertyName, it->name) &&
176  it->ID == hypothesis_ID)
177  {
178  if (it->basic_value.size() != sizeof(out_data))
179  THROW_EXCEPTION("Data sizes do not match.");
180  out_data = *reinterpret_cast<const T*>(&it->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 
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 }
205 #endif
206 
207 
Scalar * iterator
Definition: eigen_plugins.h:26
#define MRPT_START
Definition: exceptions.h:262
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:41
Classes related to the implementation of Hybrid Metric Topological (HMT) SLAM.
A structure that holds runtime class type information.
Definition: CObject.h:30
#define MRPT_END_WITH_CLEAN_UP(stuff)
Definition: exceptions.h:268
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
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.
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
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:49
std::vector< TPropertyValueIDTriplet >::iterator iterator
GLsizei const GLchar ** string
Definition: glext.h:4101
#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:266
Internal triplet for each property in utils::CMHPropertiesValuesList.
mrpt::serialization::CSerializable::Ptr value
GLuint const GLchar * name
Definition: glext.h:4054
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.
GLsizei const GLfloat * value
Definition: glext.h:4117
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:43
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:3546
const Scalar * const_iterator
Definition: eigen_plugins.h:27
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:356



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020