• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #ifndef GATEKEEPER_UNIQUE_PTR_H_included
18 #define GATEKEEPER_UNIQUE_PTR_H_included
19 
20 #include <stdlib.h> // For NULL.
21 
22 namespace gatekeeper {
23 
24 // Default deleter for pointer types.
25 template <typename T>
26 struct DefaultDelete {
27     enum { type_must_be_complete = sizeof(T) };
DefaultDeleteDefaultDelete28     DefaultDelete() {}
operatorDefaultDelete29     void operator()(T* p) const {
30         delete p;
31     }
32 };
33 
34 // Default deleter for array types.
35 template <typename T>
36 struct DefaultDelete<T[]> {
37     enum { type_must_be_complete = sizeof(T) };
38     void operator()(T* p) const {
39         delete[] p;
40     }
41 };
42 
43 // A smart pointer that deletes the given pointer on destruction.
44 // Equivalent to C++0x's std::unique_ptr (a combination of boost::scoped_ptr
45 // and boost::scoped_array).
46 // Named to be in keeping with Android style but also to avoid
47 // collision with any other implementation, until we can switch over
48 // to unique_ptr.
49 // Use thus:
50 //   UniquePtr<C> c(new C);
51 template <typename T, typename D = DefaultDelete<T> >
52 class UniquePtr {
53 public:
54     // Construct a new UniquePtr, taking ownership of the given raw pointer.
55     explicit UniquePtr(T* ptr = NULL) : mPtr(ptr) {
56     }
57 
58     ~UniquePtr() {
59         reset();
60     }
61 
62     // Accessors.
63     T& operator*() const { return *mPtr; }
64     T* operator->() const { return mPtr; }
65     T* get() const { return mPtr; }
66 
67     // Returns the raw pointer and hands over ownership to the caller.
68     // The pointer will not be deleted by UniquePtr.
69     T* release() __attribute__((warn_unused_result)) {
70         T* result = mPtr;
71         mPtr = NULL;
72         return result;
73     }
74 
75     // Takes ownership of the given raw pointer.
76     // If this smart pointer previously owned a different raw pointer, that
77     // raw pointer will be freed.
78     void reset(T* ptr = NULL) {
79         if (ptr != mPtr) {
80             D()(mPtr);
81             mPtr = ptr;
82         }
83     }
84 
85 private:
86     // The raw pointer.
87     T* mPtr;
88 
89     // Comparing unique pointers is probably a mistake, since they're unique.
90     template <typename T2> bool operator==(const UniquePtr<T2>& p) const;
91     template <typename T2> bool operator!=(const UniquePtr<T2>& p) const;
92 
93     // Disallow copy and assignment.
94     UniquePtr(const UniquePtr&);
95     void operator=(const UniquePtr&);
96 };
97 
98 // Partial specialization for array types. Like std::unique_ptr, this removes
99 // operator* and operator-> but adds operator[].
100 template <typename T, typename D>
101 class UniquePtr<T[], D> {
102 public:
103     explicit UniquePtr(T* ptr = NULL) : mPtr(ptr) {
104     }
105 
106     ~UniquePtr() {
107         reset();
108     }
109 
110     T& operator[](size_t i) const {
111         return mPtr[i];
112     }
113     T* get() const { return mPtr; }
114 
115     T* release() __attribute__((warn_unused_result)) {
116         T* result = mPtr;
117         mPtr = NULL;
118         return result;
119     }
120 
121     void reset(T* ptr = NULL) {
122         if (ptr != mPtr) {
123             D()(mPtr);
124             mPtr = ptr;
125         }
126     }
127 
128 private:
129     T* mPtr;
130 
131     // Disallow copy and assignment.
132     UniquePtr(const UniquePtr&);
133     void operator=(const UniquePtr&);
134 };
135 
136 } //namespace gatekeeper
137 #endif  // GATEKEEPER_UNIQUE_PTR_H_included
138