Main MRPT website > C++ reference
MRPT logo
CMHPropertiesValuesList.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CMHPropertiesValuesList_H
29 #define CMHPropertiesValuesList_H
30 
33 #include <mrpt/system/os.h>
34 
35 /*---------------------------------------------------------------
36  Class
37  ---------------------------------------------------------------*/
38 namespace mrpt
39 {
40  namespace utils
41  {
42  using namespace mrpt;
43 
44  // This must be added to any CSerializable derived class:
46 
47 
48  /** Internal triplet for each property in utils::CMHPropertiesValuesList */
50  {
51  TPropertyValueIDTriplet() : name(), value(NULL),ID(0)
52  {}
53 
54  std::string name;
56  int64_t ID;
57  };
58 
59  /** An arbitrary list of "annotations", or named attributes, each being an instance of any CSerializable object (Multi-hypotheses version).
60  * For each named annotation (or attribute), several values may exist, each associated to a given hypothesis ID.
61  * A non multi-hypotheses version exists in CPropertiesValuesList.
62  * \sa CSerializable, CPropertiesValuesList
63  * \ingroup mrpt_base_grp
64  */
66  {
67  // This must be added to any CSerializable derived class:
69 
70  private:
71  std::vector<TPropertyValueIDTriplet> m_properties;
72 
73  public:
74  /** Default constructor
75  */
77 
78  /** Copy constructor
79  */
80  CMHPropertiesValuesList( const CMHPropertiesValuesList& o );
81 
82  /** Copy operator
83  */
84  CMHPropertiesValuesList & operator =( const CMHPropertiesValuesList& o );
85 
86  /** Destructor
87  */
88  virtual ~CMHPropertiesValuesList();
89 
90  /** Clears the list and frees all object's memory.
91  */
92  void clear();
93 
94  /** Returns the value of the property (case insensitive) for some given hypothesis ID, or a NULL smart pointer if it does not exist.
95  */
96  CSerializablePtr get(const char *propertyName, const int64_t & hypothesis_ID ) const;
97 
98  /** 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.
99  */
100  template <typename T>
101  typename T::SmartPtr getAs(const char *propertyName, const int64_t & hypothesis_ID, bool allowNullPointer = true) const
102  {
103  MRPT_START
104  CSerializablePtr obj = get(propertyName,hypothesis_ID);
105  if (!obj)
106  {
107  if (allowNullPointer)
108  return typename T::SmartPtr();
109  else THROW_EXCEPTION("Null pointer")
110  }
111  const mrpt::utils::TRuntimeClassId* class_ID = T::classinfo;
112  ASSERT_( class_ID == obj->GetRuntimeClass() );
113  return typename T::SmartPtr( obj );
114  MRPT_END
115  }
116 
117 
118  /** Returns the value of the property (case insensitive) for the first hypothesis ID found, or NULL if it does not exist.
119  */
120  CSerializablePtr getAnyHypothesis(const char *propertyName) const;
121 
122  /** 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)
123  * \sa setMemoryReference
124  */
125  void set(const char *propertyName, const CSerializablePtr &obj, const int64_t & hypothesis_ID);
126 
127  /** 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.
128  * \sa set
129  */
130  void setMemoryReference(const char *propertyName, const CSerializablePtr& obj, const int64_t & hypothesis_ID);
131 
132  /** Remove a given property, if it exists.
133  */
134  void remove(const char *propertyName, const int64_t & hypothesis_ID);
135 
136  /** Remove all the properties for the given hypothesis.
137  */
138  void removeAll(const int64_t & hypothesis_ID);
139 
140  /** Sets/change the value of a property (case insensitive) for the given hypothesis ID, from an elemental data type.
141  */
142  template <class T>
143  void setElemental(const char *propertyName, const T &data, const int64_t & hypothesis_ID)
144  {
145  MRPT_START
146 
147  CMemoryChunkPtr memChunk = CMemoryChunkPtr( new CMemoryChunk() );
148  memChunk->setAllocBlockSize(10);
149  (*memChunk) << data;
150 
151  for (std::vector<TPropertyValueIDTriplet>::iterator it=m_properties.begin();it!=m_properties.end();++it)
152  {
153  if ( it->ID == hypothesis_ID && !system::os::_strcmpi(propertyName,it->name.c_str()) )
154  {
155  // Delete current contents &
156  // Copy new value:
157  it->value = memChunk;
158  return;
159  }
160  }
161 
162  // Insert as new:
163  TPropertyValueIDTriplet newPair;
164  newPair.name = std::string(propertyName);
165  newPair.value = memChunk;
166  newPair.ID = hypothesis_ID;
167  m_properties.push_back(newPair);
168 
170  printf("Exception while setting annotation '%s'",propertyName); \
171  );
172  }
173 
174  /** 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).
175  * \return false if the property does not exist for the given hypothesis.
176  */
177  template <class T>
178  bool getElemental(const char *propertyName, T &out_data, const int64_t & hypothesis_ID, bool raiseExceptionIfNotFound = false) const
179  {
180  MRPT_START
181  for (std::vector<TPropertyValueIDTriplet>::const_iterator it=m_properties.begin();it!=m_properties.end();++it)
182  {
183  if (!system::os::_strcmpi(propertyName,it->name.c_str()) && it->ID == hypothesis_ID )
184  {
185  CMemoryChunkPtr memChunk = CMemoryChunkPtr(it->value);
186  ASSERT_(memChunk)
187  if (memChunk->getTotalBytesCount()!=sizeof(out_data)) 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_CUSTOM_MSG1("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 
203 
206 
207  iterator begin() { return m_properties.begin(); }
208  const_iterator begin() const { return m_properties.begin(); }
209  iterator end() { return m_properties.end(); }
210  const_iterator end() const { return m_properties.end(); }
211 
212  size_t size() const { return m_properties.size(); }
213 
214  }; // End of class def.
215 
216 
217  } // End of namespace
218 } // end of namespace
219 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013