/**************************************************************************** Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd. http://www.cocos.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #pragma once #include #include #include #include "base/Ptr.h" #include "base/RefCounted.h" #include "base/memory/Memory.h" #include "base/HasMemberFunction.h" namespace se { class Object; class State; class ScriptEngine; template class TypedPrivateObject; class PrivateObjectBase { public: virtual ~PrivateObjectBase() = default; template inline T *get() const { return reinterpret_cast(getRaw()); } template inline TypedPrivateObject *typed() { return reinterpret_cast *>(this); } virtual const char *getName() const = 0; virtual void *getRaw() const = 0; virtual void allowDestroyInGC() const { CC_ABORT(); } virtual void tryAllowDestroyInGC() const {} virtual bool isSharedPtr() const { return false; } virtual bool isCCIntrusivePtr() const { return false; } friend se::Object; friend se::State; friend se::ScriptEngine; void *finalizerData{nullptr}; }; template class TypedPrivateObject : public PrivateObjectBase { public: inline std::shared_ptr share(); inline cc::IntrusivePtr &ccShared(); inline const char *getName() const override { static_assert(!std::is_base_of::value, ""); // NOLINT // remove after using c++17 return typeid(T).name(); } }; template class SharedPtrPrivateObject final : public TypedPrivateObject { public: SharedPtrPrivateObject() = default; explicit SharedPtrPrivateObject(const std::shared_ptr &ptr) : _data(ptr) {} explicit SharedPtrPrivateObject(std::shared_ptr &&ptr) : _data(std::move(ptr)) {} inline const std::shared_ptr &getData() const { return _data; } inline std::shared_ptr &getData() { return _data; } constexpr bool isSharedPtr() const override { return true; } void *getRaw() const override { if constexpr (std::is_const_v) { return reinterpret_cast(const_cast *>(_data.get())); } else { return reinterpret_cast(_data.get()); } } private: std::shared_ptr _data{nullptr}; }; template class CCIntrusivePtrPrivateObject final : public TypedPrivateObject { public: CCIntrusivePtrPrivateObject() = default; explicit CCIntrusivePtrPrivateObject(const cc::IntrusivePtr &p) : _ptr(p) {} explicit CCIntrusivePtrPrivateObject(cc::IntrusivePtr &&p) : _ptr(std::move(p)) {} ~CCIntrusivePtrPrivateObject() override { if constexpr (cc::has_setScriptObject::value) { _ptr->setScriptObject(nullptr); } } inline const cc::IntrusivePtr &getData() const { return _ptr; } inline cc::IntrusivePtr &getData() { return _ptr; } inline void *getRaw() const override { if constexpr (std::is_const_v) { return reinterpret_cast(const_cast *>(_ptr.get())); } else { return reinterpret_cast(_ptr.get()); } } inline bool isCCIntrusivePtr() const override { return true; } private: cc::IntrusivePtr _ptr; friend TypedPrivateObject; }; template class RawRefPrivateObject final : public TypedPrivateObject { public: RawRefPrivateObject() = default; explicit RawRefPrivateObject(T *p) : _ptr(p) {} ~RawRefPrivateObject() override { static_assert(!std::is_same::value, "void is not allowed!"); if constexpr (std::is_destructible::value) { if (_allowGC) { delete _ptr; } } _ptr = nullptr; } void allowDestroyInGC() const override { _allowGC = true; } void tryAllowDestroyInGC() const override { allowDestroyInGC(); } void *getRaw() const override { // CC_ASSERT(_validate); if constexpr (std::is_const_v) { return reinterpret_cast(const_cast *>(_ptr)); } else { return reinterpret_cast(_ptr); } } private: T *_ptr = nullptr; // bool _validate = true; mutable bool _allowGC = false; }; template inline std::shared_ptr TypedPrivateObject::share() { if (isSharedPtr()) { return reinterpret_cast *>(this)->getData(); } CC_ABORT(); return std::shared_ptr(nullptr); } template inline cc::IntrusivePtr &TypedPrivateObject::ccShared() { CC_ASSERT(isCCIntrusivePtr()); return reinterpret_cast *>(this)->_ptr; } #if CC_DEBUG inline void inHeap(void *ptr) { constexpr size_t r = 4 * 1024; // 4K char a; auto anchor = reinterpret_cast(&a); auto p = reinterpret_cast(ptr); // must be in heaps CC_ASSERT(std::abs(anchor - p) > r); } #endif template inline auto *make_shared_private_object(T *cobj) { // NOLINT static_assert(!std::is_same::value, "void * is not allowed"); // static_assert(!std::is_pointer_v && !std::is_null_pointer_v, "bad pointer"); #if CC_DEBUG inHeap(cobj); #endif if constexpr (std::is_base_of::value) { return ccnew CCIntrusivePtrPrivateObject(cc::IntrusivePtr(cobj)); } else { return ccnew SharedPtrPrivateObject(std::shared_ptr(cobj)); } } template inline auto *shared_ptr_private_object(std::shared_ptr &&ptr) { // NOLINT static_assert(!std::is_base_of::value, "cc::RefCounted is not acceptable for shared_ptr"); return ccnew SharedPtrPrivateObject(std::forward>(ptr)); } template inline auto *shared_ptr_private_object(const std::shared_ptr &ptr) { // NOLINT static_assert(!std::is_base_of::value, "cc::RefCounted is not acceptable for shared_ptr"); return ccnew SharedPtrPrivateObject(ptr); } template inline auto *rawref_private_object(T *ptr) { // NOLINT // static_assert(false, "always fail"); // static_assert(!std::is_base_of::value, "cc::RefCounted is not acceptable for shared_ptr"); #if CC_DEBUG inHeap(ptr); #endif return ccnew RawRefPrivateObject(ptr); } template inline auto *ccintrusive_ptr_private_object(const cc::IntrusivePtr &ptr) { // NOLINT static_assert(std::is_base_of::value, "cc::RefCounted expected!"); return ccnew CCIntrusivePtrPrivateObject(ptr); } template inline auto *ccintrusive_ptr_private_object(T *cobj) { // NOLINT static_assert(std::is_base_of::value, "cc::RefCounted expected!"); return ccnew CCIntrusivePtrPrivateObject(cc::IntrusivePtr(cobj)); } } // namespace se