]> git.ameliathe1st.gay Git - voyage-au-centre-des-fichiers.git/commitdiff
Added missing operators to phys_ptr.
authorAmelia Coutard <eliottulio.coutard@gmail.com>
Fri, 6 May 2022 18:48:46 +0000 (20:48 +0200)
committerAmelia Coutard <eliottulio.coutard@gmail.com>
Fri, 6 May 2022 22:28:36 +0000 (00:28 +0200)
src/utils.hpp

index 486d7f62a7b9d7e05abfd843c20b890ad4b7d26d..773056ffc04bc36e0f44029f601078e19be0efb4 100644 (file)
@@ -2,13 +2,14 @@
 
 #include <stddef.h>
 #include <stdint.h>
+#include <compare>
 
 namespace os {
 
        template <typename T>
        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<T> other) const { return phys_addr < other.phys_addr; }
-               constexpr bool operator<=(phys_ptr<T> other) const { return phys_addr <= other.phys_addr; }
-               constexpr bool operator==(phys_ptr<T> other) const { return phys_addr == other.phys_addr; }
-               constexpr bool operator!=(phys_ptr<T> other) const { return phys_addr != other.phys_addr; }
-               constexpr bool operator> (phys_ptr<T> other) const { return phys_addr > other.phys_addr; }
-               constexpr bool operator>=(phys_ptr<T> other) const { return phys_addr >= other.phys_addr; }
+
+               constexpr phys_ptr<T>& operator++() {
+                       phys_addr += sizeof(T);
+               }
+               constexpr phys_ptr<T>& operator++(int) {
+                       const auto old = *this;
+                       operator++();
+                       return old;
+               }
+               constexpr phys_ptr<T>& operator--() {
+                       phys_addr -= sizeof(T);
+               }
+               constexpr phys_ptr<T>& operator--(int) {
+                       const auto old = *this;
+                       operator--();
+                       return old;
+               }
+               constexpr phys_ptr<T>& operator+=(ptrdiff_t offset) {
+                       phys_addr += offset * sizeof(T);
+                       return *this;
+               }
+               constexpr phys_ptr<T>& operator-=(ptrdiff_t offset) {
+                       phys_addr -= offset * sizeof(T);
+                       return *this;
+               }
+
+               friend constexpr auto operator<=>(phys_ptr<T> a, phys_ptr<T> b) { return    a.phys_addr <=> b.phys_addr;    }
+               friend constexpr auto operator== (phys_ptr<T> a, phys_ptr<T> b) { return    a.phys_addr ==  b.phys_addr;    }
+
+               friend constexpr phys_ptr<T> operator+(phys_ptr<T> ptr, ptrdiff_t offset) {
+                       return phys_ptr<T>{ptr.phys_addr + offset * sizeof(T)};
+               }
+               friend constexpr phys_ptr<T> operator+(ptrdiff_t offset, phys_ptr<T> ptr) {
+                       return ptr + offset;
+               }
+               friend constexpr phys_ptr<T> operator-(phys_ptr<T> ptr, ptrdiff_t offset) {
+                       return phys_ptr<T>{ptr.phys_addr - offset * sizeof(T)};
+               }
 
                constexpr uintptr_t get_phys_addr() const {
                        return phys_addr;