MRPT  2.0.3
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 }
os.h
mrpt::containers::clear
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:183
mrpt::serialization::CSerializable::Ptr
std::shared_ptr< CSerializable > Ptr
Definition: CSerializable.h:36
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:177
MRPT_END_WITH_CLEAN_UP
#define MRPT_END_WITH_CLEAN_UP(stuff)
Definition: exceptions.h:247
mrpt::hmtslam::CMHPropertiesValuesList::removeAll
void removeAll(const int64_t &hypothesis_ID)
Remove all the properties for the given hypothesis.
Definition: CMHPropertiesValuesList.cpp:246
out
mrpt::vision::TStereoCalibResults out
Definition: chessboard_stereo_camera_calib_unittest.cpp:25
mrpt::hmtslam::CMHPropertiesValuesList::remove
void remove(const char *propertyName, const int64_t &hypothesis_ID)
Remove a given property, if it exists.
Definition: CMHPropertiesValuesList.cpp:232
mrpt::hmtslam::TPropertyValueIDTriplet::ID
int64_t ID
Definition: CMHPropertiesValuesList.h:25
mrpt::hmtslam::CMHPropertiesValuesList::clear
void clear()
Clears the list and frees all object's memory.
Definition: CMHPropertiesValuesList.cpp:97
mrpt::hmtslam::TPropertyValueIDTriplet::value
mrpt::serialization::CSerializable::Ptr value
Definition: CMHPropertiesValuesList.h:24
hmtslam-precomp.h
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
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:127
mrpt::hmtslam::TPropertyValueIDTriplet::name
std::string name
Definition: CMHPropertiesValuesList.h:23
mrpt::hmtslam
Classes related to the implementation of Hybrid Metric Topological (HMT) SLAM.
Definition: CHierarchicalMapMHPartition.h:27
CMHPropertiesValuesList.h
mrpt::hmtslam::CMHPropertiesValuesList::getPropertyNames
std::vector< std::string > getPropertyNames() const
Returns the name of all properties in the list.
Definition: CMHPropertiesValuesList.cpp:208
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:241
mrpt::hmtslam::CMHPropertiesValuesList::operator=
CMHPropertiesValuesList & operator=(const CMHPropertiesValuesList &o)
Copy operator.
Definition: CMHPropertiesValuesList.cpp:270
mrpt::serialization::CSerializable
The virtual base class which provides a unified interface for all persistent objects in MRPT.
Definition: CSerializable.h:30
mrpt::hmtslam::CMHPropertiesValuesList
An arbitrary list of "annotations", or named attributes, each being an instance of any CSerializable ...
Definition: CMHPropertiesValuesList.h:36
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)
To be added to all CSerializable-classes implementation files.
Definition: CSerializable.h:166
mrpt::serialization
Definition: aligned_serialization.h:13
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:107
mrpt::hmtslam::CMHPropertiesValuesList::~CMHPropertiesValuesList
~CMHPropertiesValuesList() override
Destructor.
Definition: CMHPropertiesValuesList.cpp:93
mrpt::hmtslam::CMHPropertiesValuesList::CMHPropertiesValuesList
CMHPropertiesValuesList()
Default constructor.
mrpt::hmtslam::TPropertyValueIDTriplet
Internal triplet for each property in utils::CMHPropertiesValuesList.
Definition: CMHPropertiesValuesList.h:20
MRPT_END
#define MRPT_END
Definition: exceptions.h:245
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:142
mrpt::hmtslam::CMHPropertiesValuesList::m_properties
std::vector< TPropertyValueIDTriplet > m_properties
Definition: CMHPropertiesValuesList.h:41
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
mrpt::system::os::_strcmpi
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.
Definition: os.cpp:320
mrpt::system
Definition: backtrace.h:14



Page generated by Doxygen 1.8.17 for MRPT 2.0.3 at Thu May 21 21:53:32 UTC 2020