MRPT  1.9.9
CMHPropertiesValuesList.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 
13 #include <mrpt/system/os.h>
14 #include <cstdio>
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 
48  mrpt::serialization::CArchive& in, uint8_t version)
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  // Name:
66  in >> m_properties[i].name;
67 
68  // Object:
69  in >> isNull;
70 
71  if (isNull)
72  m_properties[i].value.reset();
73  else
74  in >> m_properties[i].value;
75 
76  // Hypot. ID:
77  in >> m_properties[i].ID;
78  }
79  }
80  break;
81  default:
83  };
84 }
85 
86 /*---------------------------------------------------------------
87  Constructor
88  ---------------------------------------------------------------*/
90 /*---------------------------------------------------------------
91  Destructor
92  ---------------------------------------------------------------*/
94 /*---------------------------------------------------------------
95  clear
96  ---------------------------------------------------------------*/
98 {
100  m_properties.clear();
101  MRPT_END
102 }
103 
104 /*---------------------------------------------------------------
105  get
106  ---------------------------------------------------------------*/
108  const char* propertyName, const int64_t& hypothesis_ID) const
109 {
110  std::vector<TPropertyValueIDTriplet>::const_iterator it;
111  for (it = m_properties.begin(); it != m_properties.end(); ++it)
112  if (!os::_strcmpi(propertyName, it->name.c_str()) &&
113  it->ID == hypothesis_ID)
114  return it->value;
115 
116  for (it = m_properties.begin(); it != m_properties.end(); ++it)
117  if (!os::_strcmpi(propertyName, it->name.c_str()) && it->ID == 0)
118  return it->value;
119 
120  // Not found:
121  return CSerializable::Ptr();
122 }
123 
124 /*---------------------------------------------------------------
125  getAnyHypothesis
126  ---------------------------------------------------------------*/
128  const char* propertyName) const
129 {
130  for (const auto& m_propertie : m_properties)
131  {
132  if (!os::_strcmpi(propertyName, m_propertie.name.c_str()))
133  return m_propertie.value;
134  }
135  // Not found:
136  return CSerializable::Ptr();
137 }
138 
139 /*---------------------------------------------------------------
140  set
141  ---------------------------------------------------------------*/
143  const char* propertyName, const CSerializable::Ptr& obj,
144  const int64_t& hypothesis_ID)
145 {
146  MRPT_START
147 
148  for (auto& m_propertie : m_properties)
149  {
150  if (m_propertie.ID == hypothesis_ID &&
151  !os::_strcmpi(propertyName, m_propertie.name.c_str()))
152  {
153  // Delete current contents:
154  // Copy new value:
155  m_propertie.value.reset(dynamic_cast<CSerializable*>(obj->clone()));
156 
157  // if (!obj) it->value.clear();
158  // else it->value = obj; //->clone();
159  return;
160  }
161  }
162 
163  // Insert:
164  TPropertyValueIDTriplet newPair;
165  newPair.name = std::string(propertyName);
166  newPair.value = obj;
167  newPair.ID = hypothesis_ID;
168  m_properties.push_back(newPair);
169 
171  printf("Exception while setting annotation '%s'", propertyName););
172 }
173 
174 /*---------------------------------------------------------------
175  setMemoryReference
176  ---------------------------------------------------------------*/
178  const char* propertyName, const CSerializable::Ptr& obj,
179  const int64_t& hypothesis_ID)
180 {
181  MRPT_START
182 
183  for (auto& m_propertie : m_properties)
184  {
185  if (m_propertie.ID == hypothesis_ID &&
186  !os::_strcmpi(propertyName, m_propertie.name.c_str()))
187  {
188  // Delete current contents & set a copy of the same smart pointer:
189  m_propertie.value = obj;
190  return;
191  }
192  }
193 
194  // Insert:
195  TPropertyValueIDTriplet newPair;
196  newPair.name = std::string(propertyName);
197  newPair.value = obj;
198  newPair.ID = hypothesis_ID;
199  m_properties.push_back(newPair);
200 
202  printf("Exception while setting annotation '%s'", propertyName););
203 }
204 
205 /*---------------------------------------------------------------
206  getPropertyNames
207  ---------------------------------------------------------------*/
208 std::vector<std::string> CMHPropertiesValuesList::getPropertyNames() const
209 {
210  std::vector<std::string> ret;
211 
212  for (const auto& m_propertie : m_properties)
213  {
214  bool isNew = true;
215  for (auto& itS : ret)
216  {
217  if (itS == m_propertie.name)
218  {
219  isNew = false;
220  break;
221  }
222  }
223  if (isNew) ret.push_back(m_propertie.name); // Yes, it is new:
224  }
225 
226  return ret;
227 }
228 
229 /*---------------------------------------------------------------
230  remove
231  ---------------------------------------------------------------*/
233  const char* propertyName, const int64_t& hypothesis_ID)
234 {
235  for (auto it = m_properties.begin(); it != m_properties.end();)
236  if (!os::_strcmpi(propertyName, it->name.c_str()) &&
237  it->ID == hypothesis_ID)
238  it = m_properties.erase(it);
239  else
240  ++it;
241 }
242 
243 /*---------------------------------------------------------------
244  removeAll
245  ---------------------------------------------------------------*/
246 void CMHPropertiesValuesList::removeAll(const int64_t& hypothesis_ID)
247 {
248  for (auto it = m_properties.begin(); it != m_properties.end();)
249  if (it->ID == hypothesis_ID)
250  it = m_properties.erase(it);
251  else
252  ++it;
253 }
254 
255 /*---------------------------------------------------------------
256  Copy
257  ---------------------------------------------------------------*/
259  const CMHPropertiesValuesList& o)
260  : m_properties(o.m_properties)
261 {
262  for (auto& m_propertie : m_properties)
263  m_propertie.value.reset(
264  dynamic_cast<CSerializable*>(m_propertie.value->clone()));
265 }
266 
267 /*---------------------------------------------------------------
268  Copy
269  ---------------------------------------------------------------*/
271  const CMHPropertiesValuesList& o)
272 {
273  if (this == &o) return *this;
274 
276 
277  for (auto& m_propertie : m_properties)
278  m_propertie.value.reset(
279  dynamic_cast<CSerializable*>(m_propertie.value->clone()));
280  return *this;
281 }
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...
#define MRPT_START
Definition: exceptions.h:241
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
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 serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
#define MRPT_END_WITH_CLEAN_UP(stuff)
Definition: exceptions.h:247
void removeAll(const int64_t &hypothesis_ID)
Remove all the properties for the given hypothesis.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
CMHPropertiesValuesList()
Default constructor.
CSerializable::Ptr getAnyHypothesis(const char *propertyName) const
Returns the value of the property (case insensitive) for the first hypothesis ID found, or nullptr if it does not exist.
void clear()
Clears the list and frees all object&#39;s memory.
void remove(const char *propertyName, const int64_t &hypothesis_ID)
Remove a given property, if it exists.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
mrpt::vision::TStereoCalibResults out
CMHPropertiesValuesList & operator=(const CMHPropertiesValuesList &o)
Copy operator.
#define MRPT_END
Definition: exceptions.h:245
Internal triplet for each property in utils::CMHPropertiesValuesList.
mrpt::serialization::CSerializable::Ptr value
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
An arbitrary list of "annotations", or named attributes, each being an instance of any CSerializable ...
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...
std::vector< std::string > getPropertyNames() const
Returns the name of all properties in the list.
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, or a nullptr smart pointer if it does not exist.
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:183
std::vector< TPropertyValueIDTriplet > m_properties
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: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020