Main MRPT website > C++ reference for MRPT 1.9.9
deepcopy_poly_ptr.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <stdexcept>
12 #include <cstdlib>
13 
14 namespace mrpt
15 {
16 namespace containers
17 {
18 /** \addtogroup mrpt_containers_grp
19  * @{ */
20 
21 /** Wrapper to a std::shared_ptr<>, adding deep-copy semantics to copy ctor and
22  * copy operator, suitable for polymorphic classes with a `clone()` method.
23  * Example use: `deepcopy_poly_ptr<mrpt::poses::CPosePDF::Ptr>`
24  * \sa for non-virtual classes, see copy_ptr<T>
25  */
26 template <typename T>
28 {
29  public:
30  /** Ctor from a smart pointer; makes deep copy. */
31  deepcopy_poly_ptr(const T& ptr)
32  {
33  m_smartptr.reset(dynamic_cast<typename T::element_type*>(ptr->clone()));
34  }
35  /** Default ctor; init to nullptr. */
37  /** copy ctor: makes a copy of the object via `clone()` */
39  {
40  m_smartptr.reset(
41  dynamic_cast<typename T::element_type*>(o.m_smartptr->clone()));
42  }
44  {
45  if (this == &o) return *this;
46  m_smartptr.reset(
47  dynamic_cast<typename T::element_type*>(o.m_smartptr->clone()));
48  return *this;
49  }
51  {
52  m_smartptr.reset(
53  dynamic_cast<typename T::element_type*>(o_ptr->clone()));
54  return *this;
55  }
56  /** move ctor */
58  {
59  m_smartptr = o.m_smartptr;
60  o.m_smartptr.reset();
61  }
62  /** move operator */
64  {
65  if (this == &o) return *this;
66  m_smartptr = o.m_smartptr;
67  o.m_smartptr.reset();
68  return *this;
69  }
71  typename T::element_type* get()
72  {
73  if (m_smartptr)
74  return m_smartptr.get();
75  else
76  throw std::runtime_error("dereferencing nullptr poly_ptr");
77  }
78  const typename T::element_type* get() const
79  {
80  if (m_smartptr)
81  return m_smartptr.get();
82  else
83  throw std::runtime_error("dereferencing nullptr poly_ptr");
84  }
85 
86  typename T::element_type* operator->() { return get(); }
87  const typename T::element_type* operator->() const { return get(); }
88  typename T::element_type& operator*(void) { return *get(); }
89  const typename T::element_type& operator*(void)const { return *get(); }
90  operator bool() const { return m_smartptr ? true : false; }
91  bool operator!(void) const { return m_smartptr ? false : true; }
92  const T& get_ptr() const { return m_smartptr; }
93  T& get_ptr() { return m_smartptr; }
94  void reset() { m_smartptr.reset(); }
95 
96  private:
98 };
99 
100 /** @} */ // end of grouping
101 } // namespace containers
102 } // namespace mrpt
mrpt::containers::deepcopy_poly_ptr::reset
void reset()
Definition: deepcopy_poly_ptr.h:94
mrpt::containers::deepcopy_poly_ptr::deepcopy_poly_ptr
deepcopy_poly_ptr(deepcopy_poly_ptr &&o)
move ctor
Definition: deepcopy_poly_ptr.h:57
mrpt::containers::deepcopy_poly_ptr::operator=
deepcopy_poly_ptr< T > & operator=(const deepcopy_poly_ptr< T > &o)
Definition: deepcopy_poly_ptr.h:43
mrpt::containers::deepcopy_poly_ptr::operator*
const T::element_type & operator*(void) const
Definition: deepcopy_poly_ptr.h:89
mrpt::containers::deepcopy_poly_ptr::get
T::element_type * get()
Definition: deepcopy_poly_ptr.h:71
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:31
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::containers::deepcopy_poly_ptr::deepcopy_poly_ptr
deepcopy_poly_ptr()
Default ctor; init to nullptr.
Definition: deepcopy_poly_ptr.h:36
mrpt::containers::deepcopy_poly_ptr::operator*
T::element_type & operator*(void)
Definition: deepcopy_poly_ptr.h:88
mrpt::containers::deepcopy_poly_ptr::operator=
deepcopy_poly_ptr< T > & operator=(deepcopy_poly_ptr< T > &&o)
move operator
Definition: deepcopy_poly_ptr.h:63
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:38
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:27
mrpt::containers::deepcopy_poly_ptr::operator->
T::element_type * operator->()
Definition: deepcopy_poly_ptr.h:86
mrpt::containers::deepcopy_poly_ptr::operator!
bool operator!(void) const
Definition: deepcopy_poly_ptr.h:91
mrpt::containers::deepcopy_poly_ptr::m_smartptr
T m_smartptr
Definition: deepcopy_poly_ptr.h:97
mrpt::containers::deepcopy_poly_ptr::~deepcopy_poly_ptr
~deepcopy_poly_ptr()
Definition: deepcopy_poly_ptr.h:70
mrpt::containers::deepcopy_poly_ptr::get_ptr
T & get_ptr()
Definition: deepcopy_poly_ptr.h:93
mrpt::containers::deepcopy_poly_ptr::operator->
const T::element_type * operator->() const
Definition: deepcopy_poly_ptr.h:87
mrpt::containers::deepcopy_poly_ptr::get
const T::element_type * get() const
Definition: deepcopy_poly_ptr.h:78
mrpt::containers::deepcopy_poly_ptr::operator=
deepcopy_poly_ptr< T > & operator=(const T &o_ptr)
Definition: deepcopy_poly_ptr.h:50
mrpt::containers::deepcopy_poly_ptr::get_ptr
const T & get_ptr() const
Definition: deepcopy_poly_ptr.h:92



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST