summaryrefslogtreecommitdiffhomepage
path: root/src/kernel/NeKit
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-12-14 02:38:37 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2025-12-14 02:38:37 +0100
commit2d56cc149fa8c07a92c9ee71950f3ec415590267 (patch)
tree218bf47aa7a23db7b6c3f69cafccbe68fd4a935a /src/kernel/NeKit
parent74b02a538770b9646cd861c82430ad5fadea93eb (diff)
chore: improve Vettable pattern, add new concepts in Config.h
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'src/kernel/NeKit')
-rw-r--r--src/kernel/NeKit/Config.h40
-rw-r--r--src/kernel/NeKit/Vettable.h25
2 files changed, 48 insertions, 17 deletions
diff --git a/src/kernel/NeKit/Config.h b/src/kernel/NeKit/Config.h
index bb21ba5c..40d2ede5 100644
--- a/src/kernel/NeKit/Config.h
+++ b/src/kernel/NeKit/Config.h
@@ -15,6 +15,8 @@
#error !!! NeKernel compiles with C++20 as of December 4, 2025 !!!
#endif
+#define NE_ICODEC final : public ::Kernel::ICodec
+
/// @brief The **Kernel** namespace.
namespace Kernel {
using voidPtr = void*;
@@ -105,6 +107,16 @@ inline Args&& move(Args&& arg) {
return static_cast<Args&&>(arg);
}
+template <class Type>
+concept IsSerializable = requires() {
+ { Type::kValue };
+};
+
+template <class Type>
+concept IsNotSerializable = requires() {
+ { !Type::kValue };
+};
+
/// @brief Encoding interface, used as a proxy to convert T to Char*
/// Used to cast A to B or B to A.
class ICodec {
@@ -138,17 +150,18 @@ class ICodec {
/// @brief Convert T to a Y type.
/// @note The class must be serializable, otherwise this will fail.
- /// @tparam T the class type of type.
- /// @tparam Y the result class.
+ /// @tparam Concept the class type of type.
+ /// @tparam RetType the result class.
/// @param type the class to cast.
/// @return the class as Y.
- template <typename T, typename Y>
- Y As(T type) {
- if (type.template IsSerializable()) {
- return type.template Error();
- }
+ template <IsSerializable Concept, typename RetType>
+ RetType As(Concept& type) {
+ return type.template As<RetType>();
+ }
- return type.template As<Y>();
+ template <IsNotSerializable Concept>
+ Void As(Concept& type) {
+ static_assert(type, "Concept is not serializable.");
}
};
@@ -171,4 +184,15 @@ class ISchedulable {
/// @brief Is this object offloading to another CPU?
virtual Bool HasMP() { return NO; }
};
+
+template <class Type>
+struct FalseResult final {};
+
+template <class Type>
+struct TrueResult final {
+ using ResultType = Type;
+ using ResultTypeRef = ResultType&;
+
+ static constexpr bool kValue = true;
+};
} // namespace Kernel
diff --git a/src/kernel/NeKit/Vettable.h b/src/kernel/NeKit/Vettable.h
index 2b8221d5..5520af01 100644
--- a/src/kernel/NeKit/Vettable.h
+++ b/src/kernel/NeKit/Vettable.h
@@ -11,8 +11,14 @@
#include <CompilerKit/CompilerKit.h>
#include <NeKit/Config.h>
-#define NE_VETTABLE final : public ::Kernel::IVettable
-#define NE_NOT_VETTABLE final : public ::Kernel::INotVettable
+#define NE_VETTABLE \
+ final: \
+ public \
+ ::Kernel::IVettable
+#define NE_NOT_VETTABLE \
+ final: \
+ public \
+ ::Kernel::INotVettable
namespace Kernel {
/// @brief Vet interface for objects.
@@ -30,29 +36,30 @@ struct INotVettable {
NE_COPY_DEFAULT(INotVettable)
};
-// false_type equivalent tag.
template <class Type>
-struct Vettable final {};
+struct Vettable final {
+ static constexpr bool kValue = false;
+};
template <>
struct Vettable<INotVettable> final {
- static constexpr bool kValue = false;
+ static constexpr bool kValue = false;
};
template <>
struct Vettable<IVettable> final {
- static constexpr bool kValue = true;
+ static constexpr bool kValue = true;
};
/// @brief Concept version of Vettable.
-template <typename T, typename OnFallback>
+template <typename Type, typename OnFallback>
concept IsVettable = requires(OnFallback fallback) {
- { Vettable<T>::kValue ? true : fallback() };
+ { Vettable<Type>::kValue ? TrueResult<Type>::kValue : fallback() };
};
template <class Type, typename OnFallback>
concept IsNotVettable = requires(OnFallback fallback) {
- { !Vettable<Type>::kValue ? true : fallback() };
+ { !Vettable<Type>::kValue ? TrueResult<Type>::kValue : fallback() };
};
} // namespace Kernel