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
mrpt::hmtslam::TPropertyValueIDTriplet::basic_value
std::string basic_value
Definition: CMHPropertiesValuesList.h:25
const_iterator
const Scalar * const_iterator
Definition: eigen_plugins.h:27
mrpt::hmtslam::CMHPropertiesValuesList::begin
const_iterator begin() const
Definition: CMHPropertiesValuesList.h:201
mrpt::rtti::TRuntimeClassId
A structure that holds runtime class type information.
Definition: CObject.h:30
mrpt::serialization::CSerializable::Ptr
std::shared_ptr< CSerializable > Ptr
Definition: CSerializable.h:37
mrpt::hmtslam::CMHPropertiesValuesList::getElemental
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...
Definition: CMHPropertiesValuesList.h:170
string_utils.h
mrpt::hmtslam::CMHPropertiesValuesList::getAs
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 ...
Definition: CMHPropertiesValuesList.h:77
mrpt::hmtslam::CMHPropertiesValuesList::end
iterator end()
Definition: CMHPropertiesValuesList.h:202
mrpt::hmtslam::CMHPropertiesValuesList::setMemoryReference
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,...
Definition: CMHPropertiesValuesList.cpp:182
MRPT_END_WITH_CLEAN_UP
#define MRPT_END_WITH_CLEAN_UP(stuff)
Definition: exceptions.h:268
mrpt::hmtslam::TPropertyValueIDTriplet::TPropertyValueIDTriplet
TPropertyValueIDTriplet()
Definition: CMHPropertiesValuesList.h:24
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
mrpt::hmtslam::CMHPropertiesValuesList::~CMHPropertiesValuesList
virtual ~CMHPropertiesValuesList()
Destructor.
Definition: CMHPropertiesValuesList.cpp:95
int64_t
__int64 int64_t
Definition: rptypes.h:49
mrpt::hmtslam::CMHPropertiesValuesList::removeAll
void removeAll(const int64_t &hypothesis_ID)
Remove all the properties for the given hypothesis.
Definition: CMHPropertiesValuesList.cpp:258
mrpt::hmtslam::CMHPropertiesValuesList::remove
void remove(const char *propertyName, const int64_t &hypothesis_ID)
Remove a given property, if it exists.
Definition: CMHPropertiesValuesList.cpp:242
mrpt::hmtslam::TPropertyValueIDTriplet::ID
int64_t ID
Definition: CMHPropertiesValuesList.h:27
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
mrpt::hmtslam::CMHPropertiesValuesList::clear
void clear()
Clears the list and frees all object's memory.
Definition: CMHPropertiesValuesList.cpp:99
mrpt::hmtslam::TPropertyValueIDTriplet::value
mrpt::serialization::CSerializable::Ptr value
Definition: CMHPropertiesValuesList.h:26
name
GLuint const GLchar * name
Definition: glext.h:4054
mrpt::hmtslam::CMHPropertiesValuesList::getAnyHypothesis
CSerializable::Ptr getAnyHypothesis(const char *propertyName) const
Returns the value of the property (case insensitive) for the first hypothesis ID found,...
Definition: CMHPropertiesValuesList.cpp:129
mrpt::hmtslam::TPropertyValueIDTriplet::name
std::string name
Definition: CMHPropertiesValuesList.h:25
data
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3547
mrpt::hmtslam::CMHPropertiesValuesList::getPropertyNames
std::vector< std::string > getPropertyNames() const
Returns the name of all properties in the list.
Definition: CMHPropertiesValuesList.cpp:215
mrpt::hmtslam::CMHPropertiesValuesList::end
const_iterator end() const
Definition: CMHPropertiesValuesList.h:203
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
mrpt::hmtslam::CMHPropertiesValuesList::begin
iterator begin()
Definition: CMHPropertiesValuesList.h:200
mrpt::hmtslam::CMHPropertiesValuesList::operator=
CMHPropertiesValuesList & operator=(const CMHPropertiesValuesList &o)
Copy operator.
Definition: CMHPropertiesValuesList.cpp:285
mrpt::serialization::CSerializable
The virtual base class which provides a unified interface for all persistent objects in MRPT.
Definition: CSerializable.h:32
mrpt::hmtslam::CMHPropertiesValuesList
An arbitrary list of "annotations", or named attributes, each being an instance of any CSerializable ...
Definition: CMHPropertiesValuesList.h:38
mrpt::hmtslam::CMHPropertiesValuesList::setElemental
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,...
Definition: CMHPropertiesValuesList.h:132
mrpt::hmtslam::CMHPropertiesValuesList::get
CSerializable::Ptr get(const char *propertyName, const int64_t &hypothesis_ID) const
Returns the value of the property (case insensitive) for some given hypothesis ID,...
Definition: CMHPropertiesValuesList.cpp:109
mrpt::hmtslam::CMHPropertiesValuesList::iterator
std::vector< TPropertyValueIDTriplet >::iterator iterator
Definition: CMHPropertiesValuesList.h:197
mrpt::hmtslam::CMHPropertiesValuesList::size
size_t size() const
Definition: CMHPropertiesValuesList.h:204
mrpt::hmtslam::CMHPropertiesValuesList::CMHPropertiesValuesList
CMHPropertiesValuesList()
Default constructor.
Definition: CMHPropertiesValuesList.cpp:91
DEFINE_SERIALIZABLE
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
Definition: CSerializable.h:102
mrpt::hmtslam::TPropertyValueIDTriplet
Internal triplet for each property in utils::CMHPropertiesValuesList.
Definition: CMHPropertiesValuesList.h:22
value
GLsizei const GLfloat * value
Definition: glext.h:4117
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
mrpt::hmtslam::CMHPropertiesValuesList::set
void set(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,...
Definition: CMHPropertiesValuesList.cpp:145
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::hmtslam::CMHPropertiesValuesList::const_iterator
std::vector< TPropertyValueIDTriplet >::const_iterator const_iterator
Definition: CMHPropertiesValuesList.h:198
iterator
Scalar * iterator
Definition: eigen_plugins.h:26
mrpt::hmtslam::CMHPropertiesValuesList::m_properties
std::vector< TPropertyValueIDTriplet > m_properties
Definition: CMHPropertiesValuesList.h:43
CSerializable.h
mrpt::system::strCmpI
bool strCmpI(const std::string &s1, const std::string &s2)
Return true if the two strings are equal (case insensitive)
Definition: string_utils.cpp:305
mrpt::system::os::memcpy
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.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST