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
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*(void) const
deepcopy_poly_ptr(const T &ptr)
Ctor from a smart pointer; makes deep copy.
deepcopy_poly_ptr()
Default ctor; init to nullptr.
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()
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
const T::element_type * operator->() const
deepcopy_poly_ptr< T > & operator=(const T &o_ptr)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019