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