| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2005 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 ANDROID_STRONG_POINTER_H | 
 | 18 | #define ANDROID_STRONG_POINTER_H | 
 | 19 |  | 
 | 20 | #include <cutils/atomic.h> | 
 | 21 |  | 
 | 22 | #include <stdint.h> | 
 | 23 | #include <sys/types.h> | 
 | 24 | #include <stdlib.h> | 
 | 25 |  | 
 | 26 | // --------------------------------------------------------------------------- | 
 | 27 | namespace android { | 
 | 28 |  | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 29 | template<typename T> class wp; | 
 | 30 |  | 
 | 31 | // --------------------------------------------------------------------------- | 
 | 32 |  | 
 | 33 | #define COMPARE(_op_)                                           \ | 
 | 34 | inline bool operator _op_ (const sp<T>& o) const {              \ | 
 | 35 |     return m_ptr _op_ o.m_ptr;                                  \ | 
 | 36 | }                                                               \ | 
 | 37 | inline bool operator _op_ (const T* o) const {                  \ | 
 | 38 |     return m_ptr _op_ o;                                        \ | 
 | 39 | }                                                               \ | 
 | 40 | template<typename U>                                            \ | 
 | 41 | inline bool operator _op_ (const sp<U>& o) const {              \ | 
 | 42 |     return m_ptr _op_ o.m_ptr;                                  \ | 
 | 43 | }                                                               \ | 
 | 44 | template<typename U>                                            \ | 
 | 45 | inline bool operator _op_ (const U* o) const {                  \ | 
 | 46 |     return m_ptr _op_ o;                                        \ | 
 | 47 | }                                                               \ | 
 | 48 | inline bool operator _op_ (const wp<T>& o) const {              \ | 
 | 49 |     return m_ptr _op_ o.m_ptr;                                  \ | 
 | 50 | }                                                               \ | 
 | 51 | template<typename U>                                            \ | 
 | 52 | inline bool operator _op_ (const wp<U>& o) const {              \ | 
 | 53 |     return m_ptr _op_ o.m_ptr;                                  \ | 
 | 54 | } | 
 | 55 |  | 
 | 56 | // --------------------------------------------------------------------------- | 
 | 57 |  | 
| Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 58 | template<typename T> | 
 | 59 | class sp { | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 60 | public: | 
 | 61 |     inline sp() : m_ptr(0) { } | 
 | 62 |  | 
| Chih-Hung Hsieh | 2a92996 | 2016-08-02 12:14:47 -0700 | [diff] [blame] | 63 |     sp(T* other);  // NOLINT(implicit) | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 64 |     sp(const sp<T>& other); | 
| John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 65 |     sp(sp<T>&& other); | 
| Chih-Hung Hsieh | 2a92996 | 2016-08-02 12:14:47 -0700 | [diff] [blame] | 66 |     template<typename U> sp(U* other);  // NOLINT(implicit) | 
 | 67 |     template<typename U> sp(const sp<U>& other);  // NOLINT(implicit) | 
 | 68 |     template<typename U> sp(sp<U>&& other);  // NOLINT(implicit) | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 69 |  | 
 | 70 |     ~sp(); | 
 | 71 |  | 
 | 72 |     // Assignment | 
 | 73 |  | 
 | 74 |     sp& operator = (T* other); | 
 | 75 |     sp& operator = (const sp<T>& other); | 
| John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 76 |     sp& operator = (sp<T>&& other); | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 77 |  | 
 | 78 |     template<typename U> sp& operator = (const sp<U>& other); | 
| John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 79 |     template<typename U> sp& operator = (sp<U>&& other); | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 80 |     template<typename U> sp& operator = (U* other); | 
 | 81 |  | 
 | 82 |     //! Special optimization for use by ProcessState (and nobody else). | 
 | 83 |     void force_set(T* other); | 
 | 84 |  | 
 | 85 |     // Reset | 
 | 86 |  | 
 | 87 |     void clear(); | 
 | 88 |  | 
 | 89 |     // Accessors | 
 | 90 |  | 
 | 91 |     inline  T&      operator* () const  { return *m_ptr; } | 
 | 92 |     inline  T*      operator-> () const { return m_ptr;  } | 
 | 93 |     inline  T*      get() const         { return m_ptr; } | 
 | 94 |  | 
 | 95 |     // Operators | 
 | 96 |  | 
 | 97 |     COMPARE(==) | 
 | 98 |     COMPARE(!=) | 
 | 99 |     COMPARE(>) | 
 | 100 |     COMPARE(<) | 
 | 101 |     COMPARE(<=) | 
 | 102 |     COMPARE(>=) | 
 | 103 |  | 
 | 104 | private:     | 
 | 105 |     template<typename Y> friend class sp; | 
 | 106 |     template<typename Y> friend class wp; | 
| Mathias Agopian | 3e0f875 | 2011-02-24 18:12:34 -0800 | [diff] [blame] | 107 |     void set_pointer(T* ptr); | 
 | 108 |     T* m_ptr; | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 109 | }; | 
 | 110 |  | 
 | 111 | #undef COMPARE | 
 | 112 |  | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 113 | // --------------------------------------------------------------------------- | 
 | 114 | // No user serviceable parts below here. | 
 | 115 |  | 
 | 116 | template<typename T> | 
 | 117 | sp<T>::sp(T* other) | 
| Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 118 |         : m_ptr(other) { | 
 | 119 |     if (other) | 
 | 120 |         other->incStrong(this); | 
 | 121 | } | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 122 |  | 
 | 123 | template<typename T> | 
 | 124 | sp<T>::sp(const sp<T>& other) | 
| Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 125 |         : m_ptr(other.m_ptr) { | 
 | 126 |     if (m_ptr) | 
 | 127 |         m_ptr->incStrong(this); | 
 | 128 | } | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 129 |  | 
| John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 130 | template<typename T> | 
 | 131 | sp<T>::sp(sp<T>&& other) | 
 | 132 |         : m_ptr(other.m_ptr) { | 
 | 133 |     other.m_ptr = nullptr; | 
 | 134 | } | 
 | 135 |  | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 136 | template<typename T> template<typename U> | 
| Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 137 | sp<T>::sp(U* other) | 
 | 138 |         : m_ptr(other) { | 
 | 139 |     if (other) | 
| Colin Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 140 |         (static_cast<T*>(other))->incStrong(this); | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 141 | } | 
 | 142 |  | 
 | 143 | template<typename T> template<typename U> | 
 | 144 | sp<T>::sp(const sp<U>& other) | 
| Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 145 |         : m_ptr(other.m_ptr) { | 
 | 146 |     if (m_ptr) | 
 | 147 |         m_ptr->incStrong(this); | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 148 | } | 
 | 149 |  | 
| John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 150 | template<typename T> template<typename U> | 
 | 151 | sp<T>::sp(sp<U>&& other) | 
 | 152 |         : m_ptr(other.m_ptr) { | 
 | 153 |     other.m_ptr = nullptr; | 
 | 154 | } | 
 | 155 |  | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 156 | template<typename T> | 
| Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 157 | sp<T>::~sp() { | 
 | 158 |     if (m_ptr) | 
 | 159 |         m_ptr->decStrong(this); | 
 | 160 | } | 
 | 161 |  | 
 | 162 | template<typename T> | 
 | 163 | sp<T>& sp<T>::operator =(const sp<T>& other) { | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 164 |     T* otherPtr(other.m_ptr); | 
| Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 165 |     if (otherPtr) | 
 | 166 |         otherPtr->incStrong(this); | 
 | 167 |     if (m_ptr) | 
 | 168 |         m_ptr->decStrong(this); | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 169 |     m_ptr = otherPtr; | 
 | 170 |     return *this; | 
 | 171 | } | 
 | 172 |  | 
 | 173 | template<typename T> | 
| John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 174 | sp<T>& sp<T>::operator =(sp<T>&& other) { | 
 | 175 |     if (m_ptr) | 
 | 176 |         m_ptr->decStrong(this); | 
 | 177 |     m_ptr = other.m_ptr; | 
 | 178 |     other.m_ptr = nullptr; | 
 | 179 |     return *this; | 
 | 180 | } | 
 | 181 |  | 
 | 182 | template<typename T> | 
| Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 183 | sp<T>& sp<T>::operator =(T* other) { | 
 | 184 |     if (other) | 
 | 185 |         other->incStrong(this); | 
 | 186 |     if (m_ptr) | 
 | 187 |         m_ptr->decStrong(this); | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 188 |     m_ptr = other; | 
 | 189 |     return *this; | 
 | 190 | } | 
 | 191 |  | 
 | 192 | template<typename T> template<typename U> | 
| Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 193 | sp<T>& sp<T>::operator =(const sp<U>& other) { | 
| Mathias Agopian | 7332f80 | 2011-02-25 16:11:44 -0800 | [diff] [blame] | 194 |     T* otherPtr(other.m_ptr); | 
| Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 195 |     if (otherPtr) | 
 | 196 |         otherPtr->incStrong(this); | 
 | 197 |     if (m_ptr) | 
 | 198 |         m_ptr->decStrong(this); | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 199 |     m_ptr = otherPtr; | 
 | 200 |     return *this; | 
 | 201 | } | 
 | 202 |  | 
 | 203 | template<typename T> template<typename U> | 
| John Reck | d69089a | 2015-10-28 15:36:33 -0700 | [diff] [blame] | 204 | sp<T>& sp<T>::operator =(sp<U>&& other) { | 
 | 205 |     if (m_ptr) | 
 | 206 |         m_ptr->decStrong(this); | 
 | 207 |     m_ptr = other.m_ptr; | 
 | 208 |     other.m_ptr = nullptr; | 
 | 209 |     return *this; | 
 | 210 | } | 
 | 211 |  | 
 | 212 | template<typename T> template<typename U> | 
| Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 213 | sp<T>& sp<T>::operator =(U* other) { | 
 | 214 |     if (other) | 
| Colin Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 215 |         (static_cast<T*>(other))->incStrong(this); | 
| Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 216 |     if (m_ptr) | 
 | 217 |         m_ptr->decStrong(this); | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 218 |     m_ptr = other; | 
 | 219 |     return *this; | 
 | 220 | } | 
 | 221 |  | 
| Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 222 | template<typename T> | 
 | 223 | void sp<T>::force_set(T* other) { | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 224 |     other->forceIncStrong(this); | 
 | 225 |     m_ptr = other; | 
 | 226 | } | 
 | 227 |  | 
 | 228 | template<typename T> | 
| Mathias Agopian | 8db925f | 2013-05-09 13:04:32 -0700 | [diff] [blame] | 229 | void sp<T>::clear() { | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 230 |     if (m_ptr) { | 
 | 231 |         m_ptr->decStrong(this); | 
 | 232 |         m_ptr = 0; | 
 | 233 |     } | 
 | 234 | } | 
 | 235 |  | 
 | 236 | template<typename T> | 
| Mathias Agopian | 3e0f875 | 2011-02-24 18:12:34 -0800 | [diff] [blame] | 237 | void sp<T>::set_pointer(T* ptr) { | 
 | 238 |     m_ptr = ptr; | 
 | 239 | } | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 240 |  | 
| Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 241 | }; // namespace android | 
 | 242 |  | 
 | 243 | // --------------------------------------------------------------------------- | 
 | 244 |  | 
 | 245 | #endif // ANDROID_STRONG_POINTER_H |