MRPT  2.0.4
CMHPropertiesValuesList.h
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 #pragma once
10 
13 #include <cstdio>
14 #include <cstring>
15 #include <sstream>
16 
17 namespace mrpt::hmtslam
18 {
19 /** Internal triplet for each property in utils::CMHPropertiesValuesList */
21 {
22  TPropertyValueIDTriplet() = default;
23  std::string name, basic_value;
25  int64_t ID{0};
26 };
27 
28 /** An arbitrary list of "annotations", or named attributes, each being an
29  * instance of any CSerializable object (Multi-hypotheses version).
30  * For each named annotation (or attribute), several values may exist, each
31  * associated to a given hypothesis ID.
32  * A non multi-hypotheses version exists in CPropertiesValuesList.
33  * \sa CSerializable, CPropertiesValuesList
34  * \ingroup mrpt_hmtslam_grp
35  */
37 {
39 
40  private:
41  std::vector<TPropertyValueIDTriplet> m_properties;
42 
43  public:
44  /** Default constructor
45  */
47 
48  /** Copy constructor
49  */
51 
52  /** Copy operator
53  */
55 
56  /** Destructor
57  */
58  ~CMHPropertiesValuesList() override;
59 
60  /** Clears the list and frees all object's memory.
61  */
62  void clear();
63 
64  /** Returns the value of the property (case insensitive) for some given
65  * hypothesis ID, or a nullptr smart pointer if it does not exist.
66  */
67  CSerializable::Ptr get(
68  const char* propertyName, const int64_t& hypothesis_ID) const;
69 
70  /** Returns the value of the property (case insensitive) for some given
71  * hypothesis ID checking its class in runtime, or a nullptr smart pointer
72  * if it does not exist.
73  */
74  template <typename T>
75  typename T::Ptr getAs(
76  const char* propertyName, const int64_t& hypothesis_ID,
77  bool allowNullPointer = true) const
78  {
80  CSerializable::Ptr obj = get(propertyName, hypothesis_ID);
81  if (!obj)
82  {
83  if (allowNullPointer)
84  return typename T::Ptr();
85  else
86  THROW_EXCEPTION("Null pointer");
87  }
88  const mrpt::rtti::TRuntimeClassId* class_ID =
89  &T::GetRuntimeClassIdStatic();
90  ASSERT_(class_ID == obj->GetRuntimeClass());
91  return std::dynamic_pointer_cast<T>(obj);
92  MRPT_END
93  }
94 
95  /** Returns the value of the property (case insensitive) for the first
96  * hypothesis ID found, or nullptr if it does not exist.
97  */
98  CSerializable::Ptr getAnyHypothesis(const char* propertyName) const;
99 
100  /** Sets/change the value of the property (case insensitive) for the given
101  * hypothesis ID, making a copy of the object (or setting it to nullptr if
102  * it is the passed value)
103  * \sa setMemoryReference
104  */
105  void set(
106  const char* propertyName, const CSerializable::Ptr& obj,
107  const int64_t& hypothesis_ID);
108 
109  /** Sets/change the value of the property (case insensitive) for the given
110  * hypothesis ID, directly replacing the pointer instead of making a copy of
111  * the object.
112  * \sa set
113  */
114  void setMemoryReference(
115  const char* propertyName, const CSerializable::Ptr& obj,
116  const int64_t& hypothesis_ID);
117 
118  /** Remove a given property, if it exists.
119  */
120  void remove(const char* propertyName, const int64_t& hypothesis_ID);
121 
122  /** Remove all the properties for the given hypothesis.
123  */
124  void removeAll(const int64_t& hypothesis_ID);
125 
126  /** Sets/change the value of a property (case insensitive) for the given
127  * hypothesis ID, from an elemental data type.
128  */
129  template <class T>
131  const char* propertyName, const T& data, const int64_t& hypothesis_ID)
132  {
133  MRPT_START
134 
135  std::string basic_value;
136  basic_value.resize(sizeof(T));
137  std::memcpy(&basic_value[0], &data, sizeof(T));
138 
139  for (auto& m_propertie : m_properties)
140  {
141  if (m_propertie.ID == hypothesis_ID &&
142  mrpt::system::strCmpI(propertyName, m_propertie.name))
143  {
144  // Delete current contents &
145  // Copy new value:
146  m_propertie.basic_value = basic_value;
147  return;
148  }
149  }
150 
151  // Insert as new:
152  TPropertyValueIDTriplet newPair;
153  newPair.name = std::string(propertyName);
154  newPair.basic_value = basic_value;
155  newPair.ID = hypothesis_ID;
156  m_properties.push_back(newPair);
157 
159  printf("Exception while setting annotation '%s'", propertyName););
160  }
161 
162  /** Gets the value of a property (case insensitive) for the given hypothesis
163  * ID, retrieves it as an elemental data type (types must coincide, basic
164  * size check is performed).
165  * \return false if the property does not exist for the given hypothesis.
166  */
167  template <class T>
169  const char* propertyName, T& out_data, const int64_t& hypothesis_ID,
170  bool raiseExceptionIfNotFound = false) const
171  {
172  MRPT_START
173  for (const auto& m_propertie : m_properties)
174  {
175  if (mrpt::system::strCmpI(propertyName, m_propertie.name) &&
176  m_propertie.ID == hypothesis_ID)
177  {
178  if (m_propertie.basic_value.size() != sizeof(out_data))
179  THROW_EXCEPTION("Data sizes do not match.");
180  out_data =
181  *reinterpret_cast<const T*>(&m_propertie.basic_value[0]);
182  return true;
183  }
184  }
185  // Not found:
186  if (raiseExceptionIfNotFound)
187  THROW_EXCEPTION_FMT("Property '%s' not found", propertyName);
188  return false;
189  MRPT_END
190  }
191 
192  /** Returns the name of all properties in the list
193  */
194  std::vector<std::string> getPropertyNames() const;
195 
196  using iterator = std::vector<TPropertyValueIDTriplet>::iterator;
197  using const_iterator = std::vector<TPropertyValueIDTriplet>::const_iterator;
198 
199  iterator begin() { return m_properties.begin(); }
200  const_iterator begin() const { return m_properties.begin(); }
201  iterator end() { return m_properties.end(); }
202  const_iterator end() const { return m_properties.end(); }
203  size_t size() const { return m_properties.size(); }
204 }; // End of class def.
205 } // namespace mrpt::hmtslam
mrpt::opengl::internal::data
static struct FontData data
Definition: gltext.cpp:144
mrpt::hmtslam::TPropertyValueIDTriplet::basic_value
std::string basic_value
Definition: CMHPropertiesValuesList.h:23
mrpt::hmtslam::CMHPropertiesValuesList::begin
const_iterator begin() const
Definition: CMHPropertiesValuesList.h:200
mrpt::rtti::TRuntimeClassId
A structure that holds runtime class type information.
Definition: CObject.h:31
mrpt::serialization::CSerializable::Ptr
std::shared_ptr< CSerializable > Ptr
Definition: CSerializable.h:36
mrpt::hmtslam::CMHPropertiesValuesList::getElemental
bool getElemental(const char *propertyName, T &out_data, const int64_t &hypothesis_ID, bool raiseExceptionIfNotFound=false) const
Gets the value of a property (case insensitive) for the given hypothesis ID, retrieves it as an eleme...
Definition: CMHPropertiesValuesList.h:168
DEFINE_SERIALIZABLE
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
Definition: CSerializable.h:152
string_utils.h
mrpt::hmtslam::CMHPropertiesValuesList::getAs
T::Ptr getAs(const char *propertyName, const int64_t &hypothesis_ID, bool allowNullPointer=true) const
Returns the value of the property (case insensitive) for some given hypothesis ID checking its class ...
Definition: CMHPropertiesValuesList.h:75
mrpt::hmtslam::CMHPropertiesValuesList::end
iterator end()
Definition: CMHPropertiesValuesList.h:201
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
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
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
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
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
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::end
const_iterator end() const
Definition: CMHPropertiesValuesList.h:202
MRPT_START
#define MRPT_START
Definition: exceptions.h:241
mrpt::hmtslam::CMHPropertiesValuesList::begin
iterator begin()
Definition: CMHPropertiesValuesList.h:199
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::setElemental
void setElemental(const char *propertyName, const T &data, const int64_t &hypothesis_ID)
Sets/change the value of a property (case insensitive) for the given hypothesis ID,...
Definition: CMHPropertiesValuesList.h:130
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::iterator
std::vector< TPropertyValueIDTriplet >::iterator iterator
Definition: CMHPropertiesValuesList.h:196
mrpt::hmtslam::CMHPropertiesValuesList::~CMHPropertiesValuesList
~CMHPropertiesValuesList() override
Destructor.
Definition: CMHPropertiesValuesList.cpp:93
mrpt::hmtslam::CMHPropertiesValuesList::size
size_t size() const
Definition: CMHPropertiesValuesList.h:203
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::const_iterator
std::vector< TPropertyValueIDTriplet >::const_iterator const_iterator
Definition: CMHPropertiesValuesList.h:197
mrpt::hmtslam::CMHPropertiesValuesList::m_properties
std::vector< TPropertyValueIDTriplet > m_properties
Definition: CMHPropertiesValuesList.h:41
CSerializable.h
mrpt::hmtslam::TPropertyValueIDTriplet::TPropertyValueIDTriplet
TPropertyValueIDTriplet()=default
mrpt::system::strCmpI
bool strCmpI(const std::string &s1, const std::string &s2)
Return true if the two strings are equal (case insensitive)
Definition: string_utils.cpp:305
mrpt::system::os::memcpy
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".



Page generated by Doxygen 1.8.17 for MRPT 2.0.4 at Sun Jul 19 17:54:30 UTC 2020