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