diff --git a/AK/Ptr32.h b/AK/Ptr32.h index 5c2b9b738d..d3e458d8aa 100644 --- a/AK/Ptr32.h +++ b/AK/Ptr32.h @@ -8,29 +8,47 @@ #pragma once #include +#include namespace AK { template class Ptr32 { public: - Ptr32() = default; - Ptr32(T* ptr) - : m_ptr((u32) reinterpret_cast(ptr)) + constexpr Ptr32() = default; + Ptr32(T* const ptr) + : m_ptr((u32) reinterpret_cast(ptr)) { - VERIFY((reinterpret_cast(ptr) & 0xFFFFFFFF) == m_ptr); + VERIFY((reinterpret_cast(ptr) & 0xFFFFFFFFULL) == reinterpret_cast(m_ptr)); } - // inline T* operator*() { return (T*)m_ptr; } - // inline const T* operator*() const { return (T*)m_ptr; } + T& operator*() { return *static_cast(*this); } + T const& operator*() const { return *static_cast(*this); } - inline T* operator->() + T* operator->() { return *this; } + T const* operator->() const { return *this; } + + operator T*() { return reinterpret_cast(static_cast(m_ptr)); } + operator T const *() const { return reinterpret_cast(static_cast(m_ptr)); } + + T& operator[](size_t index) { return static_cast(*this)[index]; } + T const& operator[](size_t index) const { return static_cast(*this)[index]; } + + constexpr explicit operator bool() { return m_ptr; } + template + constexpr bool operator==(Ptr32 other) { return m_ptr == other.m_ptr; } + + constexpr Ptr32 operator+(u32 other) const { - VERIFY(m_ptr); - return m_ptr; + Ptr32 ptr {}; + ptr.m_ptr = m_ptr + other; + return ptr; + } + constexpr Ptr32 operator-(u32 other) const + { + Ptr32 ptr {}; + ptr.m_ptr = m_ptr - other; + return ptr; } - - inline operator T*() { return (T*)static_cast(m_ptr); } - inline operator const T*() const { return (T*)static_cast(m_ptr); } private: u32 m_ptr { 0 };