17b5b82d64
system/core/include is included in the global include path using -isystem, which hides all warnings. Fix warnings in libutils headers in preparation for moving from -isystem to -I. - Fix implicit cast from int64_t to long in Condition.h. Remove the __LP64__ check and always compare against LONG_MAX before casting. - Fix implicit cast from size_t to ssize_t in KeyedVector.h - Fix -Wshadow-field-in-constructor warnings in Looper.h and RefBase.h - Move destructors for MessageHandler and LooperCallback to Looper.cpp and ReferenceRenamer and VirtualLightRefBase to RefBase.cpp to prevent vtables in every compilation unit. - Declare template variables in Singleton.h - Fix old-style casts in StrongPointer.h and TypeHelpers.h - Use template metaprogramming in TypeHelpers.h to avoid warnings on memmove on non-trivial types. - Add an assignment operator to key_value_pair_t to complete rule-of-three - Use memcpy instead of dereferencing a reinterpret_casted pointer to treat the bits of a float or double as int32_t or int64_t - Escape unicode sequences inside doxygen comments between \code and \endcode - Remove WIN32 ZD definition in Compat.h, %zd works fine with mingw - Fix WIN32 printf warnings in Filemap.cpp - Initialize mNullValue with 0 in LruCache.h, some of the tests use a non-pointer type for TValue. Test: m -j native Bug: 31492149 Change-Id: I385a05a3ca01258e44fe3b37ef77e4aaff547b26
245 lines
6.3 KiB
C++
245 lines
6.3 KiB
C++
/*
|
|
* 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; \
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
class sp {
|
|
public:
|
|
inline sp() : m_ptr(0) { }
|
|
|
|
sp(T* other); // NOLINT(implicit)
|
|
sp(const sp<T>& other);
|
|
sp(sp<T>&& other);
|
|
template<typename U> sp(U* other); // NOLINT(implicit)
|
|
template<typename U> sp(const sp<U>& other); // NOLINT(implicit)
|
|
template<typename U> sp(sp<U>&& other); // NOLINT(implicit)
|
|
|
|
~sp();
|
|
|
|
// Assignment
|
|
|
|
sp& operator = (T* other);
|
|
sp& operator = (const sp<T>& other);
|
|
sp& operator = (sp<T>&& other);
|
|
|
|
template<typename U> sp& operator = (const sp<U>& other);
|
|
template<typename U> sp& operator = (sp<U>&& other);
|
|
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;
|
|
void set_pointer(T* ptr);
|
|
T* m_ptr;
|
|
};
|
|
|
|
#undef COMPARE
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// No user serviceable parts below here.
|
|
|
|
template<typename T>
|
|
sp<T>::sp(T* other)
|
|
: m_ptr(other) {
|
|
if (other)
|
|
other->incStrong(this);
|
|
}
|
|
|
|
template<typename T>
|
|
sp<T>::sp(const sp<T>& other)
|
|
: m_ptr(other.m_ptr) {
|
|
if (m_ptr)
|
|
m_ptr->incStrong(this);
|
|
}
|
|
|
|
template<typename T>
|
|
sp<T>::sp(sp<T>&& other)
|
|
: m_ptr(other.m_ptr) {
|
|
other.m_ptr = nullptr;
|
|
}
|
|
|
|
template<typename T> template<typename U>
|
|
sp<T>::sp(U* other)
|
|
: m_ptr(other) {
|
|
if (other)
|
|
(static_cast<T*>(other))->incStrong(this);
|
|
}
|
|
|
|
template<typename T> template<typename U>
|
|
sp<T>::sp(const sp<U>& other)
|
|
: m_ptr(other.m_ptr) {
|
|
if (m_ptr)
|
|
m_ptr->incStrong(this);
|
|
}
|
|
|
|
template<typename T> template<typename U>
|
|
sp<T>::sp(sp<U>&& other)
|
|
: m_ptr(other.m_ptr) {
|
|
other.m_ptr = nullptr;
|
|
}
|
|
|
|
template<typename T>
|
|
sp<T>::~sp() {
|
|
if (m_ptr)
|
|
m_ptr->decStrong(this);
|
|
}
|
|
|
|
template<typename T>
|
|
sp<T>& sp<T>::operator =(const sp<T>& other) {
|
|
T* otherPtr(other.m_ptr);
|
|
if (otherPtr)
|
|
otherPtr->incStrong(this);
|
|
if (m_ptr)
|
|
m_ptr->decStrong(this);
|
|
m_ptr = otherPtr;
|
|
return *this;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
template<typename T>
|
|
sp<T>& sp<T>::operator =(T* other) {
|
|
if (other)
|
|
other->incStrong(this);
|
|
if (m_ptr)
|
|
m_ptr->decStrong(this);
|
|
m_ptr = other;
|
|
return *this;
|
|
}
|
|
|
|
template<typename T> template<typename U>
|
|
sp<T>& sp<T>::operator =(const sp<U>& other) {
|
|
T* otherPtr(other.m_ptr);
|
|
if (otherPtr)
|
|
otherPtr->incStrong(this);
|
|
if (m_ptr)
|
|
m_ptr->decStrong(this);
|
|
m_ptr = otherPtr;
|
|
return *this;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
template<typename T> template<typename U>
|
|
sp<T>& sp<T>::operator =(U* other) {
|
|
if (other)
|
|
(static_cast<T*>(other))->incStrong(this);
|
|
if (m_ptr)
|
|
m_ptr->decStrong(this);
|
|
m_ptr = other;
|
|
return *this;
|
|
}
|
|
|
|
template<typename T>
|
|
void sp<T>::force_set(T* other) {
|
|
other->forceIncStrong(this);
|
|
m_ptr = other;
|
|
}
|
|
|
|
template<typename T>
|
|
void sp<T>::clear() {
|
|
if (m_ptr) {
|
|
m_ptr->decStrong(this);
|
|
m_ptr = 0;
|
|
}
|
|
}
|
|
|
|
template<typename T>
|
|
void sp<T>::set_pointer(T* ptr) {
|
|
m_ptr = ptr;
|
|
}
|
|
|
|
}; // namespace android
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
#endif // ANDROID_STRONG_POINTER_H
|