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-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/utils/CStream.h>
14 #include <mrpt/system/os.h>
15 #include <stdio.h>
16 #include <iostream>
17 
18 using namespace mrpt::utils;
19 using namespace mrpt::system;
20 
21 // This must be added to any CSerializable class implementation file.
23 
24 /*---------------------------------------------------------------
25  writeToStream
26  ---------------------------------------------------------------*/
27 void CPropertiesValuesList::writeToStream(
28  mrpt::utils::CStream& out, int* out_Version) const
29 {
30  if (out_Version)
31  *out_Version = 0;
32  else
33  {
34  uint32_t i, n = (uint32_t)size();
35  uint8_t isNull;
36  out << n;
37 
38  for (i = 0; i < n; i++)
39  {
40  // Name:
41  out << m_properties[i].name.c_str();
42 
43  // Object:
44  isNull = m_properties[i].value ? 1 : 0;
45  out << isNull;
46 
47  if (m_properties[i].value) out << *m_properties[i].value;
48  }
49  }
50 }
51 
52 /*---------------------------------------------------------------
53  readFromStream
54  ---------------------------------------------------------------*/
56  mrpt::utils::CStream& in, int version)
57 {
58  switch (version)
59  {
60  case 0:
61  {
62  uint32_t i, n;
63  uint8_t isNull;
64 
65  // Erase previous contents:
66  clear();
67 
68  in >> n;
69 
70  m_properties.resize(n);
71  for (i = 0; i < n; i++)
72  {
73  char nameBuf[1024];
74  // Name:
75  in >> nameBuf;
76  m_properties[i].name = nameBuf;
77 
78  // Object:
79  in >> isNull;
80 
81  if (isNull)
82  m_properties[i].value.reset(
83  static_cast<CSerializable*>(nullptr));
84  else
85  in >> m_properties[i].value;
86  }
87  }
88  break;
89  default:
91  };
92 }
93 
94 /*---------------------------------------------------------------
95  Constructor
96  ---------------------------------------------------------------*/
98 /*---------------------------------------------------------------
99  Destructor
100  ---------------------------------------------------------------*/
102 /*---------------------------------------------------------------
103  Copy Constructor
104  ---------------------------------------------------------------*/
106  : m_properties(o.m_properties)
107 {
109  it != m_properties.end(); ++it)
110  it->value.reset(dynamic_cast<CSerializable*>(it->value->clone()));
111 }
112 
113 /*---------------------------------------------------------------
114  Copy
115  ---------------------------------------------------------------*/
117  const CPropertiesValuesList& o)
118 {
119  if (this != &o) return *this;
120 
123  it != m_properties.end(); ++it)
124  it->value.reset(dynamic_cast<CSerializable*>(it->value->clone()));
125  return *this;
126 }
127 
128 /*---------------------------------------------------------------
129  clear
130  ---------------------------------------------------------------*/
132 {
133  MRPT_START
134  m_properties.clear();
135  MRPT_END
136 }
137 
138 /*---------------------------------------------------------------
139  get
140  ---------------------------------------------------------------*/
142  const std::string& propertyName) const
143 {
145  m_properties.begin();
146  it != m_properties.end(); ++it)
147  {
148  if (!os::_strcmpi(propertyName.c_str(), it->name.c_str()))
149  return it->value;
150  }
151  // Not found:
152  return CSerializable::Ptr();
153 }
154 
155 /*---------------------------------------------------------------
156  set
157  ---------------------------------------------------------------*/
159  const std::string& propertyName, const CSerializable::Ptr& obj)
160 {
161  MRPT_START
162 
164  it != m_properties.end(); ++it)
165  {
166  if (!os::_strcmpi(propertyName.c_str(), it->name.c_str()))
167  {
168  // Delete current contents:
169  // Copy new value:
170  if (!obj)
171  it->value.reset();
172  else
173  it->value = obj; //->clone();
174  return;
175  }
176  }
177 
178  // Insert:
179  TPropertyValuePair newPair;
180  newPair.name = std::string(propertyName);
181  newPair.value = obj;
182  m_properties.push_back(newPair);
183 
185  printf(
186  "Exception while setting annotation '%s'", propertyName.c_str()););
187 }
188 
189 /*---------------------------------------------------------------
190  size
191  ---------------------------------------------------------------*/
192 size_t CPropertiesValuesList::size() const { return m_properties.size(); }
193 /*---------------------------------------------------------------
194  getPropertyNames
195  ---------------------------------------------------------------*/
196 std::vector<std::string> CPropertiesValuesList::getPropertyNames() const
197 {
198  std::vector<std::string> ret;
199 
201  m_properties.begin();
202  it != m_properties.end(); ++it)
203  ret.push_back(it->name);
204 
205  return ret;
206 }
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...
CPropertiesValuesList & operator=(const CPropertiesValuesList &o)
Copy operator.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
std::vector< TPropertyValuePair > m_properties
The properties list: a map between strings and objects.
#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
std::vector< std::string > getPropertyNames() const
Returns the name of all properties in the list.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
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
size_t size() const
Returns the number of properties in the list.
unsigned char uint8_t
Definition: rptypes.h:41
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
An arbitrary list of "annotations", or named attributes, each being an instance of any CSerializable ...
#define MRPT_END
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
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
CSerializable::Ptr get(const std::string &propertyName) const
Returns the value of the property (case insensitive), or nullptr if it does not exist.
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
GLsizei const GLfloat * value
Definition: glext.h:4117
GLsizeiptr size
Definition: glext.h:3923
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 ...
unsigned __int32 uint32_t
Definition: rptypes.h:47
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