From 3c92a16bd382987395ed846b69e71090b61c1188 Mon Sep 17 00:00:00 2001 From: RedBrumbler <51795403+RedBrumbler@users.noreply.github.com> Date: Sat, 13 Aug 2022 22:49:06 +0200 Subject: [PATCH 1/3] Update SafePtr operator bool to make more sense --- shared/utils/typedefs-wrappers.hpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/shared/utils/typedefs-wrappers.hpp b/shared/utils/typedefs-wrappers.hpp index e6dc6191..573d1a5a 100644 --- a/shared/utils/typedefs-wrappers.hpp +++ b/shared/utils/typedefs-wrappers.hpp @@ -381,11 +381,9 @@ struct SafePtr { __SAFE_PTR_NULL_HANDLE_CHECK(internalHandle, internalHandle->instancePointer); } - /// @brief Returns false if this is a defaultly constructed SafePtr, true otherwise. - /// Note that this means that it will return true if it holds a nullptr value explicitly! - /// This means that you should check yourself before calling anything using the held T*. + /// @brief Returns false if this is a defaultly constructed SafePtr, or if the held pointer evaluates to false operator bool() const noexcept { - return isHandleValid(); + return isHandleValid() && ptr(); } /// @brief Dereferences the instance pointer to a reference type of the held instance. @@ -796,4 +794,4 @@ template using EventCallback = BasicEventCallback; template -using UnorderedEventCallback = BasicEventCallback; \ No newline at end of file +using UnorderedEventCallback = BasicEventCallback; From da08a27fa0703dd0a7e7edb083b3806ac6310b3c Mon Sep 17 00:00:00 2001 From: RedBrumbler <51795403+RedBrumbler@users.noreply.github.com> Date: Sat, 13 Aug 2022 23:15:52 +0200 Subject: [PATCH 2/3] add some nodiscard --- shared/utils/typedefs-wrappers.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/shared/utils/typedefs-wrappers.hpp b/shared/utils/typedefs-wrappers.hpp index 573d1a5a..fd8b8bd5 100644 --- a/shared/utils/typedefs-wrappers.hpp +++ b/shared/utils/typedefs-wrappers.hpp @@ -388,22 +388,22 @@ struct SafePtr { /// @brief Dereferences the instance pointer to a reference type of the held instance. /// Throws a NullHandleException if there is no internal handle. - T& operator *() { + [[nodiscard]] T& operator *() { return *ptr(); } - const T& operator *() const { + [[nodiscard]] const T& operator *() const { return *ptr(); } - T* const operator ->() const { + [[nodiscard]] T* const operator ->() const { return const_cast(ptr()); } /// @brief Explicitly cast this instance to a T*. /// Note, however, that the lifetime of this returned T* is not longer than the lifetime of this instance. /// Consider passing a SafePtr reference or copy instead. - explicit operator T* const() const { + [[nodiscard]] explicit operator T* const() const { return const_cast(ptr()); } From 194d1810b7181eac4a492548c69342ee54193bc1 Mon Sep 17 00:00:00 2001 From: RedBrumbler <51795403+RedBrumbler@users.noreply.github.com> Date: Sat, 13 Aug 2022 23:40:57 +0200 Subject: [PATCH 3/3] Update typedefs-wrappers.hpp --- shared/utils/typedefs-wrappers.hpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/shared/utils/typedefs-wrappers.hpp b/shared/utils/typedefs-wrappers.hpp index fd8b8bd5..fd0967cd 100644 --- a/shared/utils/typedefs-wrappers.hpp +++ b/shared/utils/typedefs-wrappers.hpp @@ -243,8 +243,12 @@ struct SafePtr { /// @brief Construct a SafePtr with the provided instance pointer (which may be nullptr). /// If you wish to wrap a non-existent pointer (ex, use as a default constructor) see the 0 arg constructor instead. SafePtr(T* wrappableInstance) : internalHandle(SafePointerWrapper::New(wrappableInstance)) {} + /// @brief Construct a SafePtr with the provided wrapper + SafePtr(T& wrappableInstance) requires(il2cpp_utils::has_il2cpp_conversion) : internalHandle(SafePointerWrapper::New(wrappableInstance.convert())) {} + /// @brief Construct a SafePtr with the provided wrapper + SafePtr(T&& wrappableInstance) requires(il2cpp_utils::has_il2cpp_conversion) : internalHandle(SafePointerWrapper::New(wrappableInstance.convert())) {} /// @brief Construct a SafePtr with the provided reference - SafePtr(T& wrappableInstance) : internalHandle(SafePointerWrapper::New(std::addressof(wrappableInstance))) {} + SafePtr(T& wrappableInstance) requires(!il2cpp_utils::has_il2cpp_conversion) : internalHandle(SafePointerWrapper::New(std::addressof(wrappableInstance))) {} /// @brief Move constructor is default, moves the internal handle and keeps reference count the same. SafePtr(SafePtr&& other) = default; /// @brief Copy constructor copies the HANDLE, that is, the held pointer remains the same.