/* * 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 #include // for common_type. // --------------------------------------------------------------------------- namespace android { template class wp; // --------------------------------------------------------------------------- template class sp { public: inline sp() : m_ptr(nullptr) { } // TODO: switch everyone to using this over new, and make RefBase operator // new private to that class so that we can avoid RefBase being used with // other memory management mechanisms. template static inline sp make(Args&&... args); sp(T* other); // NOLINT(implicit) sp(const sp& other); sp(sp&& other) noexcept; template sp(U* other); // NOLINT(implicit) template sp(const sp& other); // NOLINT(implicit) template sp(sp&& other); // NOLINT(implicit) ~sp(); // Assignment sp& operator = (T* other); sp& operator = (const sp& other); sp& operator=(sp&& other) noexcept; template sp& operator = (const sp& other); template sp& operator = (sp&& other); template 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; } inline explicit operator bool () const { return m_ptr != nullptr; } // Punt these to the wp<> implementation. template inline bool operator == (const wp& o) const { return o == *this; } template inline bool operator != (const wp& o) const { return o != *this; } private: template friend class sp; template friend class wp; void set_pointer(T* ptr); static inline void check_not_on_stack(const void* ptr); T* m_ptr; }; #define COMPARE_STRONG(_op_) \ template \ static inline bool operator _op_(const sp& t, const sp& u) { \ return t.get() _op_ u.get(); \ } \ template \ static inline bool operator _op_(const T* t, const sp& u) { \ return t _op_ u.get(); \ } \ template \ static inline bool operator _op_(const sp& t, const U* u) { \ return t.get() _op_ u; \ } \ template \ static inline bool operator _op_(const sp& t, std::nullptr_t) { \ return t.get() _op_ nullptr; \ } \ template \ static inline bool operator _op_(std::nullptr_t, const sp& t) { \ return nullptr _op_ t.get(); \ } template