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 ---------------------------------------------------------------*/
20namespace 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 */
44 class BASE_IMPEXP CMHPropertiesValuesList : public mrpt::utils::CSerializable
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 );
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 {
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:
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 {
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;
176 }
177
178 /** Returns the name of all properties in the list
179 */
180 std::vector<std::string> getPropertyNames() const;
181
182
183 typedef std::vector<TPropertyValueIDTriplet>::iterator iterator;
184 typedef std::vector<TPropertyValueIDTriplet>::const_iterator const_iterator;
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.
194 DEFINE_SERIALIZABLE_POST_CUSTOM_BASE( CMHPropertiesValuesList, mrpt::utils::CSerializable )
195
196
197 } // End of namespace
198} // end of namespace
199#endif
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE(class_name, base_name)
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE(class_name, base_name)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
An arbitrary list of "annotations", or named attributes, each being an instance of any CSerializable ...
void remove(const char *propertyName, const int64_t &hypothesis_ID)
Remove a given property, if it exists.
CSerializablePtr getAnyHypothesis(const char *propertyName) const
Returns the value of the property (case insensitive) for the first hypothesis ID found,...
std::vector< std::string > getPropertyNames() const
Returns the name of all properties in the list.
CMHPropertiesValuesList()
Default constructor.
void clear()
Clears the list and frees all object's memory.
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,...
CMHPropertiesValuesList(const CMHPropertiesValuesList &o)
Copy constructor.
virtual ~CMHPropertiesValuesList()
Destructor.
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 ...
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,...
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,...
void removeAll(const int64_t &hypothesis_ID)
Remove all the properties for the given hypothesis.
std::vector< TPropertyValueIDTriplet > m_properties
std::vector< TPropertyValueIDTriplet >::iterator iterator
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...
std::vector< TPropertyValueIDTriplet >::const_iterator const_iterator
CSerializablePtr get(const char *propertyName, const int64_t &hypothesis_ID) const
Returns the value of the property (case insensitive) for some given hypothesis ID,...
A memory buffer (implements CStream) which can be itself serialized.
Definition: CMemoryChunk.h:31
bool BASE_IMPEXP strCmpI(const std::string &s1, const std::string &s2)
Return true if the two strings are equal (case insensitive)
#define MRPT_START
Definition: mrpt_macros.h:349
#define ASSERT_(f)
Definition: mrpt_macros.h:261
#define THROW_EXCEPTION_CUSTOM_MSG1(msg, param1)
#define MRPT_END
Definition: mrpt_macros.h:353
#define MRPT_END_WITH_CLEAN_UP(stuff)
Definition: mrpt_macros.h:356
#define THROW_EXCEPTION(msg)
Definition: mrpt_macros.h:110
struct BASE_IMPEXP CMemoryChunkPtr
Definition: CMemoryChunk.h:23
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Internal triplet for each property in utils::CMHPropertiesValuesList.
A structure that holds runtime class type information.
Definition: CObject.h:47



Page generated by Doxygen 1.9.5 for MRPT 1.4.0 SVN: at Tue Dec 27 00:54:45 UTC 2022