From 2d56cc149fa8c07a92c9ee71950f3ec415590267 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 14 Dec 2025 02:38:37 +0100 Subject: chore: improve Vettable pattern, add new concepts in Config.h Signed-off-by: Amlal El Mahrouss --- src/kernel/NeKit/Config.h | 40 ++++++++++++++++++++++++++++++++-------- src/kernel/NeKit/Vettable.h | 25 ++++++++++++++++--------- 2 files changed, 48 insertions(+), 17 deletions(-) (limited to 'src') 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(arg); } +template +concept IsSerializable = requires() { + { Type::kValue }; +}; + +template +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 - Y As(T type) { - if (type.template IsSerializable()) { - return type.template Error(); - } + template + RetType As(Concept& type) { + return type.template As(); + } - return type.template As(); + template + 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 +struct FalseResult final {}; + +template +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 #include -#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 -struct Vettable final {}; +struct Vettable final { + static constexpr bool kValue = false; +}; template <> struct Vettable final { - static constexpr bool kValue = false; + static constexpr bool kValue = false; }; template <> struct Vettable final { - static constexpr bool kValue = true; + static constexpr bool kValue = true; }; /// @brief Concept version of Vettable. -template +template concept IsVettable = requires(OnFallback fallback) { - { Vettable::kValue ? true : fallback() }; + { Vettable::kValue ? TrueResult::kValue : fallback() }; }; template concept IsNotVettable = requires(OnFallback fallback) { - { !Vettable::kValue ? true : fallback() }; + { !Vettable::kValue ? TrueResult::kValue : fallback() }; }; } // namespace Kernel -- cgit v1.2.3