From fe58b444de6c00089010d308a39f78890b1782b6 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 11 Dec 2025 08:34:36 +0100 Subject: feat: kernel: `Vettable.h` interface and concepts. Signed-off-by: Amlal El Mahrouss --- src/kernel/NeKit/ErrorOr.h | 23 ++++++++------- src/kernel/NeKit/Function.h | 14 ++++----- src/kernel/NeKit/InitializerList.h | 6 ++-- src/kernel/NeKit/Ref.h | 31 +++++++++++--------- src/kernel/NeKit/Variant.h | 3 +- src/kernel/NeKit/Vet.h | 42 --------------------------- src/kernel/NeKit/Vettable.h | 59 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 100 insertions(+), 78 deletions(-) delete mode 100644 src/kernel/NeKit/Vet.h create mode 100644 src/kernel/NeKit/Vettable.h (limited to 'src/kernel/NeKit') diff --git a/src/kernel/NeKit/ErrorOr.h b/src/kernel/NeKit/ErrorOr.h index 7de7434d..d9fc972d 100644 --- a/src/kernel/NeKit/ErrorOr.h +++ b/src/kernel/NeKit/ErrorOr.h @@ -25,33 +25,36 @@ class ErrorOr final { ~ErrorOr() = default; public: + using RefType = Ref; + using Type = T; + explicit ErrorOr(ErrorT err) : mRef((T*) RTL_ALLOCA(sizeof(T))), mId(err) {} explicit ErrorOr(nullPtr) {} - explicit ErrorOr(T* klass) : mRef(klass) {} - explicit ErrorOr(T klass) : mRef(klass) {} + explicit ErrorOr(Type* klass) : mRef(klass) {} + explicit ErrorOr(Type klass) : mRef(klass) {} ErrorOr& operator=(const ErrorOr&) = default; ErrorOr(const ErrorOr&) = default; - ErrorOr& operator=(const Ref& refErr) { - mRef = refErr; + ErrorOr& operator=(const RefType& ref) { + mRef = ref; return *this; } - const T& Value() { return mRef.TryLeak(); } + const Type& Value() { return mRef.TryLeak(); } - Ref& Leak() { return mRef; } + RefType& Leak() { return mRef; } ErrorT Error() { return mId; } /// @note DO NOT MAKE THIS EXPLICIT! IT WILL BREAK THE COMPILATION. - operator bool() { return mRef; } + explicit operator bool() { return mRef.Leak(); } - BOOL HasError() { return this->mId < 0; } + BOOL HasError() { return this->mId < kErrorSuccess; } private: - Ref mRef; - ErrorT mId{0}; + RefType mRef; + ErrorT mId{0}; }; using ErrorOrAny = ErrorOr; diff --git a/src/kernel/NeKit/Function.h b/src/kernel/NeKit/Function.h index 2889e055..f7e47973 100644 --- a/src/kernel/NeKit/Function.h +++ b/src/kernel/NeKit/Function.h @@ -16,25 +16,21 @@ namespace Kernel { template class Function final { public: - Function() = delete; + Function() = delete; Function(nullPtr) = delete; - ~Function() = default; + ~Function() = default; explicit Function(T (*Fn)(Args... args)) : fFn(Fn) { MUST_PASS(fFn); } Function& operator=(const Function&) = delete; Function(const Function&) = delete; - T operator()(Args&&... args) { - return fFn(args...); - } + T operator()(Args&&... args) { return fFn(args...); } - T Call(Args&&... args) { - return fFn(args...); - } + T Call(Args&&... args) { return fFn(args...); } explicit operator bool() { return fFn; } - bool operator!() { return !fFn; } + bool operator!() { return !fFn; } private: T (*fFn)(Args... args){nullptr}; diff --git a/src/kernel/NeKit/InitializerList.h b/src/kernel/NeKit/InitializerList.h index dc33d8eb..d918ba85 100644 --- a/src/kernel/NeKit/InitializerList.h +++ b/src/kernel/NeKit/InitializerList.h @@ -29,9 +29,9 @@ class InitializerList final { T* begin() { return fList; } T* end() { return fList + N; } constexpr SizeT size() const { return N; } - - T* operator->() { return fList; } - T* operator*() { return fList; } + + T* operator->() { return fList; } + T* operator*() { return fList; } private: T fList[N]; diff --git a/src/kernel/NeKit/Ref.h b/src/kernel/NeKit/Ref.h index 5f6e1940..960df31f 100644 --- a/src/kernel/NeKit/Ref.h +++ b/src/kernel/NeKit/Ref.h @@ -12,7 +12,7 @@ #include #include #include -#include +#include namespace Kernel { /// =========================================================== /// @@ -25,10 +25,12 @@ class Ref final { ~Ref() = default; public: - Ref(T* cls) : fClass(*cls) {} - Ref(T cls) : fClass(cls) {} + using Type = T; - Ref& operator=(T ref) { + Ref(Type* cls) : fClass(*cls) {} + Ref(Type cls) : fClass(cls) {} + + Ref& operator=(Type ref) { fClass = ref; return *this; } @@ -36,29 +38,32 @@ class Ref final { NE_COPY_DEFAULT(Ref) public: - T operator->() const { return fClass; } + Type operator->() const { return fClass; } - T& Leak() { return fClass; } + Type& Leak() { return fClass; } - T& TryLeak() { return fClass; } + Type& TryLeak() { return fClass; } - T operator*() { return fClass; } + Type operator*() { return fClass; } - operator bool() { return Vettable::kValue; } - bool operator!() { return !Vettable::kValue; } + operator bool() { return Vettable::kValue; } + bool operator!() { return !Vettable::kValue; } private: - T fClass; + Type fClass; }; template class NonNullRef final { public: + using RefType = Ref; + using Type = T; + NonNullRef() = delete; NonNullRef(nullPtr) = delete; - NonNullRef(T* ref) : fRef(ref) { MUST_PASS(ref); } - NonNullRef(Ref ref) : fRef(ref) { MUST_PASS(ref); } + NonNullRef(Type* ref) : fRef(ref) { MUST_PASS(ref); } + NonNullRef(RefType ref) : fRef(ref) { MUST_PASS(ref); } Ref& operator->() { MUST_PASS(fRef); diff --git a/src/kernel/NeKit/Variant.h b/src/kernel/NeKit/Variant.h index bf2b53c4..f7b2c450 100644 --- a/src/kernel/NeKit/Variant.h +++ b/src/kernel/NeKit/Variant.h @@ -15,7 +15,7 @@ namespace Kernel { class Variant final { public: - enum struct VariantKind { + enum struct VariantKind : Int { kInvalid = 0, kString = 200, kBlob, @@ -23,6 +23,7 @@ class Variant final { kJson, kTOML, kSwap, + kCount = kSwap - kString + 1, }; public: diff --git a/src/kernel/NeKit/Vet.h b/src/kernel/NeKit/Vet.h deleted file mode 100644 index 86874085..00000000 --- a/src/kernel/NeKit/Vet.h +++ /dev/null @@ -1,42 +0,0 @@ - -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - -======================================== */ - -#pragma once - -#include -#include - -#define NE_VETTABLE : public IVet - -namespace Kernel { -/// @brief Vet interface for objects. -struct IVet { - IVet() = default; - virtual ~IVet() = default; - - NE_COPY_DEFAULT(IVet) - - /// @brief Start vetting object for validity. - auto DoVet() const { return false; } -}; - -template -struct Vettable final { - static constexpr bool kValue = false; -}; - -template <> -struct Vettable final { - static constexpr bool kValue = true; -}; - -/// @brief Concept version of Vettable. -template -concept IVettable = requires(IVet vettable, Fallback fallback) { - { Vettable::kValue ? vettable.DoVet() : fallback(vettable) }; -}; -} // namespace Kernel \ No newline at end of file diff --git a/src/kernel/NeKit/Vettable.h b/src/kernel/NeKit/Vettable.h new file mode 100644 index 00000000..d2479bf0 --- /dev/null +++ b/src/kernel/NeKit/Vettable.h @@ -0,0 +1,59 @@ + +/* ======================================== + + Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. + +======================================== */ + +#ifndef __NE_KIT_VETTABLE_H__ +#define __NE_KIT_VETTABLE_H__ + +#include +#include + +#define NE_VETTABLE : public ::Kernel::IVettable + +namespace Kernel { +/// @brief Vet interface for objects. +struct IVettable { + explicit IVettable() = default; + virtual ~IVettable() = default; + + NE_COPY_DEFAULT(IVettable) +}; + +struct INotVettable { + explicit INotVettable() = default; + virtual ~INotVettable() = default; + + NE_COPY_DEFAULT(INotVettable) +}; + +template +struct Vettable final { + static constexpr bool kValue = false; +}; + +template <> +struct Vettable final { + static constexpr bool kValue = false; +}; + +template <> +struct Vettable final { + static constexpr bool kValue = true; +}; + +/// @brief Concept version of Vettable. +template +concept IsVettable = requires(OnFallback fallback) { + { Vettable::kValue ? true : fallback() }; +}; + +template +concept IsNotVettable = requires(OnFallback fallback) { + { !Vettable::kValue ? true : fallback() }; +}; +} // namespace Kernel + +#endif // !__NE_KIT_VETTABLE_H__ \ No newline at end of file -- cgit v1.2.3