MRPT  2.0.3
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
mrpt::containers::deepcopy_poly_ptr::reset
void reset()
Definition: deepcopy_poly_ptr.h:92
mrpt::containers::deepcopy_poly_ptr::deepcopy_poly_ptr
deepcopy_poly_ptr(deepcopy_poly_ptr &&o)
move ctor
Definition: deepcopy_poly_ptr.h:55
mrpt::containers::deepcopy_poly_ptr::operator=
deepcopy_poly_ptr< T > & operator=(const deepcopy_poly_ptr< T > &o)
Definition: deepcopy_poly_ptr.h:41
mrpt::containers::deepcopy_poly_ptr::get
T::element_type * get()
Definition: deepcopy_poly_ptr.h:69
mrpt::containers::deepcopy_poly_ptr::deepcopy_poly_ptr
deepcopy_poly_ptr(const T &ptr)
Ctor from a smart pointer; makes deep copy.
Definition: deepcopy_poly_ptr.h:29
mrpt::containers::deepcopy_poly_ptr::operator*
const T::element_type & operator*() const
Definition: deepcopy_poly_ptr.h:87
mrpt::containers::deepcopy_poly_ptr::operator*
T::element_type & operator*(void)
Definition: deepcopy_poly_ptr.h:86
mrpt::containers::deepcopy_poly_ptr::operator=
deepcopy_poly_ptr< T > & operator=(deepcopy_poly_ptr< T > &&o)
move operator
Definition: deepcopy_poly_ptr.h:61
mrpt::containers::deepcopy_poly_ptr::deepcopy_poly_ptr
deepcopy_poly_ptr(const deepcopy_poly_ptr< T > &o)
copy ctor: makes a copy of the object via clone()
Definition: deepcopy_poly_ptr.h:36
mrpt::containers::deepcopy_poly_ptr
Wrapper to a std::shared_ptr<>, adding deep-copy semantics to copy ctor and copy operator,...
Definition: deepcopy_poly_ptr.h:25
mrpt::containers::deepcopy_poly_ptr::operator->
T::element_type * operator->()
Definition: deepcopy_poly_ptr.h:84
mrpt::containers::deepcopy_poly_ptr::operator!
bool operator!(void) const
Definition: deepcopy_poly_ptr.h:89
mrpt::containers::deepcopy_poly_ptr::m_smartptr
T m_smartptr
Definition: deepcopy_poly_ptr.h:95
mrpt::containers::deepcopy_poly_ptr::get_ptr
T & get_ptr()
Definition: deepcopy_poly_ptr.h:91
mrpt::containers::deepcopy_poly_ptr::operator->
const T::element_type * operator->() const
Definition: deepcopy_poly_ptr.h:85
mrpt::containers::deepcopy_poly_ptr::get
const T::element_type * get() const
Definition: deepcopy_poly_ptr.h:76
mrpt::containers::deepcopy_poly_ptr::operator=
deepcopy_poly_ptr< T > & operator=(const T &o_ptr)
Definition: deepcopy_poly_ptr.h:48
mrpt::containers::deepcopy_poly_ptr::deepcopy_poly_ptr
deepcopy_poly_ptr()=default
Default ctor; init to nullptr.
mrpt::containers::deepcopy_poly_ptr::get_ptr
const T & get_ptr() const
Definition: deepcopy_poly_ptr.h:90
mrpt::containers
Definition: bimap.h:14
mrpt::containers::deepcopy_poly_ptr::~deepcopy_poly_ptr
~deepcopy_poly_ptr()=default



Page generated by Doxygen 1.8.17 for MRPT 2.0.3 at Fri May 29 13:06:46 UTC 2020