2011-02-17 05:23:43 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ANDROID_STRONG_POINTER_H
|
|
|
|
#define ANDROID_STRONG_POINTER_H
|
|
|
|
|
|
|
|
#include <cutils/atomic.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
template<typename T> class wp;
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define COMPARE(_op_) \
|
|
|
|
inline bool operator _op_ (const sp<T>& o) const { \
|
|
|
|
return m_ptr _op_ o.m_ptr; \
|
|
|
|
} \
|
|
|
|
inline bool operator _op_ (const T* o) const { \
|
|
|
|
return m_ptr _op_ o; \
|
|
|
|
} \
|
|
|
|
template<typename U> \
|
|
|
|
inline bool operator _op_ (const sp<U>& o) const { \
|
|
|
|
return m_ptr _op_ o.m_ptr; \
|
|
|
|
} \
|
|
|
|
template<typename U> \
|
|
|
|
inline bool operator _op_ (const U* o) const { \
|
|
|
|
return m_ptr _op_ o; \
|
|
|
|
} \
|
|
|
|
inline bool operator _op_ (const wp<T>& o) const { \
|
|
|
|
return m_ptr _op_ o.m_ptr; \
|
|
|
|
} \
|
|
|
|
template<typename U> \
|
|
|
|
inline bool operator _op_ (const wp<U>& o) const { \
|
|
|
|
return m_ptr _op_ o.m_ptr; \
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2013-05-09 22:04:32 +02:00
|
|
|
template<typename T>
|
|
|
|
class sp {
|
2011-02-17 05:23:43 +01:00
|
|
|
public:
|
|
|
|
inline sp() : m_ptr(0) { }
|
|
|
|
|
|
|
|
sp(T* other);
|
|
|
|
sp(const sp<T>& other);
|
2015-10-28 23:36:33 +01:00
|
|
|
sp(sp<T>&& other);
|
2011-02-17 05:23:43 +01:00
|
|
|
template<typename U> sp(U* other);
|
|
|
|
template<typename U> sp(const sp<U>& other);
|
2015-10-28 23:36:33 +01:00
|
|
|
template<typename U> sp(sp<U>&& other);
|
2011-02-17 05:23:43 +01:00
|
|
|
|
|
|
|
~sp();
|
|
|
|
|
|
|
|
// Assignment
|
|
|
|
|
|
|
|
sp& operator = (T* other);
|
|
|
|
sp& operator = (const sp<T>& other);
|
2015-10-28 23:36:33 +01:00
|
|
|
sp& operator = (sp<T>&& other);
|
2011-02-17 05:23:43 +01:00
|
|
|
|
|
|
|
template<typename U> sp& operator = (const sp<U>& other);
|
2015-10-28 23:36:33 +01:00
|
|
|
template<typename U> sp& operator = (sp<U>&& other);
|
2011-02-17 05:23:43 +01:00
|
|
|
template<typename U> sp& operator = (U* other);
|
|
|
|
|
|
|
|
//! Special optimization for use by ProcessState (and nobody else).
|
|
|
|
void force_set(T* other);
|
|
|
|
|
|
|
|
// Reset
|
|
|
|
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
// Accessors
|
|
|
|
|
|
|
|
inline T& operator* () const { return *m_ptr; }
|
|
|
|
inline T* operator-> () const { return m_ptr; }
|
|
|
|
inline T* get() const { return m_ptr; }
|
|
|
|
|
|
|
|
// Operators
|
|
|
|
|
|
|
|
COMPARE(==)
|
|
|
|
COMPARE(!=)
|
|
|
|
COMPARE(>)
|
|
|
|
COMPARE(<)
|
|
|
|
COMPARE(<=)
|
|
|
|
COMPARE(>=)
|
|
|
|
|
|
|
|
private:
|
|
|
|
template<typename Y> friend class sp;
|
|
|
|
template<typename Y> friend class wp;
|
2011-02-25 03:12:34 +01:00
|
|
|
void set_pointer(T* ptr);
|
|
|
|
T* m_ptr;
|
2011-02-17 05:23:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#undef COMPARE
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// No user serviceable parts below here.
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
sp<T>::sp(T* other)
|
2013-05-09 22:04:32 +02:00
|
|
|
: m_ptr(other) {
|
|
|
|
if (other)
|
|
|
|
other->incStrong(this);
|
|
|
|
}
|
2011-02-17 05:23:43 +01:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
sp<T>::sp(const sp<T>& other)
|
2013-05-09 22:04:32 +02:00
|
|
|
: m_ptr(other.m_ptr) {
|
|
|
|
if (m_ptr)
|
|
|
|
m_ptr->incStrong(this);
|
|
|
|
}
|
2011-02-17 05:23:43 +01:00
|
|
|
|
2015-10-28 23:36:33 +01:00
|
|
|
template<typename T>
|
|
|
|
sp<T>::sp(sp<T>&& other)
|
|
|
|
: m_ptr(other.m_ptr) {
|
|
|
|
other.m_ptr = nullptr;
|
|
|
|
}
|
|
|
|
|
2011-02-17 05:23:43 +01:00
|
|
|
template<typename T> template<typename U>
|
2013-05-09 22:04:32 +02:00
|
|
|
sp<T>::sp(U* other)
|
|
|
|
: m_ptr(other) {
|
|
|
|
if (other)
|
|
|
|
((T*) other)->incStrong(this);
|
2011-02-17 05:23:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> template<typename U>
|
|
|
|
sp<T>::sp(const sp<U>& other)
|
2013-05-09 22:04:32 +02:00
|
|
|
: m_ptr(other.m_ptr) {
|
|
|
|
if (m_ptr)
|
|
|
|
m_ptr->incStrong(this);
|
|
|
|
}
|
2011-02-17 05:23:43 +01:00
|
|
|
|
2015-10-28 23:36:33 +01:00
|
|
|
template<typename T> template<typename U>
|
|
|
|
sp<T>::sp(sp<U>&& other)
|
|
|
|
: m_ptr(other.m_ptr) {
|
|
|
|
other.m_ptr = nullptr;
|
|
|
|
}
|
|
|
|
|
2011-02-17 05:23:43 +01:00
|
|
|
template<typename T>
|
2013-05-09 22:04:32 +02:00
|
|
|
sp<T>::~sp() {
|
|
|
|
if (m_ptr)
|
|
|
|
m_ptr->decStrong(this);
|
2011-02-17 05:23:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2013-05-09 22:04:32 +02:00
|
|
|
sp<T>& sp<T>::operator =(const sp<T>& other) {
|
2011-02-17 05:23:43 +01:00
|
|
|
T* otherPtr(other.m_ptr);
|
2013-05-09 22:04:32 +02:00
|
|
|
if (otherPtr)
|
|
|
|
otherPtr->incStrong(this);
|
|
|
|
if (m_ptr)
|
|
|
|
m_ptr->decStrong(this);
|
2011-02-17 05:23:43 +01:00
|
|
|
m_ptr = otherPtr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-10-28 23:36:33 +01:00
|
|
|
template<typename T>
|
|
|
|
sp<T>& sp<T>::operator =(sp<T>&& other) {
|
|
|
|
if (m_ptr)
|
|
|
|
m_ptr->decStrong(this);
|
|
|
|
m_ptr = other.m_ptr;
|
|
|
|
other.m_ptr = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-02-17 05:23:43 +01:00
|
|
|
template<typename T>
|
2013-05-09 22:04:32 +02:00
|
|
|
sp<T>& sp<T>::operator =(T* other) {
|
|
|
|
if (other)
|
|
|
|
other->incStrong(this);
|
|
|
|
if (m_ptr)
|
|
|
|
m_ptr->decStrong(this);
|
2011-02-17 05:23:43 +01:00
|
|
|
m_ptr = other;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> template<typename U>
|
2013-05-09 22:04:32 +02:00
|
|
|
sp<T>& sp<T>::operator =(const sp<U>& other) {
|
2011-02-26 01:11:44 +01:00
|
|
|
T* otherPtr(other.m_ptr);
|
2013-05-09 22:04:32 +02:00
|
|
|
if (otherPtr)
|
|
|
|
otherPtr->incStrong(this);
|
|
|
|
if (m_ptr)
|
|
|
|
m_ptr->decStrong(this);
|
2011-02-17 05:23:43 +01:00
|
|
|
m_ptr = otherPtr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-10-28 23:36:33 +01:00
|
|
|
template<typename T> template<typename U>
|
|
|
|
sp<T>& sp<T>::operator =(sp<U>&& other) {
|
|
|
|
if (m_ptr)
|
|
|
|
m_ptr->decStrong(this);
|
|
|
|
m_ptr = other.m_ptr;
|
|
|
|
other.m_ptr = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-02-17 05:23:43 +01:00
|
|
|
template<typename T> template<typename U>
|
2013-05-09 22:04:32 +02:00
|
|
|
sp<T>& sp<T>::operator =(U* other) {
|
|
|
|
if (other)
|
|
|
|
((T*) other)->incStrong(this);
|
|
|
|
if (m_ptr)
|
|
|
|
m_ptr->decStrong(this);
|
2011-02-17 05:23:43 +01:00
|
|
|
m_ptr = other;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-05-09 22:04:32 +02:00
|
|
|
template<typename T>
|
|
|
|
void sp<T>::force_set(T* other) {
|
2011-02-17 05:23:43 +01:00
|
|
|
other->forceIncStrong(this);
|
|
|
|
m_ptr = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2013-05-09 22:04:32 +02:00
|
|
|
void sp<T>::clear() {
|
2011-02-17 05:23:43 +01:00
|
|
|
if (m_ptr) {
|
|
|
|
m_ptr->decStrong(this);
|
|
|
|
m_ptr = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2011-02-25 03:12:34 +01:00
|
|
|
void sp<T>::set_pointer(T* ptr) {
|
|
|
|
m_ptr = ptr;
|
|
|
|
}
|
2011-02-17 05:23:43 +01:00
|
|
|
|
|
|
|
}; // namespace android
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif // ANDROID_STRONG_POINTER_H
|