Main MRPT website > C++ reference for MRPT 1.9.9
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 clone smart pointer to polymorphic classes, capable of handling
22 * copy operator, etc. making deep copies.
23 * Example use: `poly_ptr_ptr<mrpt::poses::CPosePDF::Ptr>`
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  {
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  }
50  poly_ptr_ptr<T>& operator=(const T& o_ptr)
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  private:
97 };
98 
99 /** @} */ // end of grouping
100 } // End of namespace
101 } // End of namespace
poly_ptr_ptr(poly_ptr_ptr &&o)
move ctor
Definition: poly_ptr_ptr.h:57
Wrapper to a clone smart pointer to polymorphic classes, capable of handling copy operator...
Definition: poly_ptr_ptr.h:27
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
poly_ptr_ptr< T > & operator=(const T &o_ptr)
Definition: poly_ptr_ptr.h:50
const T::element_type * operator->() const
Definition: poly_ptr_ptr.h:87
T::element_type & operator*(void)
Definition: poly_ptr_ptr.h:88
poly_ptr_ptr(const T &ptr)
Ctor from a smart pointer; makes deep copy.
Definition: poly_ptr_ptr.h:31
T::element_type * operator->()
Definition: poly_ptr_ptr.h:86
poly_ptr_ptr< T > & operator=(poly_ptr_ptr< T > &&o)
move operator
Definition: poly_ptr_ptr.h:63
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:43
const T & get_ptr() const
Definition: poly_ptr_ptr.h:92
bool operator!(void) const
Definition: poly_ptr_ptr.h:91
const T::element_type & operator*(void) const
Definition: poly_ptr_ptr.h:89
poly_ptr_ptr()
Default ctor; init to nullptr.
Definition: poly_ptr_ptr.h:36



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019