Main MRPT website > C++ reference for MRPT 1.5.6
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  // This must be added to any CSerializable derived class:
23 
24  /** Internal triplet for each property in utils::CMHPropertiesValuesList */
26  {
27  TPropertyValueIDTriplet() : name(), value(NULL),ID(0)
28  {}
29 
31  CSerializablePtr value;
33  };
34 
35  /** An arbitrary list of "annotations", or named attributes, each being an instance of any CSerializable object (Multi-hypotheses version).
36  * For each named annotation (or attribute), several values may exist, each associated to a given hypothesis ID.
37  * A non multi-hypotheses version exists in CPropertiesValuesList.
38  * \sa CSerializable, CPropertiesValuesList
39  * \ingroup mrpt_base_grp
40  */
42  {
43  // This must be added to any CSerializable derived class:
45 
46  private:
47  std::vector<TPropertyValueIDTriplet> m_properties;
48 
49  public:
50  /** Default constructor
51  */
53 
54  /** Copy constructor
55  */
57 
58  /** Copy operator
59  */
60  CMHPropertiesValuesList & operator =( const CMHPropertiesValuesList& o );
61 
62  /** Destructor
63  */
64  virtual ~CMHPropertiesValuesList();
65 
66  /** Clears the list and frees all object's memory.
67  */
68  void clear();
69 
70  /** Returns the value of the property (case insensitive) for some given hypothesis ID, or a NULL smart pointer if it does not exist.
71  */
72  CSerializablePtr get(const char *propertyName, const int64_t & hypothesis_ID ) const;
73 
74  /** Returns the value of the property (case insensitive) for some given hypothesis ID checking its class in runtime, or a NULL smart pointer if it does not exist.
75  */
76  template <typename T>
77  typename T::Ptr getAs(const char *propertyName, const int64_t & hypothesis_ID, bool allowNullPointer = true) const
78  {
80  CSerializablePtr obj = get(propertyName,hypothesis_ID);
81  if (!obj)
82  {
83  if (allowNullPointer)
84  return typename T::Ptr();
85  else THROW_EXCEPTION("Null pointer")
86  }
87  const mrpt::utils::TRuntimeClassId* class_ID = T::classinfo;
88  ASSERT_( class_ID == obj->GetRuntimeClass() );
89  return typename T::Ptr( obj );
90  MRPT_END
91  }
92 
93 
94  /** Returns the value of the property (case insensitive) for the first hypothesis ID found, or NULL if it does not exist.
95  */
96  CSerializablePtr getAnyHypothesis(const char *propertyName) const;
97 
98  /** Sets/change the value of the property (case insensitive) for the given hypothesis ID, making a copy of the object (or setting it to NULL if it is the passed value)
99  * \sa setMemoryReference
100  */
101  void set(const char *propertyName, const CSerializablePtr &obj, const int64_t & hypothesis_ID);
102 
103  /** Sets/change the value of the property (case insensitive) for the given hypothesis ID, directly replacing the pointer instead of making a copy of the object.
104  * \sa set
105  */
106  void setMemoryReference(const char *propertyName, const CSerializablePtr& obj, const int64_t & hypothesis_ID);
107 
108  /** Remove a given property, if it exists.
109  */
110  void remove(const char *propertyName, const int64_t & hypothesis_ID);
111 
112  /** Remove all the properties for the given hypothesis.
113  */
114  void removeAll(const int64_t & hypothesis_ID);
115 
116  /** Sets/change the value of a property (case insensitive) for the given hypothesis ID, from an elemental data type.
117  */
118  template <class T>
119  void setElemental(const char *propertyName, const T &data, const int64_t & hypothesis_ID)
120  {
121  MRPT_START
122 
123  CMemoryChunkPtr memChunk = CMemoryChunkPtr( new CMemoryChunk() );
124  memChunk->setAllocBlockSize(10);
125  (*memChunk) << data;
126 
127  for (std::vector<TPropertyValueIDTriplet>::iterator it=m_properties.begin();it!=m_properties.end();++it)
128  {
129  if ( it->ID == hypothesis_ID && mrpt::system::strCmpI(propertyName,it->name) )
130  {
131  // Delete current contents &
132  // Copy new value:
133  it->value = memChunk;
134  return;
135  }
136  }
137 
138  // Insert as new:
139  TPropertyValueIDTriplet newPair;
140  newPair.name = std::string(propertyName);
141  newPair.value = memChunk;
142  newPair.ID = hypothesis_ID;
143  m_properties.push_back(newPair);
144 
146  printf("Exception while setting annotation '%s'",propertyName); \
147  );
148  }
149 
150  /** Gets the value of a property (case insensitive) for the given hypothesis ID, retrieves it as an elemental data type (types must coincide, basic size check is performed).
151  * \return false if the property does not exist for the given hypothesis.
152  */
153  template <class T>
154  bool getElemental(const char *propertyName, T &out_data, const int64_t & hypothesis_ID, bool raiseExceptionIfNotFound = false) const
155  {
156  MRPT_START
157  for (std::vector<TPropertyValueIDTriplet>::const_iterator it=m_properties.begin();it!=m_properties.end();++it)
158  {
159  if (mrpt::system::strCmpI(propertyName,it->name) && it->ID == hypothesis_ID )
160  {
161  CMemoryChunkPtr memChunk = CMemoryChunkPtr(it->value);
162  ASSERT_(memChunk)
163  if (memChunk->getTotalBytesCount()!=sizeof(out_data)) THROW_EXCEPTION("Data sizes do not match.");
164  out_data = *static_cast<T*>( memChunk->getRawBufferData() );
165  return true;
166  }
167  }
168  // Not found:
169  if (raiseExceptionIfNotFound)
170  THROW_EXCEPTION_FMT("Property '%s' not found", propertyName );
171  return false;
172  MRPT_END
173  }
174 
175  /** Returns the name of all properties in the list
176  */
177  std::vector<std::string> getPropertyNames() const;
178 
179 
182 
183  iterator begin() { return m_properties.begin(); }
184  const_iterator begin() const { return m_properties.begin(); }
185  iterator end() { return m_properties.end(); }
186  const_iterator end() const { return m_properties.end(); }
187 
188  size_t size() const { return m_properties.size(); }
189 
190  }; // End of class def.
192 
193 
194  } // End of namespace
195 } // end of namespace
196 #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:39
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Scalar * iterator
Definition: eigen_plugins.h:23
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:24
GLsizei GLsizei GLuint * obj
Definition: glext.h:3902
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:113
Internal triplet for each property in utils::CMHPropertiesValuesList.
#define MRPT_END
__int64 int64_t
Definition: rptypes.h:51
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE(class_name, base_name)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
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:27
GLsizei const GLchar ** string
Definition: glext.h:3919
#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
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE(class_name, base_name)
GLuint const GLchar * name
Definition: glext.h:3891
#define ASSERT_(f)
GLsizei const GLfloat * value
Definition: glext.h:3929
A structure that holds runtime class type information.
Definition: CObject.h:46
std::vector< TPropertyValueIDTriplet > m_properties
bool BASE_IMPEXP 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:3520
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.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019