From: Amelia Coutard <eliottulio.coutard@gmail.com>
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 <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;