any_poly.h
Go to the documentation of this file.
1 
26 #ifndef TESSERACT_COMMON_ANY_POLY_H
27 #define TESSERACT_COMMON_ANY_POLY_H
30 #include <boost/serialization/access.hpp>
31 #include <boost/serialization/export.hpp>
32 #include <boost/stacktrace.hpp>
33 #include <boost/core/demangle.hpp>
35 
36 #include <memory>
37 #include <typeindex>
38 #include <unordered_map>
40 
41 namespace tesseract_common
42 {
44 {
45 public:
46  virtual ~AnyInterface() = default;
47 
52  virtual std::type_index getType() const;
53 
58  virtual std::unique_ptr<AnyInterface> clone() const = 0;
59 
60  // Operators
61  bool operator==(const AnyInterface& rhs) const;
62  bool operator!=(const AnyInterface& rhs) const;
63 
64 protected:
70  virtual bool equals(const AnyInterface& other) const = 0;
71 
72 private:
75  template <class Archive>
76  void serialize(Archive& ar, const unsigned int version); // NOLINT
77 };
78 
79 template <typename T>
80 class AnyWrapper : public AnyInterface
81 {
82 public:
83  AnyWrapper() = default;
84  explicit AnyWrapper(const T& value_) : value(value_) {}
85  explicit AnyWrapper(T&& value_) : value(std::move(value_)) {}
86 
87  std::type_index getType() const override final { return typeid(T); }
88 
89  std::unique_ptr<AnyInterface> clone() const override final { return std::make_unique<AnyWrapper<T>>(value); }
90 
91  bool equals(const AnyInterface& other) const override final
92  {
93  const auto* derived_other = dynamic_cast<const AnyWrapper<T>*>(&other);
94  if (derived_other == nullptr)
95  return false;
96 
97  return (value == derived_other->value);
98  }
99 
100  T value{};
101 
102 private:
105  template <class Archive>
106  void serialize(Archive& ar, const unsigned int /*version*/)
107  {
108  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(AnyInterface);
109  ar& boost::serialization::make_nvp("value", value);
110  }
111 };
112 
113 class AnyPoly
114 {
115 public:
116  AnyPoly() = default; // Default constructor
117  ~AnyPoly() = default; // Default destructor
118  AnyPoly(const AnyPoly& other);
119  AnyPoly& operator=(const AnyPoly& other);
120  AnyPoly(AnyPoly&& other) noexcept = default;
121  AnyPoly& operator=(AnyPoly&& other) noexcept = default;
122  AnyPoly(const AnyInterface& impl);
123 
125  template <typename T,
126  typename std::enable_if_t<!std::is_base_of_v<AnyInterface, T> && !std::is_same_v<std::decay_t<T>, AnyPoly>,
127  bool> = true>
128  AnyPoly(T&& value) : impl_(std::make_unique<AnyWrapper<std::decay_t<T>>>(std::forward<T>(value)))
129  {
130  }
131 
132  template <typename T,
133  typename std::enable_if_t<!std::is_base_of_v<AnyInterface, T> && !std::is_same_v<std::decay_t<T>, AnyPoly>,
134  bool> = true>
135  AnyPoly& operator=(T&& value)
136  {
137  impl_ = std::make_unique<AnyWrapper<std::decay_t<T>>>(std::forward<T>(value));
138  return *this;
139  }
140 
142  template <typename T,
143  typename std::enable_if_t<!std::is_base_of_v<AnyInterface, T> && !std::is_same_v<std::decay_t<T>, AnyPoly>,
144  bool> = true>
145  AnyPoly(const T& value) : impl_(std::make_unique<AnyWrapper<T>>(value))
146  {
147  }
148 
150  template <typename T,
151  typename std::enable_if_t<!std::is_base_of_v<AnyInterface, T> && !std::is_same_v<std::decay_t<T>, AnyPoly>,
152  bool> = true>
153  AnyPoly& operator=(const T& value)
154  {
155  impl_ = std::make_unique<AnyWrapper<T>>(value);
156  return *this;
157  }
158 
163  std::type_index getType() const;
164 
169  bool isNull() const;
170 
176  AnyInterface& get();
177  const AnyInterface& get() const;
178 
179  // Type Casting
180  template <typename T, typename std::enable_if_t<std::is_base_of_v<AnyInterface, T>, bool> = true>
181  T& as()
182  {
183  if (getType() != typeid(T))
184  throw std::runtime_error("AnyPoly, tried to cast '" + boost::core::demangle(getType().name()) + "' to '" +
185  boost::core::demangle(typeid(T).name()) + "'\nBacktrace:\n" +
186  boost::stacktrace::to_string(boost::stacktrace::stacktrace()) + "\n");
187 
188  return *dynamic_cast<T*>(impl_.get());
189  }
190 
191  template <typename T, typename std::enable_if_t<!std::is_base_of_v<AnyInterface, T>, bool> = true>
192  T& as()
193  {
194  if (getType() != typeid(T))
195  throw std::runtime_error("AnyPoly, tried to cast '" + boost::core::demangle(getType().name()) + "' to '" +
196  boost::core::demangle(typeid(T).name()) + "'\nBacktrace:\n" +
197  boost::stacktrace::to_string(boost::stacktrace::stacktrace()) + "\n");
198 
199  return dynamic_cast<AnyWrapper<T>*>(impl_.get())->value;
200  }
201 
202  template <typename T, typename std::enable_if_t<std::is_base_of_v<AnyInterface, T>, bool> = true>
203  const T& as() const
204  {
205  if (getType() != typeid(T))
206  throw std::runtime_error("AnyPoly, tried to cast '" + boost::core::demangle(getType().name()) + "' to '" +
207  boost::core::demangle(typeid(T).name()) + "'\nBacktrace:\n" +
208  boost::stacktrace::to_string(boost::stacktrace::stacktrace()) + "\n");
209 
210  return *dynamic_cast<const T*>(impl_.get());
211  }
212 
213  template <typename T, typename std::enable_if_t<!std::is_base_of_v<AnyInterface, T>, bool> = true>
214  const T& as() const
215  {
216  if (getType() != typeid(T))
217  throw std::runtime_error("AnyPoly, tried to cast '" + boost::core::demangle(getType().name()) + "' to '" +
218  boost::core::demangle(typeid(T).name()) + "'\nBacktrace:\n" +
219  boost::stacktrace::to_string(boost::stacktrace::stacktrace()) + "\n");
220 
221  return dynamic_cast<const AnyWrapper<T>*>(impl_.get())->value;
222  }
223 
224  // Operators
225  bool operator==(const AnyPoly& rhs) const;
226  bool operator!=(const AnyPoly& rhs) const;
227 
228 private:
229  std::unique_ptr<AnyInterface> impl_;
230 
233  template <class Archive>
234  void serialize(Archive& ar, const unsigned int version); // NOLINT
235 };
236 
244 
252 
253 } // namespace tesseract_common
254 
255 BOOST_SERIALIZATION_ASSUME_ABSTRACT(tesseract_common::AnyInterface)
256 BOOST_CLASS_TRACKING(tesseract_common::AnyInterface, boost::serialization::track_never)
257 
258 BOOST_CLASS_EXPORT_KEY(tesseract_common::AnyPoly)
259 BOOST_CLASS_TRACKING(tesseract_common::AnyPoly, boost::serialization::track_never)
260 
261 BOOST_CLASS_EXPORT_KEY(tesseract_common::BoolAnyPoly)
262 BOOST_CLASS_TRACKING(tesseract_common::BoolAnyPoly, boost::serialization::track_never)
263 
264 BOOST_CLASS_EXPORT_KEY(tesseract_common::IntAnyPoly)
265 BOOST_CLASS_TRACKING(tesseract_common::IntAnyPoly, boost::serialization::track_never)
266 
267 BOOST_CLASS_EXPORT_KEY(tesseract_common::UnsignedAnyPoly)
268 BOOST_CLASS_TRACKING(tesseract_common::UnsignedAnyPoly, boost::serialization::track_never)
269 
270 BOOST_CLASS_EXPORT_KEY(tesseract_common::DoubleAnyPoly)
271 BOOST_CLASS_TRACKING(tesseract_common::DoubleAnyPoly, boost::serialization::track_never)
272 
273 BOOST_CLASS_EXPORT_KEY(tesseract_common::FloatAnyPoly)
274 BOOST_CLASS_TRACKING(tesseract_common::FloatAnyPoly, boost::serialization::track_never)
275 
276 BOOST_CLASS_EXPORT_KEY(tesseract_common::StringAnyPoly)
277 BOOST_CLASS_TRACKING(tesseract_common::StringAnyPoly, boost::serialization::track_never)
278 
279 BOOST_CLASS_EXPORT_KEY(tesseract_common::SizeTAnyPoly)
280 BOOST_CLASS_TRACKING(tesseract_common::SizeTAnyPoly, boost::serialization::track_never)
281 
282 BOOST_CLASS_EXPORT_KEY(tesseract_common::UMapStringStringAnyPoly)
283 BOOST_CLASS_TRACKING(tesseract_common::UMapStringStringAnyPoly, boost::serialization::track_never)
284 
285 BOOST_CLASS_EXPORT_KEY(tesseract_common::UMapStringBoolAnyPoly)
286 BOOST_CLASS_TRACKING(tesseract_common::UMapStringBoolAnyPoly, boost::serialization::track_never)
287 
288 BOOST_CLASS_EXPORT_KEY(tesseract_common::UMapStringIntAnyPoly)
289 BOOST_CLASS_TRACKING(tesseract_common::UMapStringIntAnyPoly, boost::serialization::track_never)
290 
291 BOOST_CLASS_EXPORT_KEY(tesseract_common::UMapStringUnsignedAnyPoly)
292 BOOST_CLASS_TRACKING(tesseract_common::UMapStringUnsignedAnyPoly, boost::serialization::track_never)
293 
294 BOOST_CLASS_EXPORT_KEY(tesseract_common::UMapStringDoubleAnyPoly)
295 BOOST_CLASS_TRACKING(tesseract_common::UMapStringDoubleAnyPoly, boost::serialization::track_never)
296 
297 BOOST_CLASS_EXPORT_KEY(tesseract_common::UMapStringFloatAnyPoly)
298 BOOST_CLASS_TRACKING(tesseract_common::UMapStringFloatAnyPoly, boost::serialization::track_never)
299 
300 BOOST_CLASS_EXPORT_KEY(tesseract_common::UMapStringSizeTAnyPoly)
301 BOOST_CLASS_TRACKING(tesseract_common::UMapStringSizeTAnyPoly, boost::serialization::track_never)
302 
303 #endif // TESSERACT_COMMON_ANY_POLY_H
tesseract_common::AnyPoly::operator=
AnyPoly & operator=(T &&value)
Definition: any_poly.h:135
tesseract_common::AnyWrapper::serialize
void serialize(Archive &ar, const unsigned int)
Definition: any_poly.h:106
tesseract_common::AnyInterface::getType
virtual std::type_index getType() const
Get the type index of the object stored.
Definition: any_poly.cpp:41
tesseract_common::AnyWrapper::value
T value
Definition: any_poly.h:100
tesseract_common::AnyPoly::get
AnyInterface & get()
Get the instruction being stored.
Definition: any_poly.cpp:80
tesseract_common
Definition: allowed_collision_matrix.h:19
tesseract_common::AnyWrapper::getType
std::type_index getType() const override final
Get the type index of the object stored.
Definition: any_poly.h:87
tesseract_common::AnyInterface::serialize
void serialize(Archive &ar, const unsigned int version)
Definition: any_poly.cpp:51
tesseract_common::AnyPoly::operator!=
bool operator!=(const AnyPoly &rhs) const
Definition: any_poly.cpp:97
tesseract_common::AnyWrapper::access
friend class boost::serialization::access
Definition: any_poly.h:103
tesseract_common::AnyPoly::access
friend class boost::serialization::access
Definition: any_poly.h:231
tesseract_common::AnyWrapper::AnyWrapper
AnyWrapper(const T &value_)
Definition: any_poly.h:84
macros.h
Common Tesseract Macros.
tesseract_common::AnyPoly::operator==
bool operator==(const AnyPoly &rhs) const
Definition: any_poly.cpp:83
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
Definition: macros.h:71
tesseract_common::AnyWrapper
Definition: any_poly.h:80
tesseract_common::AnyInterface::clone
virtual std::unique_ptr< AnyInterface > clone() const =0
Make a deep copy of the object.
tesseract_common::AnyPoly::AnyPoly
AnyPoly(const T &value)
Definition: any_poly.h:145
tesseract_common::Serialization
Definition: serialization.h:97
tesseract_common::AnyWrapper::clone
std::unique_ptr< AnyInterface > clone() const override final
Make a deep copy of the object.
Definition: any_poly.h:89
tesseract_common::AnyInterface::access
friend class boost::serialization::access
Definition: any_poly.h:73
tesseract_common::AnyPoly::as
const T & as() const
Definition: any_poly.h:203
tesseract_common::AnyPoly::serialize
void serialize(Archive &ar, const unsigned int version)
Definition: any_poly.cpp:101
tesseract_common::AnyPoly
Definition: any_poly.h:113
tesseract_common::AnyInterface
Definition: any_poly.h:43
tesseract_common::AnyPoly::getType
std::type_index getType() const
Get the stored derived type.
Definition: any_poly.cpp:71
tesseract_common::AnyInterface::equals
virtual bool equals(const AnyInterface &other) const =0
Check if two objects are equal.
tesseract_common::AnyInterface::~AnyInterface
virtual ~AnyInterface()=default
tesseract_common::AnyPoly::impl_
std::unique_ptr< AnyInterface > impl_
Definition: any_poly.h:229
tesseract_common::AnyPoly::~AnyPoly
~AnyPoly()=default
tesseract_common::AnyPoly::as
T & as()
Definition: any_poly.h:181
tesseract_common::AnyPoly::operator=
AnyPoly & operator=(const AnyPoly &other)
Definition: any_poly.cpp:61
tesseract_common::AnyInterface::operator!=
bool operator!=(const AnyInterface &rhs) const
Definition: any_poly.cpp:47
tesseract_common::AnyWrapper::AnyWrapper
AnyWrapper(T &&value_)
Definition: any_poly.h:85
TESSERACT_COMMON_IGNORE_WARNINGS_POP
#define TESSERACT_COMMON_IGNORE_WARNINGS_POP
Definition: macros.h:72
tesseract_common::AnyWrapper::AnyWrapper
AnyWrapper()=default
serialization.h
Additional Boost serialization wrappers.
tesseract_common::AnyWrapper::equals
bool equals(const AnyInterface &other) const override final
Check if two objects are equal.
Definition: any_poly.h:91
tesseract_common::AnyPoly::AnyPoly
AnyPoly()=default
tesseract_common::AnyPoly::AnyPoly
AnyPoly(T &&value)
Definition: any_poly.h:128
tesseract_common::AnyInterface::operator==
bool operator==(const AnyInterface &rhs) const
Definition: any_poly.cpp:44
tesseract_common::AnyPoly::operator=
AnyPoly & operator=(const T &value)
Definition: any_poly.h:153
tesseract_common::AnyPoly::isNull
bool isNull() const
Check if the poly type is null.
Definition: any_poly.cpp:79


tesseract_common
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:01:40