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-2017, 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 
15 #include <cstdio>
16 
17 namespace mrpt
18 {
19 namespace utils
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  */
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::utils::TRuntimeClassId* class_ID = &T::GetRuntimeClassIdStatic();
91  ASSERT_(class_ID == obj->GetRuntimeClass());
92  return std::dynamic_pointer_cast<T>(obj);
93  MRPT_END
94  }
95 
96  /** Returns the value of the property (case insensitive) for the first
97  * hypothesis ID found, or nullptr if it does not exist.
98  */
99  CSerializable::Ptr getAnyHypothesis(const char* propertyName) const;
100 
101  /** Sets/change the value of the property (case insensitive) for the given
102  * hypothesis ID, making a copy of the object (or setting it to nullptr if
103  * it is the passed value)
104  * \sa setMemoryReference
105  */
106  void set(
107  const char* propertyName, const CSerializable::Ptr& obj,
108  const int64_t& hypothesis_ID);
109 
110  /** Sets/change the value of the property (case insensitive) for the given
111  * hypothesis ID, directly replacing the pointer instead of making a copy of
112  * the object.
113  * \sa set
114  */
115  void setMemoryReference(
116  const char* propertyName, const CSerializable::Ptr& obj,
117  const int64_t& hypothesis_ID);
118 
119  /** Remove a given property, if it exists.
120  */
121  void remove(const char* propertyName, const int64_t& hypothesis_ID);
122 
123  /** Remove all the properties for the given hypothesis.
124  */
125  void removeAll(const int64_t& hypothesis_ID);
126 
127  /** Sets/change the value of a property (case insensitive) for the given
128  * hypothesis ID, from an elemental data type.
129  */
130  template <class T>
132  const char* propertyName, const T& data, const int64_t& hypothesis_ID)
133  {
134  MRPT_START
135 
136  CMemoryChunk::Ptr memChunk = mrpt::make_aligned_shared<CMemoryChunk>();
137  memChunk->setAllocBlockSize(10);
138  (*memChunk) << data;
139 
141  m_properties.begin();
142  it != m_properties.end(); ++it)
143  {
144  if (it->ID == hypothesis_ID &&
145  mrpt::system::strCmpI(propertyName, it->name))
146  {
147  // Delete current contents &
148  // Copy new value:
149  it->value = memChunk;
150  return;
151  }
152  }
153 
154  // Insert as new:
155  TPropertyValueIDTriplet newPair;
156  newPair.name = std::string(propertyName);
157  newPair.value = memChunk;
158  newPair.ID = hypothesis_ID;
159  m_properties.push_back(newPair);
160 
162  printf("Exception while setting annotation '%s'", propertyName););
163  }
164 
165  /** Gets the value of a property (case insensitive) for the given hypothesis
166  * ID, retrieves it as an elemental data type (types must coincide, basic
167  * size check is performed).
168  * \return false if the property does not exist for the given hypothesis.
169  */
170  template <class T>
172  const char* propertyName, T& out_data, const int64_t& hypothesis_ID,
173  bool raiseExceptionIfNotFound = false) const
174  {
175  MRPT_START
177  m_properties.begin();
178  it != m_properties.end(); ++it)
179  {
180  if (mrpt::system::strCmpI(propertyName, it->name) &&
181  it->ID == hypothesis_ID)
182  {
183  CMemoryChunk::Ptr memChunk =
184  std::dynamic_pointer_cast<CMemoryChunk>(it->value);
185  ASSERT_(memChunk)
186  if (memChunk->getTotalBytesCount() != sizeof(out_data))
187  THROW_EXCEPTION("Data sizes do not match.");
188  out_data = *static_cast<T*>(memChunk->getRawBufferData());
189  return true;
190  }
191  }
192  // Not found:
193  if (raiseExceptionIfNotFound)
194  THROW_EXCEPTION_FMT("Property '%s' not found", propertyName);
195  return false;
196  MRPT_END
197  }
198 
199  /** Returns the name of all properties in the list
200  */
201  std::vector<std::string> getPropertyNames() const;
202 
205 
206  iterator begin() { return m_properties.begin(); }
207  const_iterator begin() const { return m_properties.begin(); }
208  iterator end() { return m_properties.end(); }
209  const_iterator end() const { return m_properties.end(); }
210  size_t size() const { return m_properties.size(); }
211 }; // End of class def.
212 } // End of namespace
213 } // end of namespace
214 #endif
An arbitrary list of "annotations", or named attributes, each being an instance of any CSerializable ...
std::vector< TPropertyValueIDTriplet >::const_iterator const_iterator
#define MRPT_END_WITH_CLEAN_UP(stuff)
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Scalar * iterator
Definition: eigen_plugins.h:26
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.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
Internal triplet for each property in utils::CMHPropertiesValuesList.
#define MRPT_END
CMHPropertiesValuesList & operator=(const CMHPropertiesValuesList &o)
Copy operator.
__int64 int64_t
Definition: rptypes.h:49
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 ...
A memory buffer (implements CStream) which can be itself serialized.
Definition: CMemoryChunk.h:24
void removeAll(const int64_t &hypothesis_ID)
Remove all the properties for the given hypothesis.
GLsizei const GLchar ** string
Definition: glext.h:4101
std::shared_ptr< CSerializable > Ptr
Definition: CSerializable.h:47
#define MRPT_START
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...
std::vector< TPropertyValueIDTriplet >::iterator iterator
GLuint const GLchar * name
Definition: glext.h:4054
void clear()
Clears the list and frees all object&#39;s memory.
#define ASSERT_(f)
std::vector< std::string > getPropertyNames() const
Returns the name of all properties in the list.
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...
GLsizei const GLfloat * value
Definition: glext.h:4117
A structure that holds runtime class type information.
Definition: CObject.h:31
std::shared_ptr< CMemoryChunk > Ptr
Definition: CMemoryChunk.h:26
std::vector< TPropertyValueIDTriplet > m_properties
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
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.
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...



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019