Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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 /*---------------------------------------------------------------
18  Class
19  ---------------------------------------------------------------*/
20 namespace mrpt
21 {
22  namespace utils
23  {
24  // This must be added to any CSerializable derived class:
26 
27  /** Internal triplet for each property in utils::CMHPropertiesValuesList */
29  {
30  TPropertyValueIDTriplet() : name(), value(NULL),ID(0)
31  {}
32 
33  std::string name;
34  CSerializablePtr value;
35  int64_t ID;
36  };
37 
38  /** An arbitrary list of "annotations", or named attributes, each being an instance of any CSerializable object (Multi-hypotheses version).
39  * For each named annotation (or attribute), several values may exist, each associated to a given hypothesis ID.
40  * A non multi-hypotheses version exists in CPropertiesValuesList.
41  * \sa CSerializable, CPropertiesValuesList
42  * \ingroup mrpt_base_grp
43  */
45  {
46  // This must be added to any CSerializable derived class:
48 
49  private:
50  std::vector<TPropertyValueIDTriplet> m_properties;
51 
52  public:
53  /** Default constructor
54  */
56 
57  /** Copy constructor
58  */
60 
61  /** Copy operator
62  */
64 
65  /** Destructor
66  */
68 
69  /** Clears the list and frees all object's memory.
70  */
71  void clear();
72 
73  /** Returns the value of the property (case insensitive) for some given hypothesis ID, or a NULL smart pointer if it does not exist.
74  */
75  CSerializablePtr get(const char *propertyName, const int64_t & hypothesis_ID ) const;
76 
77  /** 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.
78  */
79  template <typename T>
80  typename T::SmartPtr getAs(const char *propertyName, const int64_t & hypothesis_ID, bool allowNullPointer = true) const
81  {
83  CSerializablePtr obj = get(propertyName,hypothesis_ID);
84  if (!obj)
85  {
86  if (allowNullPointer)
87  return typename T::SmartPtr();
88  else THROW_EXCEPTION("Null pointer")
89  }
90  const mrpt::utils::TRuntimeClassId* class_ID = T::classinfo;
91  ASSERT_( class_ID == obj->GetRuntimeClass() );
92  return typename T::SmartPtr( obj );
93  MRPT_END
94  }
95 
96 
97  /** Returns the value of the property (case insensitive) for the first hypothesis ID found, or NULL if it does not exist.
98  */
99  CSerializablePtr getAnyHypothesis(const char *propertyName) const;
100 
101  /** 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)
102  * \sa setMemoryReference
103  */
104  void set(const char *propertyName, const CSerializablePtr &obj, const int64_t & hypothesis_ID);
105 
106  /** 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.
107  * \sa set
108  */
109  void setMemoryReference(const char *propertyName, const CSerializablePtr& obj, const int64_t & hypothesis_ID);
110 
111  /** Remove a given property, if it exists.
112  */
113  void remove(const char *propertyName, const int64_t & hypothesis_ID);
114 
115  /** Remove all the properties for the given hypothesis.
116  */
117  void removeAll(const int64_t & hypothesis_ID);
118 
119  /** Sets/change the value of a property (case insensitive) for the given hypothesis ID, from an elemental data type.
120  */
121  template <class T>
122  void setElemental(const char *propertyName, const T &data, const int64_t & hypothesis_ID)
123  {
124  MRPT_START
125 
126  CMemoryChunkPtr memChunk = CMemoryChunkPtr( new CMemoryChunk() );
127  memChunk->setAllocBlockSize(10);
128  (*memChunk) << data;
129 
130  for (std::vector<TPropertyValueIDTriplet>::iterator it=m_properties.begin();it!=m_properties.end();++it)
131  {
132  if ( it->ID == hypothesis_ID && mrpt::system::strCmpI(propertyName,it->name) )
133  {
134  // Delete current contents &
135  // Copy new value:
136  it->value = memChunk;
137  return;
138  }
139  }
140 
141  // Insert as new:
142  TPropertyValueIDTriplet newPair;
143  newPair.name = std::string(propertyName);
144  newPair.value = memChunk;
145  newPair.ID = hypothesis_ID;
146  m_properties.push_back(newPair);
147 
149  printf("Exception while setting annotation '%s'",propertyName); \
150  );
151  }
152 
153  /** 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).
154  * \return false if the property does not exist for the given hypothesis.
155  */
156  template <class T>
157  bool getElemental(const char *propertyName, T &out_data, const int64_t & hypothesis_ID, bool raiseExceptionIfNotFound = false) const
158  {
159  MRPT_START
160  for (std::vector<TPropertyValueIDTriplet>::const_iterator it=m_properties.begin();it!=m_properties.end();++it)
161  {
162  if (mrpt::system::strCmpI(propertyName,it->name) && it->ID == hypothesis_ID )
163  {
164  CMemoryChunkPtr memChunk = CMemoryChunkPtr(it->value);
165  ASSERT_(memChunk)
166  if (memChunk->getTotalBytesCount()!=sizeof(out_data)) THROW_EXCEPTION("Data sizes do not match.");
167  out_data = *static_cast<T*>( memChunk->getRawBufferData() );
168  return true;
169  }
170  }
171  // Not found:
172  if (raiseExceptionIfNotFound)
173  THROW_EXCEPTION_CUSTOM_MSG1("Property '%s' not found", propertyName );
174  return false;
175  MRPT_END
176  }
177 
178  /** Returns the name of all properties in the list
179  */
180  std::vector<std::string> getPropertyNames() const;
181 
182 
185 
186  iterator begin() { return m_properties.begin(); }
187  const_iterator begin() const { return m_properties.begin(); }
188  iterator end() { return m_properties.end(); }
189  const_iterator end() const { return m_properties.end(); }
190 
191  size_t size() const { return m_properties.size(); }
192 
193  }; // End of class def.
195 
196 
197  } // End of namespace
198 } // end of namespace
199 #endif
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: mrpt_macros.h:110
iterator
Scalar * iterator
Definition: eigen_plugins.h:23
mrpt::utils::TPropertyValueIDTriplet
Internal triplet for each property in utils::CMHPropertiesValuesList.
Definition: CMHPropertiesValuesList.h:29
mrpt::utils::CMHPropertiesValuesList::iterator
std::vector< TPropertyValueIDTriplet >::iterator iterator
Definition: CMHPropertiesValuesList.h:183
const_iterator
const Scalar * const_iterator
Definition: eigen_plugins.h:24
string_utils.h
mrpt::utils::CMHPropertiesValuesList
An arbitrary list of "annotations", or named attributes, each being an instance of any CSerializable ...
Definition: CMHPropertiesValuesList.h:45
mrpt::utils::CMHPropertiesValuesList::end
const_iterator end() const
Definition: CMHPropertiesValuesList.h:189
mrpt::utils::CMHPropertiesValuesList::getAnyHypothesis
CSerializablePtr getAnyHypothesis(const char *propertyName) const
Returns the value of the property (case insensitive) for the first hypothesis ID found,...
DEFINE_SERIALIZABLE_POST_CUSTOM_BASE
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE(class_name, base_name)
Definition: CSerializable.h:182
mrpt::utils::CMHPropertiesValuesList::setMemoryReference
void setMemoryReference(const char *propertyName, const CSerializablePtr &obj, const int64_t &hypothesis_ID)
Sets/change the value of the property (case insensitive) for the given hypothesis ID,...
mrpt::utils::CMHPropertiesValuesList::removeAll
void removeAll(const int64_t &hypothesis_ID)
Remove all the properties for the given hypothesis.
MRPT_END_WITH_CLEAN_UP
#define MRPT_END_WITH_CLEAN_UP(stuff)
Definition: mrpt_macros.h:356
mrpt::utils::CMHPropertiesValuesList::getAs
T::SmartPtr 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:80
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CParticleFilter.h:17
MRPT_END
#define MRPT_END
Definition: mrpt_macros.h:353
mrpt::utils::CMHPropertiesValuesList::CMHPropertiesValuesList
CMHPropertiesValuesList()
Default constructor.
mrpt::utils::CMHPropertiesValuesList::remove
void remove(const char *propertyName, const int64_t &hypothesis_ID)
Remove a given property, if it exists.
mrpt::utils::CMHPropertiesValuesList::end
iterator end()
Definition: CMHPropertiesValuesList.h:188
MRPT_START
#define MRPT_START
Definition: mrpt_macros.h:349
mrpt::utils::CMHPropertiesValuesList::CMHPropertiesValuesList
CMHPropertiesValuesList(const CMHPropertiesValuesList &o)
Copy constructor.
mrpt::utils::CMemoryChunk
A memory buffer (implements CStream) which can be itself serialized.
Definition: CMemoryChunk.h:31
mrpt::utils::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:122
mrpt::system::strCmpI
bool BASE_IMPEXP strCmpI(const std::string &s1, const std::string &s2)
Return true if the two strings are equal (case insensitive)
mrpt::utils::TPropertyValueIDTriplet::value
CSerializablePtr value
Definition: CMHPropertiesValuesList.h:34
mrpt::utils::CMHPropertiesValuesList::begin
iterator begin()
Definition: CMHPropertiesValuesList.h:186
mrpt::utils::CMHPropertiesValuesList::clear
void clear()
Clears the list and frees all object's memory.
mrpt::utils::CMHPropertiesValuesList::~CMHPropertiesValuesList
virtual ~CMHPropertiesValuesList()
Destructor.
mrpt::utils::CSerializable
The virtual base class which provides a unified interface for all persistent objects in MRPT.
Definition: CSerializable.h:40
mrpt::utils::CMHPropertiesValuesList::begin
const_iterator begin() const
Definition: CMHPropertiesValuesList.h:187
mrpt::utils::CMHPropertiesValuesList::set
void set(const char *propertyName, const CSerializablePtr &obj, const int64_t &hypothesis_ID)
Sets/change the value of the property (case insensitive) for the given hypothesis ID,...
DEFINE_SERIALIZABLE
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
Definition: CSerializable.h:147
mrpt::utils::TPropertyValueIDTriplet::ID
int64_t ID
Definition: CMHPropertiesValuesList.h:35
mrpt::utils::CMHPropertiesValuesList::getPropertyNames
std::vector< std::string > getPropertyNames() const
Returns the name of all properties in the list.
mrpt::utils::TPropertyValueIDTriplet::name
std::string name
Definition: CMHPropertiesValuesList.h:33
mrpt::utils::CMHPropertiesValuesList::const_iterator
std::vector< TPropertyValueIDTriplet >::const_iterator const_iterator
Definition: CMHPropertiesValuesList.h:184
ASSERT_
#define ASSERT_(f)
Definition: mrpt_macros.h:261
mrpt::utils::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:157
mrpt::utils::CMHPropertiesValuesList::m_properties
std::vector< TPropertyValueIDTriplet > m_properties
Definition: CMHPropertiesValuesList.h:50
CSerializable.h
mrpt::utils::TPropertyValueIDTriplet::TPropertyValueIDTriplet
TPropertyValueIDTriplet()
Definition: CMHPropertiesValuesList.h:30
mrpt::utils::CMHPropertiesValuesList::size
size_t size() const
Definition: CMHPropertiesValuesList.h:191
CMemoryChunk.h
mrpt::utils::TRuntimeClassId
A structure that holds runtime class type information.
Definition: CObject.h:47
mrpt::utils::CMHPropertiesValuesList::get
CSerializablePtr get(const char *propertyName, const int64_t &hypothesis_ID) const
Returns the value of the property (case insensitive) for some given hypothesis ID,...
THROW_EXCEPTION_CUSTOM_MSG1
#define THROW_EXCEPTION_CUSTOM_MSG1(msg, param1)
DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE(class_name, base_name)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
Definition: CSerializable.h:178



Page generated by Doxygen 1.8.20 for MRPT 1.4.0 SVN: at Thu Aug 27 02:40:23 UTC 2020