Main MRPT website > C++ reference for MRPT 1.9.9
CPropertiesValuesList.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 
14 #include <mrpt/system/os.h>
15 #include <stdio.h>
16 #include <iostream>
17 
18 using namespace mrpt::system;
19 using namespace mrpt::hmtslam;
20 using namespace mrpt::serialization;
21 
22 // This must be added to any CSerializable class implementation file.
24 
25 uint8_t CPropertiesValuesList::serializeGetVersion() const { return 0; }
26 void CPropertiesValuesList::serializeTo(
28 {
29  uint32_t i, n = (uint32_t)size();
30  uint8_t isNull;
31  out << n;
32 
33  for (i = 0; i < n; i++)
34  {
35  // Name:
36  out << m_properties[i].name.c_str();
37 
38  // Object:
39  isNull = m_properties[i].value ? 1 : 0;
40  out << isNull;
41 
42  if (m_properties[i].value) out << *m_properties[i].value;
43  }
44 }
45 
46 void CPropertiesValuesList::serializeFrom(
48 {
49  switch (version)
50  {
51  case 0:
52  {
53  uint32_t i, n;
54  uint8_t isNull;
55 
56  // Erase previous contents:
57  clear();
58 
59  in >> n;
60 
61  m_properties.resize(n);
62  for (i = 0; i < n; i++)
63  {
64  char nameBuf[1024];
65  // Name:
66  in >> nameBuf;
67  m_properties[i].name = nameBuf;
68 
69  // Object:
70  in >> isNull;
71 
72  if (isNull)
73  m_properties[i].value.reset(
74  static_cast<CSerializable*>(nullptr));
75  else
76  in >> m_properties[i].value;
77  }
78  }
79  break;
80  default:
82  };
83 }
84 
85 /*---------------------------------------------------------------
86  Constructor
87  ---------------------------------------------------------------*/
88 CPropertiesValuesList::CPropertiesValuesList() {}
89 /*---------------------------------------------------------------
90  Destructor
91  ---------------------------------------------------------------*/
92 CPropertiesValuesList::~CPropertiesValuesList() { clear(); }
93 /*---------------------------------------------------------------
94  Copy Constructor
95  ---------------------------------------------------------------*/
96 CPropertiesValuesList::CPropertiesValuesList(const CPropertiesValuesList& o)
97  : m_properties(o.m_properties)
98 {
100  it != m_properties.end(); ++it)
101  it->value.reset(dynamic_cast<CSerializable*>(it->value->clone()));
102 }
103 
104 /*---------------------------------------------------------------
105  Copy
106  ---------------------------------------------------------------*/
108  const CPropertiesValuesList& o)
109 {
110  if (this != &o) return *this;
111 
114  it != m_properties.end(); ++it)
115  it->value.reset(dynamic_cast<CSerializable*>(it->value->clone()));
116  return *this;
117 }
118 
119 /*---------------------------------------------------------------
120  clear
121  ---------------------------------------------------------------*/
123 {
124  MRPT_START
125  m_properties.clear();
126  MRPT_END
127 }
128 
129 /*---------------------------------------------------------------
130  get
131  ---------------------------------------------------------------*/
133  const std::string& propertyName) const
134 {
136  m_properties.begin();
137  it != m_properties.end(); ++it)
138  {
139  if (!os::_strcmpi(propertyName.c_str(), it->name.c_str()))
140  return it->value;
141  }
142  // Not found:
143  return CSerializable::Ptr();
144 }
145 
146 /*---------------------------------------------------------------
147  set
148  ---------------------------------------------------------------*/
150  const std::string& propertyName, const CSerializable::Ptr& obj)
151 {
152  MRPT_START
153 
155  it != m_properties.end(); ++it)
156  {
157  if (!os::_strcmpi(propertyName.c_str(), it->name.c_str()))
158  {
159  // Delete current contents:
160  // Copy new value:
161  if (!obj)
162  it->value.reset();
163  else
164  it->value = obj; //->clone();
165  return;
166  }
167  }
168 
169  // Insert:
170  TPropertyValuePair newPair;
171  newPair.name = std::string(propertyName);
172  newPair.value = obj;
173  m_properties.push_back(newPair);
174 
176  printf(
177  "Exception while setting annotation '%s'", propertyName.c_str()););
178 }
179 
180 /*---------------------------------------------------------------
181  size
182  ---------------------------------------------------------------*/
183 size_t CPropertiesValuesList::size() const { return m_properties.size(); }
184 /*---------------------------------------------------------------
185  getPropertyNames
186  ---------------------------------------------------------------*/
187 std::vector<std::string> CPropertiesValuesList::getPropertyNames() const
188 {
189  std::vector<std::string> ret;
190 
192  m_properties.begin();
193  it != m_properties.end(); ++it)
194  ret.push_back(it->name);
195 
196  return ret;
197 }
Scalar * iterator
Definition: eigen_plugins.h:26
#define MRPT_START
Definition: exceptions.h:262
std::vector< std::string > getPropertyNames() const
Returns the name of all properties in the list.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:25
Classes related to the implementation of Hybrid Metric Topological (HMT) SLAM.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
GLenum GLsizei n
Definition: glext.h:5074
void set(const std::string &propertyName, const CSerializable::Ptr &obj)
Sets/change the value of the property (case insensitive), making a copy of the object (or setting it ...
#define MRPT_END_WITH_CLEAN_UP(stuff)
Definition: exceptions.h:268
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
unsigned char uint8_t
Definition: rptypes.h:41
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:90
GLsizei const GLchar ** string
Definition: glext.h:4101
CSerializable::Ptr get(const std::string &propertyName) const
Returns the value of the property (case insensitive), or nullptr if it does not exist.
std::vector< TPropertyValuePair > m_properties
The properties list: a map between strings and objects.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
#define MRPT_END
Definition: exceptions.h:266
size_t size() const
Returns the number of properties in the list.
GLuint in
Definition: glext.h:7274
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:32
GLsizei const GLfloat * value
Definition: glext.h:4117
GLsizeiptr size
Definition: glext.h:3923
unsigned __int32 uint32_t
Definition: rptypes.h:47
CPropertiesValuesList & operator=(const CPropertiesValuesList &o)
Copy operator.
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:188
const Scalar * const_iterator
Definition: eigen_plugins.h:27
An arbitrary list of "annotations", or named attributes, each being an instance of any CSerializable ...
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.
Definition: os.cpp:320



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019