Main MRPT website > C++ reference for MRPT 1.5.7
poly_ptr_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-2017, 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 utils
17  {
18  /** \addtogroup stlext_grp
19  * @{ */
20 
21  /** Wrapper to a smart pointer to polymorphic classes, capable of handling
22  * copy operator, etc. making deep copies.
23  * Example use: `poly_ptr_ptr<mrpt::poses::CPosePDFPtr>`
24  * \sa copy_ptr<T>
25  */
26  template <typename T>
28  {
29  public:
30  /** Ctor from a smart pointer; makes deep copy. */
31  poly_ptr_ptr(const T &ptr) {
32  m_smartptr = ptr;
33  m_smartptr.make_unique();
34  }
35  /** Default ctor; init to nullptr. */
37  /** copy ctor: makes a copy of the object via `clone()` */
40  m_smartptr.make_unique();
41  }
43  if (this == &o) return *this;
45  m_smartptr.make_unique();
46  return *this;
47  }
48  poly_ptr_ptr<T> & operator =(const T &o_ptr) {
49  m_smartptr = o_ptr;
50  m_smartptr.make_unique();
51  return *this;
52  }
53 #if (__cplusplus>199711L)
54  /** move ctor */
56  m_smartptr = o.m_smartptr;
57  o.m_smartptr.clear_unique();
58  }
59  /** move operator */
60  poly_ptr_ptr<T> &operator =(poly_ptr_ptr<T> && o) {
61  if (this == &o) return *this;
62  m_smartptr = o.m_smartptr;
63  o.m_smartptr.clear_unique();
64  return *this;
65  }
66 #endif
68 
69  typename T::value_type * pointer() {
70  if (m_smartptr) return m_smartptr.pointer();
71  else throw std::runtime_error("dereferencing NULL poly_ptr_ptr");
72  }
73  const typename T::value_type * pointer() const {
74  if (m_smartptr) return m_smartptr.pointer();
75  else throw std::runtime_error("dereferencing NULL poly_ptr_ptr");
76  }
77 
78  typename T::value_type * operator->() { return pointer(); }
79  const typename T::value_type * operator->() const { return pointer(); }
80 
81  typename T::value_type& operator*(void) { return *pointer(); }
82  const typename T::value_type& operator*(void) const { return *pointer(); }
83 
84  operator bool() const { return m_smartptr.present(); }
85  bool operator!(void) const { return !m_smartptr.present(); }
86 
87  const T & get_ptr() const { return m_smartptr; }
88  T & get_ptr() { return m_smartptr; }
89 
90  void clear_unique() { m_smartptr.clear_unique(); }
91  private:
93  };
94 
95  /** @} */ // end of grouping
96  } // End of namespace
97 } // End of namespace
Wrapper to a smart pointer to polymorphic classes, capable of handling copy operator, etc.
Definition: poly_ptr_ptr.h:27
const T::value_type & operator*(void) const
Definition: poly_ptr_ptr.h:82
T::value_type * pointer()
Definition: poly_ptr_ptr.h:69
poly_ptr_ptr(const poly_ptr_ptr< T > &o)
copy ctor: makes a copy of the object via clone()
Definition: poly_ptr_ptr.h:38
const T::value_type * operator->() const
Definition: poly_ptr_ptr.h:79
T::value_type & operator*(void)
Definition: poly_ptr_ptr.h:81
T::value_type * operator->()
Definition: poly_ptr_ptr.h:78
poly_ptr_ptr(const T &ptr)
Ctor from a smart pointer; makes deep copy.
Definition: poly_ptr_ptr.h:31
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
poly_ptr_ptr< T > & operator=(const poly_ptr_ptr< T > &o)
Definition: poly_ptr_ptr.h:42
const T & get_ptr() const
Definition: poly_ptr_ptr.h:87
bool operator!(void) const
Definition: poly_ptr_ptr.h:85
const T::value_type * pointer() const
Definition: poly_ptr_ptr.h:73
poly_ptr_ptr()
Default ctor; init to nullptr.
Definition: poly_ptr_ptr.h:36



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019