orocos_kdl
scoped_ptr.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  scoped_ptr - a not too smart pointer for exception safe heap objects
3  -------------------------
4  begin : May 2019
5  copyright : (C) 2019 Intermodalics
6 
7  ***************************************************************************
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU Lesser General Public *
10  * License as published by the Free Software Foundation; either *
11  * version 2.1 of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * Lesser General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU Lesser General Public *
19  * License along with this library; if not, write to the Free Software *
20  * Foundation, Inc., 59 Temple Place, *
21  * Suite 330, Boston, MA 02111-1307 USA *
22  * *
23  ***************************************************************************/
24 
25 #if (__cplusplus > 199711L)
26 #include <utility>
27 #else
28 #include <algorithm>
29 #endif
30 
31 namespace KDL {
32 
33 template<typename T> class scoped_ptr;
34 
35 template<typename T> void swap(scoped_ptr<T>&, scoped_ptr<T>&);
36 
37 template<typename T>
38 class scoped_ptr {
39  public:
40  scoped_ptr() : ptr_(0) { }
41  explicit scoped_ptr(T* p) : ptr_(p) { }
42 
43  ~scoped_ptr() { delete ptr_; }
44 
45  T* operator->() { return ptr_; }
46  const T* operator->() const { return ptr_; }
47 
48  T* get() const { return ptr_; }
49 
50  void reset(T* p = 0) {
51  T* old = ptr_;
52  ptr_ = p;
53  delete old;
54  }
55 
56  T* release() {
57  T* old = ptr_;
58  ptr_ = 0;
59  return old;
60  }
61 
62  friend void swap<>(scoped_ptr<T>& a, scoped_ptr<T>& b);
63 
64  private:
65  scoped_ptr(const scoped_ptr&); // not-copyable
66  scoped_ptr& operator=(const scoped_ptr&); // not-copyable
67 
68  T* ptr_;
69 };
70 
71 template<typename T>
73  using std::swap;
74  swap(a.ptr_, b.ptr_);
75 }
76 
77 
78 } // namespace KDL
KDL::scoped_ptr::release
T * release()
Definition: scoped_ptr.hpp:78
utility
KDL::scoped_ptr::ptr_
T * ptr_
Definition: scoped_ptr.hpp:90
KDL::swap
void swap(scoped_ptr< T > &, scoped_ptr< T > &)
Definition: scoped_ptr.hpp:94
KDL::scoped_ptr::get
T * get() const
Definition: scoped_ptr.hpp:70
algorithm
KDL
Definition: kukaLWR_DHnew.cpp:25
KDL::scoped_ptr::operator->
T * operator->()
Definition: scoped_ptr.hpp:67
KDL::scoped_ptr::scoped_ptr
scoped_ptr()
Definition: scoped_ptr.hpp:62
KDL::scoped_ptr::reset
void reset(T *p=0)
Definition: scoped_ptr.hpp:72
std::swap
T swap(T... args)
KDL::scoped_ptr::~scoped_ptr
~scoped_ptr()
Definition: scoped_ptr.hpp:65
KDL::scoped_ptr
Definition: scoped_ptr.hpp:55
KDL::scoped_ptr::operator=
scoped_ptr & operator=(const scoped_ptr &)