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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST