Bridge++  Ver. 2.0.2
unique_pointer.h
Go to the documentation of this file.
1 
15 #ifndef UNIQUE_POINTER_INCLUDED
16 #define UNIQUE_POINTER_INCLUDED
17 
18 #include <cstddef>
19 #include <cassert>
20 #define _assert(expr) assert(expr)
21 
22 namespace Bridge {
23  template<typename T>
24  class unique_ptr // noncopyable
25  {
26  public:
27  typedef T * pointer;
28  typedef T element_type;
29 
30  private:
32 
33  unique_ptr(unique_ptr const&);
35 
37 
38  // safe-bool idiom to avoid unintentional conversion
40 
41  public:
42 
43  explicit
44  unique_ptr(pointer p = pointer()) : _ptr(p) {}
45 
47  {
48  if (_ptr != pointer()) delete _ptr;
49  _ptr = pointer();
50  }
51 
52  void reset(pointer p = pointer())
53  {
54  _assert(p == pointer() || p != _ptr);
55  this_type(p).swap(*this);
56  }
57 
59  {
60  _assert(_ptr != pointer());
61  return *_ptr;
62  }
63 
65  {
66  _assert(_ptr != pointer());
67  return _ptr;
68  }
69 
70  pointer get() const
71  {
72  return _ptr;
73  }
74 
76  {
77  pointer _p = _ptr;
78 
79  _ptr = pointer();
80  return _p;
81  }
82 
83  void swap(unique_ptr& _u)
84  {
85  pointer tmp = _u._ptr;
86 
87  _u._ptr = _ptr;
88  _ptr = tmp;
89  }
90 
91  // operator bool() const {
92  // return _ptr == pointer() ? false : true;
93  // }
94 
95  operator unspecified_bool_type() const {
96  return _ptr == 0 ? 0 : &this_type::get;
97  }
98  };
99 
100  template<typename T>
101  class unique_ptr<T[]> // noncopyable
102  {
103  public:
104  typedef T *pointer;
105  typedef T element_type;
106 
107  private:
109 
110  unique_ptr(unique_ptr const&);
112 
114 
115  public:
116 
117  explicit
119 
121  {
122  if (_ptr != pointer()) delete [] _ptr;
123  _ptr = pointer();
124  }
125 
126  void reset(pointer p = pointer())
127  {
128  _assert(p == pointer() || p != _ptr);
129  this_type(p).swap(*this);
130  }
131 
132  element_type& operator[](std::ptrdiff_t i) const
133  {
134  _assert(_ptr != pointer());
135  _assert(i >= 0);
136  return _ptr[i];
137  }
138 
139  pointer get() const
140  {
141  return _ptr;
142  }
143 
144  operator bool() const {
145  return _ptr == pointer() ? false : true;
146  }
147 
148  void swap(unique_ptr& _u)
149  {
150  pointer tmp = _u._ptr;
151 
152  _u._ptr = _ptr;
153  _ptr = tmp;
154  }
155  };
156 } // end namespace Bridge
157 
158 #undef _assert
159 #endif /* UNIQUE_POINTER_INCLUDED */
Bridge::unique_ptr::element_type
T element_type
Definition: unique_pointer.h:28
Bridge
Definition: bridgeIO.cpp:24
Bridge::unique_ptr::this_type
unique_ptr< T > this_type
Definition: unique_pointer.h:36
Bridge::unique_ptr::_ptr
pointer _ptr
Definition: unique_pointer.h:31
_assert
#define _assert(expr)
Definition: unique_pointer.h:20
Bridge::unique_ptr::swap
void swap(unique_ptr &_u)
Definition: unique_pointer.h:83
Bridge::unique_ptr< T[]>::get
pointer get() const
Definition: unique_pointer.h:139
Bridge::unique_ptr::get
pointer get() const
Definition: unique_pointer.h:70
Bridge::unique_ptr< T[]>
Definition: unique_pointer.h:101
Bridge::unique_ptr::release
pointer release()
Definition: unique_pointer.h:75
Bridge::unique_ptr::unique_ptr
unique_ptr(unique_ptr const &)
Bridge::unique_ptr< T[]>::unique_ptr
unique_ptr(pointer p=pointer())
Definition: unique_pointer.h:118
Bridge::unique_ptr< T[]>::swap
void swap(unique_ptr &_u)
Definition: unique_pointer.h:148
Bridge::unique_ptr< T[]>::pointer
T * pointer
Definition: unique_pointer.h:104
Bridge::unique_ptr< T[]>::reset
void reset(pointer p=pointer())
Definition: unique_pointer.h:126
Bridge::unique_ptr::operator=
unique_ptr & operator=(unique_ptr const &)
Bridge::unique_ptr::pointer
T * pointer
Definition: unique_pointer.h:27
Bridge::unique_ptr< T[]>::this_type
unique_ptr< T[]> this_type
Definition: unique_pointer.h:113
Bridge::unique_ptr< T[]>::_ptr
pointer _ptr
Definition: unique_pointer.h:108
Bridge::unique_ptr::operator->
pointer operator->() const
Definition: unique_pointer.h:64
Bridge::unique_ptr::operator*
element_type & operator*() const
Definition: unique_pointer.h:58
Bridge::unique_ptr< T[]>::~unique_ptr
~unique_ptr()
Definition: unique_pointer.h:120
Bridge::unique_ptr
Definition: unique_pointer.h:24
Bridge::unique_ptr< T[]>::element_type
T element_type
Definition: unique_pointer.h:105
Bridge::unique_ptr< T[]>::operator[]
element_type & operator[](std::ptrdiff_t i) const
Definition: unique_pointer.h:132
Bridge::unique_ptr::unique_ptr
unique_ptr(pointer p=pointer())
Definition: unique_pointer.h:44
Bridge::unique_ptr::reset
void reset(pointer p=pointer())
Definition: unique_pointer.h:52
Bridge::unique_ptr::~unique_ptr
~unique_ptr()
Definition: unique_pointer.h:46
Bridge::unique_ptr::unspecified_bool_type
element_type *(this_type::* unspecified_bool_type)() const
Definition: unique_pointer.h:39