mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:07:45 +00:00
AK: Make Ptr32 more transparent
This adds a transparent dereference operator, aswell as a constant arrow operator Also increaces the verbosity of the code
This commit is contained in:
parent
06f1edb516
commit
8a739f986a
1 changed files with 30 additions and 12 deletions
42
AK/Ptr32.h
42
AK/Ptr32.h
|
@ -8,29 +8,47 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/Assertions.h>
|
#include <AK/Assertions.h>
|
||||||
|
#include <AK/Types.h>
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class Ptr32 {
|
class Ptr32 {
|
||||||
public:
|
public:
|
||||||
Ptr32() = default;
|
constexpr Ptr32() = default;
|
||||||
Ptr32(T* ptr)
|
Ptr32(T* const ptr)
|
||||||
: m_ptr((u32) reinterpret_cast<uintptr_t>(ptr))
|
: m_ptr((u32) reinterpret_cast<FlatPtr>(ptr))
|
||||||
{
|
{
|
||||||
VERIFY((reinterpret_cast<uintptr_t>(ptr) & 0xFFFFFFFF) == m_ptr);
|
VERIFY((reinterpret_cast<FlatPtr>(ptr) & 0xFFFFFFFFULL) == reinterpret_cast<FlatPtr>(m_ptr));
|
||||||
}
|
}
|
||||||
// inline T* operator*() { return (T*)m_ptr; }
|
T& operator*() { return *static_cast<T*>(*this); }
|
||||||
// inline const T* operator*() const { return (T*)m_ptr; }
|
T const& operator*() const { return *static_cast<T const*>(*this); }
|
||||||
|
|
||||||
inline T* operator->()
|
T* operator->() { return *this; }
|
||||||
|
T const* operator->() const { return *this; }
|
||||||
|
|
||||||
|
operator T*() { return reinterpret_cast<T*>(static_cast<FlatPtr>(m_ptr)); }
|
||||||
|
operator T const *() const { return reinterpret_cast<T const*>(static_cast<FlatPtr>(m_ptr)); }
|
||||||
|
|
||||||
|
T& operator[](size_t index) { return static_cast<T*>(*this)[index]; }
|
||||||
|
T const& operator[](size_t index) const { return static_cast<T const*>(*this)[index]; }
|
||||||
|
|
||||||
|
constexpr explicit operator bool() { return m_ptr; }
|
||||||
|
template<typename U>
|
||||||
|
constexpr bool operator==(Ptr32<U> other) { return m_ptr == other.m_ptr; }
|
||||||
|
|
||||||
|
constexpr Ptr32<T> operator+(u32 other) const
|
||||||
{
|
{
|
||||||
VERIFY(m_ptr);
|
Ptr32<T> ptr {};
|
||||||
return m_ptr;
|
ptr.m_ptr = m_ptr + other;
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
constexpr Ptr32<T> operator-(u32 other) const
|
||||||
|
{
|
||||||
|
Ptr32<T> ptr {};
|
||||||
|
ptr.m_ptr = m_ptr - other;
|
||||||
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline operator T*() { return (T*)static_cast<uintptr_t>(m_ptr); }
|
|
||||||
inline operator const T*() const { return (T*)static_cast<uintptr_t>(m_ptr); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
u32 m_ptr { 0 };
|
u32 m_ptr { 0 };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue