/* * Copyright 2006 The Android Open Source Project * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef SkRefCnt_DEFINED #define SkRefCnt_DEFINED #include "include/core/SkTypes.h" #include "include/private/base/SkDebug.h" #include <atomic> #include <cstddef> #include <cstdint> #include <iosfwd> #include <type_traits> #include <utility> /** \class SkRefCntBase SkRefCntBase is the base class for objects that may be shared by multiple objects. When an existing owner wants to share a reference, it calls ref(). When an owner wants to release its reference, it calls unref(). When the shared object's reference count goes to zero as the result of an unref() call, its (virtual) destructor is called. It is an error for the destructor to be called explicitly (or via the object going out of scope on the stack or calling delete) if getRefCnt() > 1. */ class SK_API SkRefCntBase { … }; #ifdef SK_REF_CNT_MIXIN_INCLUDE // It is the responsibility of the following include to define the type SkRefCnt. // This SkRefCnt should normally derive from SkRefCntBase. #include SK_REF_CNT_MIXIN_INCLUDE #else class SK_API SkRefCnt : public SkRefCntBase { // "#include SK_REF_CNT_MIXIN_INCLUDE" doesn't work with this build system. #if defined(SK_BUILD_FOR_GOOGLE3) public: void deref() const { this->unref(); } #endif }; #endif /////////////////////////////////////////////////////////////////////////////// /** Call obj->ref() and return obj. The obj must not be nullptr. */ template <typename T> static inline T* SkRef(T* obj) { … } /** Check if the argument is non-null, and if so, call obj->ref() and return obj. */ template <typename T> static inline T* SkSafeRef(T* obj) { … } /** Check if the argument is non-null, and if so, call obj->unref() */ template <typename T> static inline void SkSafeUnref(T* obj) { … } /////////////////////////////////////////////////////////////////////////////// // This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead of 8 or 16. // There's only benefit to using this if the deriving class does not otherwise need a vtable. template <typename Derived> class SkNVRefCnt { … }; /////////////////////////////////////////////////////////////////////////////////////////////////// /** * Shared pointer class to wrap classes that support a ref()/unref() interface. * * This can be used for classes inheriting from SkRefCnt, but it also works for other * classes that match the interface, but have different internal choices: e.g. the hosted class * may have its ref/unref be thread-safe, but that is not assumed/imposed by sk_sp. * * Declared with the trivial_abi attribute where supported so that sk_sp and types containing it * may be considered as trivially relocatable by the compiler so that destroying-move operations * i.e. move constructor followed by destructor can be optimized to memcpy. */ template <typename T> class SK_TRIVIAL_ABI sk_sp { … }; template <typename T> inline void swap(sk_sp<T>& a, sk_sp<T>& b) /*noexcept*/ { … } template <typename T, typename U> inline bool operator==(const sk_sp<T>& a, const sk_sp<U>& b) { … } template <typename T> inline bool operator==(const sk_sp<T>& a, std::nullptr_t) /*noexcept*/ { … } template <typename T> inline bool operator==(std::nullptr_t, const sk_sp<T>& b) /*noexcept*/ { … } template <typename T, typename U> inline bool operator!=(const sk_sp<T>& a, const sk_sp<U>& b) { … } template <typename T> inline bool operator!=(const sk_sp<T>& a, std::nullptr_t) /*noexcept*/ { … } template <typename T> inline bool operator!=(std::nullptr_t, const sk_sp<T>& b) /*noexcept*/ { … } template <typename C, typename CT, typename T> auto operator<<(std::basic_ostream<C, CT>& os, const sk_sp<T>& sp) -> decltype(os << sp.get()) { … } template <typename T> sk_sp(T*) -> sk_sp<T>; template <typename T, typename... Args> sk_sp<T> sk_make_sp(Args&&... args) { … } /* * Returns a sk_sp wrapping the provided ptr AND calls ref on it (if not null). * * This is different than the semantics of the constructor for sk_sp, which just wraps the ptr, * effectively "adopting" it. */ template <typename T> sk_sp<T> sk_ref_sp(T* obj) { … } template <typename T> sk_sp<T> sk_ref_sp(const T* obj) { … } #endif