From: Amelia Coutard Date: Fri, 6 May 2022 18:48:46 +0000 (+0200) Subject: Added missing operators to phys_ptr. X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=1fed8abff92c72923c5839b54d79f145828519dc;p=voyage-au-centre-des-fichiers.git Added missing operators to phys_ptr. --- diff --git a/src/utils.hpp b/src/utils.hpp index 486d7f6..773056f 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -2,13 +2,14 @@ #include #include +#include namespace os { template class phys_ptr { public: - constexpr phys_ptr(uintptr_t phys_addr): phys_addr(phys_addr) {} + constexpr explicit phys_ptr(uintptr_t phys_addr): phys_addr(phys_addr) {} constexpr phys_ptr(nullptr_t): phys_addr(0) {} constexpr T& operator[](size_t i) const { @@ -20,12 +21,44 @@ namespace os { constexpr T* operator->() const { return get_virt_addr(); } - constexpr bool operator< (phys_ptr other) const { return phys_addr < other.phys_addr; } - constexpr bool operator<=(phys_ptr other) const { return phys_addr <= other.phys_addr; } - constexpr bool operator==(phys_ptr other) const { return phys_addr == other.phys_addr; } - constexpr bool operator!=(phys_ptr other) const { return phys_addr != other.phys_addr; } - constexpr bool operator> (phys_ptr other) const { return phys_addr > other.phys_addr; } - constexpr bool operator>=(phys_ptr other) const { return phys_addr >= other.phys_addr; } + + constexpr phys_ptr& operator++() { + phys_addr += sizeof(T); + } + constexpr phys_ptr& operator++(int) { + const auto old = *this; + operator++(); + return old; + } + constexpr phys_ptr& operator--() { + phys_addr -= sizeof(T); + } + constexpr phys_ptr& operator--(int) { + const auto old = *this; + operator--(); + return old; + } + constexpr phys_ptr& operator+=(ptrdiff_t offset) { + phys_addr += offset * sizeof(T); + return *this; + } + constexpr phys_ptr& operator-=(ptrdiff_t offset) { + phys_addr -= offset * sizeof(T); + return *this; + } + + friend constexpr auto operator<=>(phys_ptr a, phys_ptr b) { return a.phys_addr <=> b.phys_addr; } + friend constexpr auto operator== (phys_ptr a, phys_ptr b) { return a.phys_addr == b.phys_addr; } + + friend constexpr phys_ptr operator+(phys_ptr ptr, ptrdiff_t offset) { + return phys_ptr{ptr.phys_addr + offset * sizeof(T)}; + } + friend constexpr phys_ptr operator+(ptrdiff_t offset, phys_ptr ptr) { + return ptr + offset; + } + friend constexpr phys_ptr operator-(phys_ptr ptr, ptrdiff_t offset) { + return phys_ptr{ptr.phys_addr - offset * sizeof(T)}; + } constexpr uintptr_t get_phys_addr() const { return phys_addr;