Main MRPT website > C++ reference for MRPT 1.5.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 
12 
14 #include <mrpt/system/os.h>
15 #include <stdio.h>
16 
17 using namespace mrpt::utils;
18 using namespace mrpt::system;
19 
20 // This must be added to any CSerializable class implementation file.
22 
23 /*---------------------------------------------------------------
24  writeToStream
25  ---------------------------------------------------------------*/
26 void CMHPropertiesValuesList::writeToStream(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)
46  out << *m_properties[i].value;
47 
48  // Hypot. ID:
49  out << m_properties[i].ID;
50  }
51 
52  }
53 
54 }
55 
56 /*---------------------------------------------------------------
57  readFromStream
58  ---------------------------------------------------------------*/
60 {
61  switch(version)
62  {
63  case 0:
64  {
65  uint32_t i,n;
66  uint8_t isNull;
67 
68  // Erase previous contents:
69  clear();
70 
71  in >> n;
72 
73  m_properties.resize(n);
74  for (i=0;i<n;i++)
75  {
76  char nameBuf[1024];
77  // Name:
78  in >> nameBuf;
79  m_properties[i].name = nameBuf;
80 
81  // Object:
82  in >> isNull;
83 
84  if (isNull)
85  m_properties[i].value.clear();
86  else
87  in >> m_properties[i].value;
88 
89  // Hypot. ID:
90  in >> m_properties[i].ID;
91  }
92 
93  } break;
94  default:
96 
97  };
98 }
99 
100 
101 /*---------------------------------------------------------------
102  Constructor
103  ---------------------------------------------------------------*/
105 {
106 
107 }
108 
109 /*---------------------------------------------------------------
110  Destructor
111  ---------------------------------------------------------------*/
113 {
114  clear();
115 }
116 
117 /*---------------------------------------------------------------
118  clear
119  ---------------------------------------------------------------*/
121 {
122  MRPT_START
123  m_properties.clear();
124  MRPT_END
125 }
126 
127 /*---------------------------------------------------------------
128  get
129  ---------------------------------------------------------------*/
131  const char *propertyName,
132  const int64_t &hypothesis_ID) const
133 {
135  for (it=m_properties.begin();it!=m_properties.end();++it)
136  if (!os::_strcmpi(propertyName,it->name.c_str()) && it->ID == hypothesis_ID )
137  return it->value;
138 
139  for (it=m_properties.begin();it!=m_properties.end();++it)
140  if (!os::_strcmpi(propertyName,it->name.c_str()) && it->ID == 0 )
141  return it->value;
142 
143  // Not found:
144  return CSerializablePtr();
145 }
146 
147 /*---------------------------------------------------------------
148  getAnyHypothesis
149  ---------------------------------------------------------------*/
150 CSerializablePtr CMHPropertiesValuesList::getAnyHypothesis(const char *propertyName) const
151 {
152  for (std::vector<TPropertyValueIDTriplet>::const_iterator it=m_properties.begin();it!=m_properties.end();++it)
153  {
154  if (!os::_strcmpi(propertyName,it->name.c_str()) )
155  return it->value;
156  }
157  // Not found:
158  return CSerializablePtr();
159 }
160 
161 /*---------------------------------------------------------------
162  set
163  ---------------------------------------------------------------*/
165  const char *propertyName,
166  const CSerializablePtr &obj,
167  const int64_t &hypothesis_ID)
168 {
169  MRPT_START
170 
171  for (std::vector<TPropertyValueIDTriplet>::iterator it=m_properties.begin();it!=m_properties.end();++it)
172  {
173  if ( it->ID == hypothesis_ID && !os::_strcmpi(propertyName,it->name.c_str()) )
174  {
175  // Delete current contents:
176  // Copy new value:
177  it->value = obj;
178  it->value.make_unique();
179 
180  //if (!obj) it->value.clear();
181  //else it->value = obj; //->duplicate();
182  return;
183  }
184  }
185 
186  // Insert:
187  TPropertyValueIDTriplet newPair;
188  newPair.name = std::string(propertyName);
189  newPair.value = obj;
190  newPair.ID = hypothesis_ID;
191  m_properties.push_back(newPair);
192 
194  printf("Exception while setting annotation '%s'",propertyName); \
195  );
196 }
197 
198 /*---------------------------------------------------------------
199  setMemoryReference
200  ---------------------------------------------------------------*/
202  const char *propertyName,
203  const CSerializablePtr &obj,
204  const int64_t & hypothesis_ID)
205 {
206  MRPT_START
207 
208  for (std::vector<TPropertyValueIDTriplet>::iterator it=m_properties.begin();it!=m_properties.end();++it)
209  {
210  if ( it->ID == hypothesis_ID && !os::_strcmpi(propertyName,it->name.c_str()) )
211  {
212  // Delete current contents & set a copy of the same smart pointer:
213  it->value = obj;
214  return;
215  }
216  }
217 
218  // Insert:
219  TPropertyValueIDTriplet newPair;
220  newPair.name = std::string(propertyName);
221  newPair.value = obj;
222  newPair.ID = hypothesis_ID;
223  m_properties.push_back(newPair);
224 
226  printf("Exception while setting annotation '%s'",propertyName); \
227  );
228 }
229 
230 
231 
232 /*---------------------------------------------------------------
233  getPropertyNames
234  ---------------------------------------------------------------*/
235 std::vector<std::string> CMHPropertiesValuesList::getPropertyNames() const
236 {
237  std::vector<std::string> ret;
238 
239  for (std::vector<TPropertyValueIDTriplet>::const_iterator it=m_properties.begin();it!=m_properties.end();++it)
240  {
241  bool isNew = true;
242  for (std::vector<std::string>::iterator itS = ret.begin(); itS!=ret.end(); ++itS)
243  {
244  if ( (*itS) == it->name )
245  {
246  isNew = false;
247  break;
248  }
249  }
250  if (isNew)
251  ret.push_back(it->name); // Yes, it is new:
252  }
253 
254  return ret;
255 }
256 
257 /*---------------------------------------------------------------
258  remove
259  ---------------------------------------------------------------*/
261  const char *propertyName,
262  const int64_t & hypothesis_ID)
263 {
264  for (std::vector<TPropertyValueIDTriplet>::iterator it=m_properties.begin();it!=m_properties.end(); )
265  if (!os::_strcmpi(propertyName,it->name.c_str()) && it->ID == hypothesis_ID )
266  it = m_properties.erase(it);
267  else ++it;
268 }
269 
270 /*---------------------------------------------------------------
271  removeAll
272  ---------------------------------------------------------------*/
273 void CMHPropertiesValuesList::removeAll( const int64_t & hypothesis_ID)
274 {
275  for (std::vector<TPropertyValueIDTriplet>::iterator it=m_properties.begin();it!=m_properties.end();)
276  if (it->ID == hypothesis_ID )
277  it = m_properties.erase(it);
278  else ++it;
279 }
280 
281 
282 
283 /*---------------------------------------------------------------
284  Copy
285  ---------------------------------------------------------------*/
287  m_properties ( o.m_properties )
288 {
290  it->value.make_unique();
291 }
292 
293 /*---------------------------------------------------------------
294  Copy
295  ---------------------------------------------------------------*/
297 {
298  if (this==&o) return *this;
299 
301 
303  it->value.make_unique();
304  return *this;
305 }
306 
An arbitrary list of "annotations", or named attributes, each being an instance of any CSerializable ...
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...
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
#define MRPT_END_WITH_CLEAN_UP(stuff)
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:39
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...
void remove(const char *propertyName, const int64_t &hypothesis_ID)
Remove a given property, if it exists.
GLenum GLsizei n
Definition: glext.h:4618
void readFromStream(mrpt::utils::CStream &in, int version)
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
Scalar * iterator
Definition: eigen_plugins.h:23
const Scalar * const_iterator
Definition: eigen_plugins.h:24
IMPLEMENTS_SERIALIZABLE(CLogFileRecord_FullEval, CHolonomicLogFileRecord, mrpt::nav) IMPLEMENTS_SERIALIZABLE(CHolonomicFullEval
GLsizei GLsizei GLuint * obj
Definition: glext.h:3902
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:113
unsigned char uint8_t
Definition: rptypes.h:43
CSerializablePtr getAnyHypothesis(const char *propertyName) const
Returns the value of the property (case insensitive) for the first hypothesis ID found, or NULL if it does not exist.
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:38
CMHPropertiesValuesList & operator=(const CMHPropertiesValuesList &o)
Copy operator.
#define MRPT_END
__int64 int64_t
Definition: rptypes.h:51
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
int version
Definition: mrpt_jpeglib.h:898
void removeAll(const int64_t &hypothesis_ID)
Remove all the properties for the given hypothesis.
GLsizei const GLchar ** string
Definition: glext.h:3919
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
CSerializablePtr 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 NULL smart pointer if it does not exist.
int BASE_IMPEXP _strcmpi(const char *str1, const char *str2) MRPT_NO_THROWS
An OS-independent version of strcmpi.
Definition: os.cpp:320
GLuint in
Definition: glext.h:6301
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.
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
std::vector< TPropertyValueIDTriplet > m_properties
unsigned __int32 uint32_t
Definition: rptypes.h:49



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020