media/omx-plugin/include/ics/utils/StrongPointer.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright (C) 2005 The Android Open Source Project
michael@0 3 *
michael@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 5 * you may not use this file except in compliance with the License.
michael@0 6 * You may obtain a copy of the License at
michael@0 7 *
michael@0 8 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 9 *
michael@0 10 * Unless required by applicable law or agreed to in writing, software
michael@0 11 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 13 * See the License for the specific language governing permissions and
michael@0 14 * limitations under the License.
michael@0 15 */
michael@0 16
michael@0 17 #ifndef ANDROID_STRONG_POINTER_H
michael@0 18 #define ANDROID_STRONG_POINTER_H
michael@0 19
michael@0 20 #include <cutils/atomic.h>
michael@0 21
michael@0 22 #include <stdint.h>
michael@0 23 #include <sys/types.h>
michael@0 24 #include <stdlib.h>
michael@0 25
michael@0 26 // ---------------------------------------------------------------------------
michael@0 27 namespace android {
michael@0 28
michael@0 29 class TextOutput;
michael@0 30 TextOutput& printStrongPointer(TextOutput& to, const void* val);
michael@0 31
michael@0 32 template<typename T> class wp;
michael@0 33
michael@0 34 // ---------------------------------------------------------------------------
michael@0 35
michael@0 36 #define COMPARE(_op_) \
michael@0 37 inline bool operator _op_ (const sp<T>& o) const { \
michael@0 38 return m_ptr _op_ o.m_ptr; \
michael@0 39 } \
michael@0 40 inline bool operator _op_ (const T* o) const { \
michael@0 41 return m_ptr _op_ o; \
michael@0 42 } \
michael@0 43 template<typename U> \
michael@0 44 inline bool operator _op_ (const sp<U>& o) const { \
michael@0 45 return m_ptr _op_ o.m_ptr; \
michael@0 46 } \
michael@0 47 template<typename U> \
michael@0 48 inline bool operator _op_ (const U* o) const { \
michael@0 49 return m_ptr _op_ o; \
michael@0 50 } \
michael@0 51 inline bool operator _op_ (const wp<T>& o) const { \
michael@0 52 return m_ptr _op_ o.m_ptr; \
michael@0 53 } \
michael@0 54 template<typename U> \
michael@0 55 inline bool operator _op_ (const wp<U>& o) const { \
michael@0 56 return m_ptr _op_ o.m_ptr; \
michael@0 57 }
michael@0 58
michael@0 59 // ---------------------------------------------------------------------------
michael@0 60
michael@0 61 template <typename T>
michael@0 62 class sp
michael@0 63 {
michael@0 64 public:
michael@0 65 inline sp() : m_ptr(0) { }
michael@0 66
michael@0 67 sp(T* other);
michael@0 68 sp(const sp<T>& other);
michael@0 69 template<typename U> sp(U* other);
michael@0 70 template<typename U> sp(const sp<U>& other);
michael@0 71
michael@0 72 ~sp();
michael@0 73
michael@0 74 // Assignment
michael@0 75
michael@0 76 sp& operator = (T* other);
michael@0 77 sp& operator = (const sp<T>& other);
michael@0 78
michael@0 79 template<typename U> sp& operator = (const sp<U>& other);
michael@0 80 template<typename U> sp& operator = (U* other);
michael@0 81
michael@0 82 //! Special optimization for use by ProcessState (and nobody else).
michael@0 83 void force_set(T* other);
michael@0 84
michael@0 85 // Reset
michael@0 86
michael@0 87 void clear();
michael@0 88
michael@0 89 // Accessors
michael@0 90
michael@0 91 inline T& operator* () const { return *m_ptr; }
michael@0 92 inline T* operator-> () const { return m_ptr; }
michael@0 93 inline T* get() const { return m_ptr; }
michael@0 94
michael@0 95 // Operators
michael@0 96
michael@0 97 COMPARE(==)
michael@0 98 COMPARE(!=)
michael@0 99 COMPARE(>)
michael@0 100 COMPARE(<)
michael@0 101 COMPARE(<=)
michael@0 102 COMPARE(>=)
michael@0 103
michael@0 104 private:
michael@0 105 template<typename Y> friend class sp;
michael@0 106 template<typename Y> friend class wp;
michael@0 107 void set_pointer(T* ptr);
michael@0 108 T* m_ptr;
michael@0 109 };
michael@0 110
michael@0 111 #undef COMPARE
michael@0 112
michael@0 113 template <typename T>
michael@0 114 TextOutput& operator<<(TextOutput& to, const sp<T>& val);
michael@0 115
michael@0 116 // ---------------------------------------------------------------------------
michael@0 117 // No user serviceable parts below here.
michael@0 118
michael@0 119 template<typename T>
michael@0 120 sp<T>::sp(T* other)
michael@0 121 : m_ptr(other)
michael@0 122 {
michael@0 123 if (other) other->incStrong(this);
michael@0 124 }
michael@0 125
michael@0 126 template<typename T>
michael@0 127 sp<T>::sp(const sp<T>& other)
michael@0 128 : m_ptr(other.m_ptr)
michael@0 129 {
michael@0 130 if (m_ptr) m_ptr->incStrong(this);
michael@0 131 }
michael@0 132
michael@0 133 template<typename T> template<typename U>
michael@0 134 sp<T>::sp(U* other) : m_ptr(other)
michael@0 135 {
michael@0 136 if (other) ((T*)other)->incStrong(this);
michael@0 137 }
michael@0 138
michael@0 139 template<typename T> template<typename U>
michael@0 140 sp<T>::sp(const sp<U>& other)
michael@0 141 : m_ptr(other.m_ptr)
michael@0 142 {
michael@0 143 if (m_ptr) m_ptr->incStrong(this);
michael@0 144 }
michael@0 145
michael@0 146 template<typename T>
michael@0 147 sp<T>::~sp()
michael@0 148 {
michael@0 149 if (m_ptr) m_ptr->decStrong(this);
michael@0 150 }
michael@0 151
michael@0 152 template<typename T>
michael@0 153 sp<T>& sp<T>::operator = (const sp<T>& other) {
michael@0 154 T* otherPtr(other.m_ptr);
michael@0 155 if (otherPtr) otherPtr->incStrong(this);
michael@0 156 if (m_ptr) m_ptr->decStrong(this);
michael@0 157 m_ptr = otherPtr;
michael@0 158 return *this;
michael@0 159 }
michael@0 160
michael@0 161 template<typename T>
michael@0 162 sp<T>& sp<T>::operator = (T* other)
michael@0 163 {
michael@0 164 if (other) other->incStrong(this);
michael@0 165 if (m_ptr) m_ptr->decStrong(this);
michael@0 166 m_ptr = other;
michael@0 167 return *this;
michael@0 168 }
michael@0 169
michael@0 170 template<typename T> template<typename U>
michael@0 171 sp<T>& sp<T>::operator = (const sp<U>& other)
michael@0 172 {
michael@0 173 T* otherPtr(other.m_ptr);
michael@0 174 if (otherPtr) otherPtr->incStrong(this);
michael@0 175 if (m_ptr) m_ptr->decStrong(this);
michael@0 176 m_ptr = otherPtr;
michael@0 177 return *this;
michael@0 178 }
michael@0 179
michael@0 180 template<typename T> template<typename U>
michael@0 181 sp<T>& sp<T>::operator = (U* other)
michael@0 182 {
michael@0 183 if (other) ((T*)other)->incStrong(this);
michael@0 184 if (m_ptr) m_ptr->decStrong(this);
michael@0 185 m_ptr = other;
michael@0 186 return *this;
michael@0 187 }
michael@0 188
michael@0 189 template<typename T>
michael@0 190 void sp<T>::force_set(T* other)
michael@0 191 {
michael@0 192 other->forceIncStrong(this);
michael@0 193 m_ptr = other;
michael@0 194 }
michael@0 195
michael@0 196 template<typename T>
michael@0 197 void sp<T>::clear()
michael@0 198 {
michael@0 199 if (m_ptr) {
michael@0 200 m_ptr->decStrong(this);
michael@0 201 m_ptr = 0;
michael@0 202 }
michael@0 203 }
michael@0 204
michael@0 205 template<typename T>
michael@0 206 void sp<T>::set_pointer(T* ptr) {
michael@0 207 m_ptr = ptr;
michael@0 208 }
michael@0 209
michael@0 210 template <typename T>
michael@0 211 inline TextOutput& operator<<(TextOutput& to, const sp<T>& val)
michael@0 212 {
michael@0 213 return printStrongPointer(to, val.get());
michael@0 214 }
michael@0 215
michael@0 216 }; // namespace android
michael@0 217
michael@0 218 // ---------------------------------------------------------------------------
michael@0 219
michael@0 220 #endif // ANDROID_STRONG_POINTER_H

mercurial