MRPT  2.0.4
deepcopy_poly_ptr.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 
11 #include <cstdlib>
12 #include <stdexcept>
13 
14 namespace mrpt::containers
15 {
16 /** \addtogroup mrpt_containers_grp
17  * @{ */
18 
19 /** Wrapper to a std::shared_ptr<>, adding deep-copy semantics to copy ctor and
20  * copy operator, suitable for polymorphic classes with a `clone()` method.
21  * Example use: `deepcopy_poly_ptr<mrpt::poses::CPosePDF::Ptr>`
22  * \sa for non-virtual classes, see copy_ptr<T>
23  */
24 template <typename T>
26 {
27  public:
28  /** Ctor from a smart pointer; makes deep copy. */
29  deepcopy_poly_ptr(const T& ptr)
30  {
31  m_smartptr.reset(dynamic_cast<typename T::element_type*>(ptr->clone()));
32  }
33  /** Default ctor; init to nullptr. */
34  deepcopy_poly_ptr() = default;
35  /** copy ctor: makes a copy of the object via `clone()` */
37  {
38  m_smartptr.reset(
39  dynamic_cast<typename T::element_type*>(o.m_smartptr->clone()));
40  }
42  {
43  if (this == &o) return *this;
44  m_smartptr.reset(
45  dynamic_cast<typename T::element_type*>(o.m_smartptr->clone()));
46  return *this;
47  }
49  {
50  m_smartptr.reset(
51  dynamic_cast<typename T::element_type*>(o_ptr->clone()));
52  return *this;
53  }
54  /** move ctor */
56  {
57  m_smartptr = o.m_smartptr;
58  o.m_smartptr.reset();
59  }
60  /** move operator */
62  {
63  if (this == &o) return *this;
64  m_smartptr = o.m_smartptr;
65  o.m_smartptr.reset();
66  return *this;
67  }
68  ~deepcopy_poly_ptr() = default;
69  typename T::element_type* get()
70  {
71  if (m_smartptr)
72  return m_smartptr.get();
73  else
74  throw std::runtime_error("dereferencing nullptr poly_ptr");
75  }
76  const typename T::element_type* get() const
77  {
78  if (m_smartptr)
79  return m_smartptr.get();
80  else
81  throw std::runtime_error("dereferencing nullptr poly_ptr");
82  }
83 
84  typename T::element_type* operator->() { return get(); }
85  const typename T::element_type* operator->() const { return get(); }
86  typename T::element_type& operator*(void) { return *get(); }
87  const typename T::element_type& operator*() const { return *get(); }
88  operator bool() const { return m_smartptr ? true : false; }
89  bool operator!(void) const { return m_smartptr ? false : true; }
90  const T& get_ptr() const { return m_smartptr; }
91  T& get_ptr() { return m_smartptr; }
92  void reset() { m_smartptr.reset(); }
93 
94  private:
96 };
97 
98 /** @} */ // end of grouping
99 } // namespace mrpt::containers
Wrapper to a std::shared_ptr<>, adding deep-copy semantics to copy ctor and copy operator, suitable for polymorphic classes with a clone() method.
deepcopy_poly_ptr(deepcopy_poly_ptr &&o)
move ctor
deepcopy_poly_ptr< T > & operator=(const deepcopy_poly_ptr< T > &o)
const T::element_type & operator*() const
deepcopy_poly_ptr(const T &ptr)
Ctor from a smart pointer; makes deep copy.
deepcopy_poly_ptr< T > & operator=(deepcopy_poly_ptr< T > &&o)
move operator
deepcopy_poly_ptr(const deepcopy_poly_ptr< T > &o)
copy ctor: makes a copy of the object via clone()
const T::element_type * operator->() const
deepcopy_poly_ptr< T > & operator=(const T &o_ptr)
deepcopy_poly_ptr()=default
Default ctor; init to nullptr.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020