2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* === NOTE === NOTE === NOTE === NOTE === NOTE === NOTE === NOTE === NOTE ===
19 * THIS IS A COPY OF libcore/include/UniquePtr.h AND AS SUCH THAT IS THE
20 * CANONICAL SOURCE OF THIS FILE. PLEASE KEEP THEM IN SYNC.
22 * === NOTE === NOTE === NOTE === NOTE === NOTE === NOTE === NOTE === NOTE ===
25 #ifndef UNIQUE_PTR_H_included
26 #define UNIQUE_PTR_H_included
28 #include <cstdlib> // For NULL.
30 // Default deleter for pointer types.
32 struct DefaultDelete {
33 enum { type_must_be_complete = sizeof(T) };
35 void operator()(T* p) const {
40 // Default deleter for array types.
42 struct DefaultDelete<T[]> {
43 enum { type_must_be_complete = sizeof(T) };
44 void operator()(T* p) const {
49 // A smart pointer that deletes the given pointer on destruction.
50 // Equivalent to C++0x's std::unique_ptr (a combination of boost::scoped_ptr
51 // and boost::scoped_array).
52 // Named to be in keeping with Android style but also to avoid
53 // collision with any other implementation, until we can switch over
56 // UniquePtr<C> c(new C);
57 template <typename T, typename D = DefaultDelete<T> >
60 // Construct a new UniquePtr, taking ownership of the given raw pointer.
61 explicit UniquePtr(T* ptr = NULL) : mPtr(ptr) {
69 T& operator*() const { return *mPtr; }
70 T* operator->() const { return mPtr; }
71 T* get() const { return mPtr; }
73 // Returns the raw pointer and hands over ownership to the caller.
74 // The pointer will not be deleted by UniquePtr.
75 T* release() __attribute__((warn_unused_result)) {
81 // Takes ownership of the given raw pointer.
82 // If this smart pointer previously owned a different raw pointer, that
83 // raw pointer will be freed.
84 void reset(T* ptr = NULL) {
95 // Comparing unique pointers is probably a mistake, since they're unique.
96 template <typename T2> bool operator==(const UniquePtr<T2>& p) const;
97 template <typename T2> bool operator!=(const UniquePtr<T2>& p) const;
99 // Disallow copy and assignment.
100 UniquePtr(const UniquePtr&);
101 void operator=(const UniquePtr&);
104 // Partial specialization for array types. Like std::unique_ptr, this removes
105 // operator* and operator-> but adds operator[].
106 template <typename T, typename D>
107 class UniquePtr<T[], D> {
109 explicit UniquePtr(T* ptr = NULL) : mPtr(ptr) {
116 T& operator[](size_t i) const {
119 T* get() const { return mPtr; }
121 T* release() __attribute__((warn_unused_result)) {
127 void reset(T* ptr = NULL) {
137 // Disallow copy and assignment.
138 UniquePtr(const UniquePtr&);
139 void operator=(const UniquePtr&);
144 // Run these tests with:
145 // g++ -g -DUNIQUE_PTR_TESTS -x c++ UniquePtr.h && ./a.out
149 static void assert(bool b) {
151 fprintf(stderr, "FAIL\n");
154 fprintf(stderr, "OK\n");
156 static int cCount = 0;
161 static bool freed = false;
163 void operator()(int* p) {
170 int main(int argc, char* argv[]) {
172 // UniquePtr<T> tests...
175 // Can we free a single object?
177 UniquePtr<C> c(new C);
181 // Does release work?
184 UniquePtr<C> c(new C);
192 UniquePtr<C> c(new C);
200 // UniquePtr<T[]> tests...
203 // Can we free an array?
205 UniquePtr<C[]> cs(new C[4]);
209 // Does release work?
211 UniquePtr<C[]> c(new C[4]);
219 UniquePtr<C[]> c(new C[4]);
227 // Custom deleter tests...
231 UniquePtr<int, Freer> i(reinterpret_cast<int*>(malloc(sizeof(int))));
239 #endif // UNIQUE_PTR_H_included