MRPT  2.0.1
CPropertiesValuesList.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "hmtslam-precomp.h" // Precompiled headers
11 
14 #include <mrpt/system/os.h>
15 #include <cstdio>
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(
47  mrpt::serialization::CArchive& in, uint8_t version)
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  // Name:
65  in >> m_properties[i].name;
66 
67  // Object:
68  in >> isNull;
69 
70  if (isNull)
71  m_properties[i].value.reset(
72  static_cast<CSerializable*>(nullptr));
73  else
74  in >> m_properties[i].value;
75  }
76  }
77  break;
78  default:
80  };
81 }
82 
83 /*---------------------------------------------------------------
84  Constructor
85  ---------------------------------------------------------------*/
86 CPropertiesValuesList::CPropertiesValuesList() = default;
87 /*---------------------------------------------------------------
88  Destructor
89  ---------------------------------------------------------------*/
90 CPropertiesValuesList::~CPropertiesValuesList() { clear(); }
91 /*---------------------------------------------------------------
92  Copy Constructor
93  ---------------------------------------------------------------*/
94 CPropertiesValuesList::CPropertiesValuesList(const CPropertiesValuesList& o)
95  : m_properties(o.m_properties)
96 {
97  for (auto& m_propertie : m_properties)
98  m_propertie.value.reset(
99  dynamic_cast<CSerializable*>(m_propertie.value->clone()));
100 }
101 
102 /*---------------------------------------------------------------
103  Copy
104  ---------------------------------------------------------------*/
106  const CPropertiesValuesList& o)
107 {
108  if (this != &o) return *this;
109 
111  for (auto& m_propertie : m_properties)
112  m_propertie.value.reset(
113  dynamic_cast<CSerializable*>(m_propertie.value->clone()));
114  return *this;
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 std::string& propertyName) const
132 {
133  for (const auto& m_propertie : m_properties)
134  {
135  if (!os::_strcmpi(propertyName.c_str(), m_propertie.name.c_str()))
136  return m_propertie.value;
137  }
138  // Not found:
139  return CSerializable::Ptr();
140 }
141 
142 /*---------------------------------------------------------------
143  set
144  ---------------------------------------------------------------*/
146  const std::string& propertyName, const CSerializable::Ptr& obj)
147 {
148  MRPT_START
149 
150  for (auto& m_propertie : m_properties)
151  {
152  if (!os::_strcmpi(propertyName.c_str(), m_propertie.name.c_str()))
153  {
154  // Delete current contents:
155  // Copy new value:
156  if (!obj)
157  m_propertie.value.reset();
158  else
159  m_propertie.value = obj; //->clone();
160  return;
161  }
162  }
163 
164  // Insert:
165  TPropertyValuePair newPair;
166  newPair.name = std::string(propertyName);
167  newPair.value = obj;
168  m_properties.push_back(newPair);
169 
171  printf(
172  "Exception while setting annotation '%s'", propertyName.c_str()););
173 }
174 
175 /*---------------------------------------------------------------
176  size
177  ---------------------------------------------------------------*/
178 size_t CPropertiesValuesList::size() const { return m_properties.size(); }
179 /*---------------------------------------------------------------
180  getPropertyNames
181  ---------------------------------------------------------------*/
182 std::vector<std::string> CPropertiesValuesList::getPropertyNames() const
183 {
184  std::vector<std::string> ret;
185 
186  for (const auto& m_propertie : m_properties)
187  ret.push_back(m_propertie.name);
188 
189  return ret;
190 }
#define MRPT_START
Definition: exceptions.h:241
std::vector< std::string > getPropertyNames() const
Returns the name of all properties in the list.
size_t size(const MATRIXLIKE &m, const int dim)
Classes related to the implementation of Hybrid Metric Topological (HMT) SLAM.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
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:247
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
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:54
mrpt::vision::TStereoCalibResults out
#define MRPT_END
Definition: exceptions.h:245
size_t size() const
Returns the number of properties in the list.
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
CPropertiesValuesList & operator=(const CPropertiesValuesList &o)
Copy operator.
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:183
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 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020