vbl_scoped_ptr.h
Go to the documentation of this file.
1 #ifndef vbl_scoped_ptr_h_
2 #define vbl_scoped_ptr_h_
3 //:
4 // \file
5 // \author Amitha Perera
6 // \brief Scoped pointer lifted from BOOST.
7 //
8 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
9 // Copyright (c) 2001, 2002 Peter Dimov
10 //
11 // Permission to copy, use, modify, sell and distribute this software
12 // is granted provided this copyright notice appears in all copies.
13 // This software is provided "as is" without express or implied
14 // warranty, and with no claim as to its suitability for any purpose.
15 //
16 // http://www.boost.org/libs/smart_ptr/scoped_ptr.htm
17 //
18 // Modified from the original boost sources to fit the VXL restrictions.
19 
20 #ifdef _MSC_VER
21 # include <vcl_msvc_warnings.h>
22 #endif
23 #include <vbl/vbl_checked_delete.h>
24 
25 //:
26 // vbl_scoped_ptr mimics a built-in pointer except that it guarantees
27 // deletion of the object pointed to, either on destruction of the
28 // vbl_scoped_ptr or via an explicit reset(). vbl_scoped_ptr is a
29 // simple solution for simple needs; use vbl_shared_ptr or
30 // std::unique_ptr if your needs are more complex.
31 //
32 // To use this to manage pointer member variables using forward
33 // declaration, explicitly define a destructor in your .cxx so that
34 // the vbl_scoped_ptr destructor is called there rather than being
35 // inlined. For example, Y.h:
36 // \code
37 // struct X;
38 // struct Y {
39 // vbl_scoped_ptr<X> member;
40 //
41 // ~Y() { } // NO: causes ~vbl_scoped_ptr<X> to be instantiated, which means X must be complete.
42 // ~Y(); // YES: destructor not yet generated
43 // };
44 // \endcode
45 // Y.cxx:
46 // \code
47 // #include "X.h"
48 // Y::~Y()
49 // { } // causes ~vbl_scoped_ptr<X> to be instantiated and inlined, but X is complete here, so all is well.
50 // \endcode
51 
52 template <class T>
53 class vbl_scoped_ptr
54 {
55  private:
56  T* ptr_;
57 
58  // not copyable, not assignable.
59  vbl_scoped_ptr( vbl_scoped_ptr const& ) = delete;
60  vbl_scoped_ptr& operator=( vbl_scoped_ptr const& ) = delete;
61 
63 
64 
65 
66  public:
67  typedef T element_type;
68 
69  //:
70  explicit vbl_scoped_ptr( T* p = nullptr )
71  : ptr_(p) // never throws
72  {
73  }
74 
75  //:
76  // T must be complete when this destructor is instantiated.
77  ~vbl_scoped_ptr() // never throws
78  {
80  }
81 
82  //: Make this own \p p, releasing any existing pointer.
83  void reset( T* p = nullptr ) // never throws
84  {
85  this_type(p).swap(*this);
86  }
87 
88  //:
89  T& operator*() const // never throws
90  {
91  return *ptr_;
92  }
93 
94  //:
95  T* operator->() const // never throws
96  {
97  return ptr_;
98  }
99 
100  //:
101  T* get_pointer() const // never throws
102  {
103  return ptr_;
104  }
105 
106  // implicit conversion to "bool"
107 
108  //: Safe implicit conversion to bool.
109  //
110  // This allows for if (sp) type of usage.
111  explicit operator bool () const
112  {
113  return ptr_ ? true : false;
114  }
115 
116  //:
117  bool operator! () const // never throws
118  {
119  return ptr_ == 0;
120  }
121 
122  //:
123  void swap( vbl_scoped_ptr& b ) // never throws
124  {
125  T* tmp = b.ptr_;
126  b.ptr_ = ptr_;
127  ptr_ = tmp;
128  }
129 };
130 
131 #endif // vbl_scoped_ptr_h_
vbl_scoped_ptr(T *p=nullptr)
void swap(vbl_scoped_ptr &b)
T * operator->() const
~vbl_scoped_ptr()
T must be complete when this destructor is instantiated.
vbl_scoped_ptr mimics a built-in pointer except that it guarantees deletion of the object pointed to,...
Definition: vbl_fwd.h:19
void reset(T *p=nullptr)
Make this own p, releasing any existing pointer.
T & operator *() const
T * get_pointer() const
bool operator!() const
vbl_scoped_ptr< T > this_type
vbl_scoped_ptr(vbl_scoped_ptr const &)=delete
void vbl_checked_delete(T *x)
Checks that T is complete and deletes x.
vbl_scoped_ptr & operator=(vbl_scoped_ptr const &)=delete