diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-12-11 08:34:36 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-12-11 08:34:36 +0100 |
| commit | fe58b444de6c00089010d308a39f78890b1782b6 (patch) | |
| tree | 84849ef1576aca5410210503c58268d3a9592c14 | |
| parent | 076c8378e96a9fac9864c9d02bb63fa7dd423e4a (diff) | |
feat: kernel: `Vettable.h` interface and concepts.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
| -rw-r--r-- | src/kernel/KernelKit/Semaphore.h | 2 | ||||
| -rw-r--r-- | src/kernel/KernelKit/Timer.h | 2 | ||||
| -rw-r--r-- | src/kernel/KernelKit/TraceSrv.h | 2 | ||||
| -rw-r--r-- | src/kernel/NeKit/ErrorOr.h | 23 | ||||
| -rw-r--r-- | src/kernel/NeKit/Function.h | 14 | ||||
| -rw-r--r-- | src/kernel/NeKit/InitializerList.h | 6 | ||||
| -rw-r--r-- | src/kernel/NeKit/Ref.h | 31 | ||||
| -rw-r--r-- | src/kernel/NeKit/Variant.h | 3 | ||||
| -rw-r--r-- | src/kernel/NeKit/Vet.h | 42 | ||||
| -rw-r--r-- | src/kernel/NeKit/Vettable.h | 59 |
10 files changed, 103 insertions, 81 deletions
diff --git a/src/kernel/KernelKit/Semaphore.h b/src/kernel/KernelKit/Semaphore.h index ecfdb6a9..1cbc08c0 100644 --- a/src/kernel/KernelKit/Semaphore.h +++ b/src/kernel/KernelKit/Semaphore.h @@ -110,4 +110,4 @@ inline BOOL rtl_sem_wait(SemaphoreArr& sem, UInt64 owner, UInt64 timeout, } } // namespace Kernel -#endif // !__KERNEL_KIT_TLS_H__
\ No newline at end of file +#endif // !__KERNEL_KIT_TLS_H__
\ No newline at end of file diff --git a/src/kernel/KernelKit/Timer.h b/src/kernel/KernelKit/Timer.h index 97bc5891..c55ca3a1 100644 --- a/src/kernel/KernelKit/Timer.h +++ b/src/kernel/KernelKit/Timer.h @@ -75,4 +75,4 @@ inline UInt64 rtl_milliseconds(UInt64 time) { } } // namespace Kernel -#endif // !__KERNEL_KIT_TIMER_H__
\ No newline at end of file +#endif // !__KERNEL_KIT_TIMER_H__
\ No newline at end of file diff --git a/src/kernel/KernelKit/TraceSrv.h b/src/kernel/KernelKit/TraceSrv.h index 6a4dbc97..276e9f2b 100644 --- a/src/kernel/KernelKit/TraceSrv.h +++ b/src/kernel/KernelKit/TraceSrv.h @@ -23,4 +23,4 @@ namespace Detail { } // namespace Detail } // namespace Kernel -#endif // !__KERNELKIT_TRACESRV_H__
\ No newline at end of file +#endif // !__KERNELKIT_TRACESRV_H__
\ No newline at end of file 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<T>; + 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<T>& 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<T>& 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<T> mRef; - ErrorT mId{0}; + RefType mRef; + ErrorT mId{0}; }; using ErrorOrAny = ErrorOr<voidPtr>; 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 <typename T, typename... Args> 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 <KernelKit/HeapMgr.h> #include <NeKit/Config.h> #include <NeKit/KernelPanic.h> -#include <NeKit/Vet.h> +#include <NeKit/Vettable.h> 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<T>::kValue; } - bool operator!() { return !Vettable<T>::kValue; } + operator bool() { return Vettable<Type>::kValue; } + bool operator!() { return !Vettable<Type>::kValue; } private: - T fClass; + Type fClass; }; template <typename T> class NonNullRef final { public: + using RefType = Ref<T>; + using Type = T; + NonNullRef() = delete; NonNullRef(nullPtr) = delete; - NonNullRef(T* ref) : fRef(ref) { MUST_PASS(ref); } - NonNullRef(Ref<T> ref) : fRef(ref) { MUST_PASS(ref); } + NonNullRef(Type* ref) : fRef(ref) { MUST_PASS(ref); } + NonNullRef(RefType ref) : fRef(ref) { MUST_PASS(ref); } Ref<T>& 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 <CompilerKit/CompilerKit.h> -#include <NeKit/Config.h> - -#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 <typename T> -struct Vettable final { - static constexpr bool kValue = false; -}; - -template <> -struct Vettable<IVet> final { - static constexpr bool kValue = true; -}; - -/// @brief Concept version of Vettable. -template <typename T, typename Fallback> -concept IVettable = requires(IVet vettable, Fallback fallback) { - { Vettable<T>::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 <CompilerKit/CompilerKit.h> +#include <NeKit/Config.h> + +#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 <typename T> +struct Vettable final { + static constexpr bool kValue = false; +}; + +template <> +struct Vettable<INotVettable> final { + static constexpr bool kValue = false; +}; + +template <> +struct Vettable<IVettable> final { + static constexpr bool kValue = true; +}; + +/// @brief Concept version of Vettable. +template <typename T, typename OnFallback> +concept IsVettable = requires(OnFallback fallback) { + { Vettable<T>::kValue ? true : fallback() }; +}; + +template <typename T, typename OnFallback> +concept IsNotVettable = requires(OnFallback fallback) { + { !Vettable<T>::kValue ? true : fallback() }; +}; +} // namespace Kernel + +#endif // !__NE_KIT_VETTABLE_H__
\ No newline at end of file |
