1 // Copyright 2013 Google Inc. All Rights Reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google Inc. nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 // This is a copy of breakpad's standalone scoped_ptr, which has been
30 // renamed to nonstd::unique_ptr, and from which more complicated classes
31 // have been removed. The reset() method has also been tweaked to more
32 // closely match c++11, and an implicit conversion to bool has been added.
33
34 // Scopers help you manage ownership of a pointer, helping you easily manage the
35 // a pointer within a scope, and automatically destroying the pointer at the
36 // end of a scope.
37 //
38 // A unique_ptr<T> is like a T*, except that the destructor of unique_ptr<T>
39 // automatically deletes the pointer it holds (if any).
40 // That is, unique_ptr<T> owns the T object that it points to.
41 // Like a T*, a unique_ptr<T> may hold either NULL or a pointer to a T object.
42 // Also like T*, unique_ptr<T> is thread-compatible, and once you
43 // dereference it, you get the thread safety guarantees of T.
44 //
45 // Example usage (unique_ptr):
46 // {
47 // unique_ptr<Foo> foo(new Foo("wee"));
48 // } // foo goes out of scope, releasing the pointer with it.
49 //
50 // {
51 // unique_ptr<Foo> foo; // No pointer managed.
52 // foo.reset(new Foo("wee")); // Now a pointer is managed.
53 // foo.reset(new Foo("wee2")); // Foo("wee") was destroyed.
54 // foo.reset(new Foo("wee3")); // Foo("wee2") was destroyed.
55 // foo->Method(); // Foo::Method() called.
56 // foo.get()->Method(); // Foo::Method() called.
57 // SomeFunc(foo.release()); // SomeFunc takes ownership, foo no longer
58 // // manages a pointer.
59 // foo.reset(new Foo("wee4")); // foo manages a pointer again.
60 // foo.reset(); // Foo("wee4") destroyed, foo no longer
61 // // manages a pointer.
62 // } // foo wasn't managing a pointer, so nothing was destroyed.
63 //
64 // The size of a unique_ptr is small: sizeof(unique_ptr<C>) == sizeof(C*)
65
66 #ifndef NONSTD_UNIQUE_PTR_H_
67 #define NONSTD_UNIQUE_PTR_H_
68
69 // This is an implementation designed to match the anticipated future TR2
70 // implementation of the unique_ptr class.
71
72 #include <assert.h>
73 #include <stddef.h>
74 #include <stdlib.h>
75
76 namespace nonstd {
77
78 // Common implementation for both pointers to elements and pointers to
79 // arrays. These are differentiated below based on the need to invoke
80 // delete vs. delete[] as appropriate.
81 template <class C>
82 class unique_ptr_base {
83 public:
84
85 // The element type
86 typedef C element_type;
87
unique_ptr_base(C * p)88 explicit unique_ptr_base(C* p) : ptr_(p) { }
89
90 // Accessors to get the owned object.
91 // operator* and operator-> will assert() if there is no current object.
92 C& operator*() const {
93 assert(ptr_ != NULL);
94 return *ptr_;
95 }
96 C* operator->() const {
97 assert(ptr_ != NULL);
98 return ptr_;
99 }
get()100 C* get() const { return ptr_; }
101
102 // Comparison operators.
103 // These return whether two unique_ptr refer to the same object, not just to
104 // two different but equal objects.
105 bool operator==(C* p) const { return ptr_ == p; }
106 bool operator!=(C* p) const { return ptr_ != p; }
107
108 // Swap two scoped pointers.
swap(unique_ptr_base & p2)109 void swap(unique_ptr_base& p2) {
110 C* tmp = ptr_;
111 ptr_ = p2.ptr_;
112 p2.ptr_ = tmp;
113 }
114
115 // Release a pointer.
116 // The return value is the current pointer held by this object.
117 // If this object holds a NULL pointer, the return value is NULL.
118 // After this operation, this object will hold a NULL pointer,
119 // and will not own the object any more.
release()120 C* release() {
121 C* retVal = ptr_;
122 ptr_ = NULL;
123 return retVal;
124 }
125
126 // Allow promotion to bool for conditional statements.
127 operator bool() const { return ptr_ != NULL; }
128
129 protected:
130 C* ptr_;
131 };
132
133 // Implementation for ordinary pointers using delete.
134 template <class C>
135 class unique_ptr : public unique_ptr_base<C> {
136 public:
137 using unique_ptr_base<C>::ptr_;
138
139 // Constructor. Defaults to initializing with NULL. There is no way
140 // to create an uninitialized unique_ptr. The input parameter must be
141 // allocated with new (not new[] - see below).
142 explicit unique_ptr(C* p = NULL) : unique_ptr_base<C>(p) { }
143
144 // Destructor. If there is a C object, delete it.
145 // We don't need to test ptr_ == NULL because C++ does that for us.
~unique_ptr()146 ~unique_ptr() {
147 enum { type_must_be_complete = sizeof(C) };
148 delete ptr_;
149 }
150
151 // Reset. Deletes the current owned object, if any.
152 // Then takes ownership of a new object, if given.
153 // this->reset(this->get()) works.
154 void reset(C* p = NULL) {
155 if (p != ptr_) {
156 enum { type_must_be_complete = sizeof(C) };
157 C* old_ptr = ptr_;
158 ptr_ = p;
159 delete old_ptr;
160 }
161 }
162
163 private:
164 // Forbid comparison of unique_ptr types. If C2 != C, it totally doesn't
165 // make sense, and if C2 == C, it still doesn't make sense because you should
166 // never have the same object owned by two different unique_ptrs.
167 template <class C2> bool operator==(unique_ptr<C2> const& p2) const;
168 template <class C2> bool operator!=(unique_ptr<C2> const& p2) const;
169
170 // Disallow evil constructors
171 unique_ptr(const unique_ptr&);
172 void operator=(const unique_ptr&);
173 };
174
175 // Specialization for arrays using delete[].
176 template <class C>
177 class unique_ptr<C[]> : public unique_ptr_base<C> {
178 public:
179 using unique_ptr_base<C>::ptr_;
180
181 // Constructor. Defaults to initializing with NULL. There is no way
182 // to create an uninitialized unique_ptr. The input parameter must be
183 // allocated with new[] (not new - see above).
184 explicit unique_ptr(C* p = NULL) : unique_ptr_base<C>(p) { }
185
186 // Destructor. If there is a C object, delete it.
187 // We don't need to test ptr_ == NULL because C++ does that for us.
~unique_ptr()188 ~unique_ptr() {
189 enum { type_must_be_complete = sizeof(C) };
190 delete[] ptr_;
191 }
192
193 // Reset. Deletes the current owned object, if any.
194 // Then takes ownership of a new object, if given.
195 // this->reset(this->get()) works.
196 void reset(C* p = NULL) {
197 if (p != ptr_) {
198 enum { type_must_be_complete = sizeof(C) };
199 C* old_ptr = ptr_;
200 ptr_ = p;
201 delete[] old_ptr;
202 }
203 }
204
205 // Support indexing since it is holding array.
206 C& operator[] (size_t i) { return ptr_[i]; }
207
208 private:
209 // Forbid comparison of unique_ptr types. If C2 != C, it totally doesn't
210 // make sense, and if C2 == C, it still doesn't make sense because you should
211 // never have the same object owned by two different unique_ptrs.
212 template <class C2> bool operator==(unique_ptr<C2> const& p2) const;
213 template <class C2> bool operator!=(unique_ptr<C2> const& p2) const;
214
215 // Disallow evil constructors
216 unique_ptr(const unique_ptr&);
217 void operator=(const unique_ptr&);
218 };
219
220 // Free functions
221 template <class C>
swap(unique_ptr<C> & p1,unique_ptr<C> & p2)222 void swap(unique_ptr<C>& p1, unique_ptr<C>& p2) {
223 p1.swap(p2);
224 }
225
226 template <class C>
227 bool operator==(C* p1, const unique_ptr<C>& p2) {
228 return p1 == p2.get();
229 }
230
231 template <class C>
232 bool operator!=(C* p1, const unique_ptr<C>& p2) {
233 return p1 != p2.get();
234 }
235
236 } // namespace nonstd
237
238 #endif // NONSTD_UNIQUE_PTR_H_
239