Skip to content

Commit

Permalink
Improvements to unityW
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBrumbler committed Jan 24, 2024
1 parent b09416f commit 7ca5a2b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
6 changes: 5 additions & 1 deletion include/cordl_internals/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ struct FieldException : public ::il2cpp_utils::exceptions::StackTraceException {
struct NullException : public ::il2cpp_utils::exceptions::StackTraceException {
using StackTraceException::StackTraceException;
};

struct CastException : public ::il2cpp_utils::exceptions::StackTraceException {
using StackTraceException::StackTraceException;
};
} // namespace cordl_internals
} // end anonymous namespace
} // end anonymous namespace
18 changes: 13 additions & 5 deletions include/cordl_internals/unity-utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#define __UNITYW_UNITY_NULL_HANDLE_CHECK(...) \
if (isAlive()) \
return __VA_ARGS__; \
CRASH_UNLESS(false)
SAFE_ABORT_MSG(std::string(typeid(*this).name()) + "Unity object with null handle was accessed!")
#endif

template <typename T> struct UnityW {
Expand All @@ -27,18 +27,21 @@ template <typename T> struct UnityW {
requires(std::is_convertible_v<U *, T *>)
constexpr UnityW(UnityW<U> u) : innerPtr(u.innerPtr) {}

constexpr UnityW(nullptr_t) noexcept : innerPtr(nullptr) {}
explicit constexpr UnityW(void *p) noexcept : innerPtr(static_cast<T *>(p)) {}

constexpr void *convert() const noexcept {
return const_cast<void *>(static_cast<void const *>(unsafePtr()));
}

template <typename U> inline UnityW<U> cast() const {
return UnityW<U>(::il2cpp_utils::cast<U>(const_cast<T *>(this->innerPtr)));
auto attemptedCast = try_cast<U>();
if (!attemptedCast) throw ::cordl_internals::CastException(std::string(typeid(*this).name()) + " could not cast to " + std::string(typeid(UnityW<U>).name()));
return UnityW<U>(attemptedCast.value());
}

template <typename U> inline std::optional<UnityW<U>> try_cast() const {
auto attemptedCast =
::il2cpp_utils::try_cast<U>(const_cast<T *>(this->innerPtr));
auto attemptedCast = ::il2cpp_utils::try_cast<U>(const_cast<T *>(this->innerPtr));
if (!attemptedCast)
return std::nullopt;

Expand All @@ -54,14 +57,18 @@ template <typename T> struct UnityW {
constexpr T const *ptr() const { __UNITYW_UNITY_NULL_HANDLE_CHECK(innerPtr); }

/// @brief Implicitly cast this instance to a T*.
constexpr operator T *() const { return const_cast<T *>(ptr()); }
constexpr operator T *() const noexcept { return const_cast<T *>(unsafePtr()); }

/// @brief access underlying pointer with ->
constexpr T *operator->() { return const_cast<T *>(ptr()); }

/// @brief access underlying pointer with ->
constexpr T const *operator->() const { return ptr(); }

/// @brief get access to the underlying object as a reference
constexpr T &operator*() { return *ptr(); }

/// @brief get access to the underlying object as a reference
constexpr T const &operator*() const { return *ptr(); }

constexpr operator bool() const { return isAlive(); }
Expand All @@ -88,6 +95,7 @@ template <typename T> struct UnityW {
return ptr->m_CachedPtr;
}


private:
T *innerPtr;
};
Expand Down

0 comments on commit 7ca5a2b

Please sign in to comment.