Main MRPT website > C++ reference for MRPT 1.9.9
CMHPropertiesValuesList.cpp
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 
10 #include "base-precomp.h" // Precompiled headers
11 
13 #include <mrpt/system/os.h>
14 #include <stdio.h>
15 
16 using namespace mrpt::utils;
17 using namespace mrpt::system;
18 
19 // This must be added to any CSerializable class implementation file.
21 
22 /*---------------------------------------------------------------
23  writeToStream
24  ---------------------------------------------------------------*/
26  mrpt::utils::CStream& out, int* out_Version) const
27 {
28  if (out_Version)
29  *out_Version = 0;
30  else
31  {
32  uint32_t i, n = (uint32_t)m_properties.size();
33  uint8_t isNull;
34  out << n;
35 
36  for (i = 0; i < n; i++)
37  {
38  // Name:
39  out << m_properties[i].name.c_str();
40 
41  // Object:
42  isNull = !m_properties[i].value;
43  out << isNull;
44 
45  if (!isNull) out << *m_properties[i].value;
46 
47  // Hypot. ID:
48  out << m_properties[i].ID;
49  }
50  }
51 }
52 
53 /*---------------------------------------------------------------
54  readFromStream
55  ---------------------------------------------------------------*/
57  mrpt::utils::CStream& in, int version)
58 {
59  switch (version)
60  {
61  case 0:
62  {
63  uint32_t i, n;
64  uint8_t isNull;
65 
66  // Erase previous contents:
67  clear();
68 
69  in >> n;
70 
71  m_properties.resize(n);
72  for (i = 0; i < n; i++)
73  {
74  char nameBuf[1024];
75  // Name:
76  in >> nameBuf;
77  m_properties[i].name = nameBuf;
78 
79  // Object:
80  in >> isNull;
81 
82  if (isNull)
83  m_properties[i].value.reset();
84  else
85  in >> m_properties[i].value;
86 
87  // Hypot. ID:
88  in >> m_properties[i].ID;
89  }
90  }
91  break;
92  default:
94  };
95 }
96 
97 /*---------------------------------------------------------------
98  Constructor
99  ---------------------------------------------------------------*/
101 /*---------------------------------------------------------------
102  Destructor
103  ---------------------------------------------------------------*/
105 /*---------------------------------------------------------------
106  clear
107  ---------------------------------------------------------------*/
109 {
110  MRPT_START
111  m_properties.clear();
112  MRPT_END
113 }
114 
115 /*---------------------------------------------------------------
116  get
117  ---------------------------------------------------------------*/
119  const char* propertyName, const int64_t& hypothesis_ID) const
120 {
122  for (it = m_properties.begin(); it != m_properties.end(); ++it)
123  if (!os::_strcmpi(propertyName, it->name.c_str()) &&
124  it->ID == hypothesis_ID)
125  return it->value;
126 
127  for (it = m_properties.begin(); it != m_properties.end(); ++it)
128  if (!os::_strcmpi(propertyName, it->name.c_str()) && it->ID == 0)
129  return it->value;
130 
131  // Not found:
132  return CSerializable::Ptr();
133 }
134 
135 /*---------------------------------------------------------------
136  getAnyHypothesis
137  ---------------------------------------------------------------*/
139  const char* propertyName) const
140 {
142  m_properties.begin();
143  it != m_properties.end(); ++it)
144  {
145  if (!os::_strcmpi(propertyName, it->name.c_str())) return it->value;
146  }
147  // Not found:
148  return CSerializable::Ptr();
149 }
150 
151 /*---------------------------------------------------------------
152  set
153  ---------------------------------------------------------------*/
155  const char* propertyName, const CSerializable::Ptr& obj,
156  const int64_t& hypothesis_ID)
157 {
158  MRPT_START
159 
161  m_properties.begin();
162  it != m_properties.end(); ++it)
163  {
164  if (it->ID == hypothesis_ID &&
165  !os::_strcmpi(propertyName, it->name.c_str()))
166  {
167  // Delete current contents:
168  // Copy new value:
169  it->value.reset(dynamic_cast<CSerializable*>(obj->clone()));
170 
171  // if (!obj) it->value.clear();
172  // else it->value = obj; //->clone();
173  return;
174  }
175  }
176 
177  // Insert:
178  TPropertyValueIDTriplet newPair;
179  newPair.name = std::string(propertyName);
180  newPair.value = obj;
181  newPair.ID = hypothesis_ID;
182  m_properties.push_back(newPair);
183 
185  printf("Exception while setting annotation '%s'", propertyName););
186 }
187 
188 /*---------------------------------------------------------------
189  setMemoryReference
190  ---------------------------------------------------------------*/
192  const char* propertyName, const CSerializable::Ptr& obj,
193  const int64_t& hypothesis_ID)
194 {
195  MRPT_START
196 
198  m_properties.begin();
199  it != m_properties.end(); ++it)
200  {
201  if (it->ID == hypothesis_ID &&
202  !os::_strcmpi(propertyName, it->name.c_str()))
203  {
204  // Delete current contents & set a copy of the same smart pointer:
205  it->value = obj;
206  return;
207  }
208  }
209 
210  // Insert:
211  TPropertyValueIDTriplet newPair;
212  newPair.name = std::string(propertyName);
213  newPair.value = obj;
214  newPair.ID = hypothesis_ID;
215  m_properties.push_back(newPair);
216 
218  printf("Exception while setting annotation '%s'", propertyName););
219 }
220 
221 /*---------------------------------------------------------------
222  getPropertyNames
223  ---------------------------------------------------------------*/
224 std::vector<std::string> CMHPropertiesValuesList::getPropertyNames() const
225 {
226  std::vector<std::string> ret;
227 
229  m_properties.begin();
230  it != m_properties.end(); ++it)
231  {
232  bool isNew = true;
233  for (std::vector<std::string>::iterator itS = ret.begin();
234  itS != ret.end(); ++itS)
235  {
236  if ((*itS) == it->name)
237  {
238  isNew = false;
239  break;
240  }
241  }
242  if (isNew) ret.push_back(it->name); // Yes, it is new:
243  }
244 
245  return ret;
246 }
247 
248 /*---------------------------------------------------------------
249  remove
250  ---------------------------------------------------------------*/
252  const char* propertyName, const int64_t& hypothesis_ID)
253 {
255  m_properties.begin();
256  it != m_properties.end();)
257  if (!os::_strcmpi(propertyName, it->name.c_str()) &&
258  it->ID == hypothesis_ID)
259  it = m_properties.erase(it);
260  else
261  ++it;
262 }
263 
264 /*---------------------------------------------------------------
265  removeAll
266  ---------------------------------------------------------------*/
267 void CMHPropertiesValuesList::removeAll(const int64_t& hypothesis_ID)
268 {
270  m_properties.begin();
271  it != m_properties.end();)
272  if (it->ID == hypothesis_ID)
273  it = m_properties.erase(it);
274  else
275  ++it;
276 }
277 
278 /*---------------------------------------------------------------
279  Copy
280  ---------------------------------------------------------------*/
282  const CMHPropertiesValuesList& o)
283  : m_properties(o.m_properties)
284 {
286  m_properties.begin();
287  it != m_properties.end(); ++it)
288  it->value.reset(dynamic_cast<CSerializable*>(it->value->clone()));
289 }
290 
291 /*---------------------------------------------------------------
292  Copy
293  ---------------------------------------------------------------*/
295  const CMHPropertiesValuesList& o)
296 {
297  if (this == &o) return *this;
298 
300 
302  m_properties.begin();
303  it != m_properties.end(); ++it)
304  it->value.reset(dynamic_cast<CSerializable*>(it->value->clone()));
305  return *this;
306 }
An arbitrary list of "annotations", or named attributes, each being an instance of any CSerializable ...
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
#define MRPT_END_WITH_CLEAN_UP(stuff)
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
void remove(const char *propertyName, const int64_t &hypothesis_ID)
Remove a given property, if it exists.
GLenum GLsizei n
Definition: glext.h:5074
Scalar * iterator
Definition: eigen_plugins.h:26
const Scalar * const_iterator
Definition: eigen_plugins.h:27
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:189
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
unsigned char uint8_t
Definition: rptypes.h:41
Internal triplet for each property in utils::CMHPropertiesValuesList.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
CMHPropertiesValuesList & operator=(const CMHPropertiesValuesList &o)
Copy operator.
#define MRPT_END
__int64 int64_t
Definition: rptypes.h:49
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
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.
GLuint in
Definition: glext.h:7274
void clear()
Clears the list and frees all object&#39;s memory.
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...
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
std::vector< TPropertyValueIDTriplet > m_properties
unsigned __int32 uint32_t
Definition: rptypes.h:47
void set(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...
CSerializable::Ptr get(const char *propertyName, const int64_t &hypothesis_ID) const
Returns the value of the property (case insensitive) for some given hypothesis ID, or a nullptr smart pointer if it does not exist.
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.
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.
Definition: os.cpp:319



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