summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorAmlal <amlal@nekernel.org>2025-05-08 13:38:22 +0200
committerAmlal <amlal@nekernel.org>2025-05-08 13:38:22 +0200
commit9a574e0a95d5e353e50c000cc723ebc6dacfc0a0 (patch)
tree326989c95a0e4c2f68ae53734a0e3a4fcb1645d0 /lib
parente79c5ed3500e155e551dcb216c6845687c7fd4a9 (diff)
feat(lib): add equiv.hpp C++ header, see commit details.
Signed-off-by: Amlal <amlal@nekernel.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/astdx/equiv.hpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/lib/astdx/equiv.hpp b/lib/astdx/equiv.hpp
new file mode 100644
index 0000000..73e63b3
--- /dev/null
+++ b/lib/astdx/equiv.hpp
@@ -0,0 +1,101 @@
+/*
+ * File: equiv.hpp
+ * Purpose: equivalence runtime c++ header.
+ * Author: Amlal El Mahrouss (amlal@nekernel.org)
+ * Copyright 2025, Amlal El Mahrouss all rights reserved.
+ */
+
+#pragma once
+
+namespace astdx
+{
+ template <typename T>
+ struct basic_hash_trait
+ {
+ /// @brief hash from T's result.
+ static typename T::result hash()
+ {
+ static T val;
+ return val.hash();
+ }
+ };
+
+ template <typename T, typename U>
+ struct is_same
+ {
+ static constexpr bool value = false;
+
+ /// @brief check if hash matches what we expect.
+ constexpr bool operator()() noexcept
+ {
+ return T::hash() == U::hash();
+ }
+ };
+
+ template <typename T>
+ struct is_same<T, T>
+ {
+ static constexpr bool value = true;
+ };
+
+ template <typename T, typename U>
+ struct is_not_same
+ {
+ static constexpr bool value = true;
+
+ constexpr bool operator()() noexcept
+ {
+ return T::hash() != U::hash();
+ }
+ };
+ template <typename T>
+ struct is_not_same<T, T>
+ {
+ static constexpr bool value = false;
+ };
+
+ template <typename T>
+ struct equiv_is_int8
+ {
+ private:
+ T left_ = 255, right_ = 255;
+
+ public:
+ using result = T;
+
+ constexpr result hash()
+ {
+ return (left_ + right_) < 1;
+ }
+ };
+
+ template <typename T>
+ struct equiv_not_int8
+ {
+ private:
+ T left_ = 255, right_ = 255;
+
+ public:
+ using result = T;
+
+ constexpr result hash()
+ {
+ return (left_ + right_) > 0;
+ }
+ };
+
+ template <typename T>
+ struct equiv_is_real
+ {
+ private:
+ T left_ = 5, right_ = 3;
+
+ public:
+ using result = T;
+
+ constexpr result hash()
+ {
+ return left_ / right_;
+ }
+ };
+} // namespace astdx \ No newline at end of file