NeKernel dev
Loading...
Searching...
No Matches
ErrorOr.h
Go to the documentation of this file.
1/*
2 * ========================================================
3 *
4 * NeKernel
5 * Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
6 *
7 * ========================================================
8 */
9
10#pragma once
11
12#include <NeKit/Config.h>
13#include <NeKit/Ref.h>
14
15namespace Kernel {
16using ErrorT = Int32;
17
21template <typename T>
22class ErrorOr final {
23 public:
24 explicit ErrorOr() = default;
25 ~ErrorOr() = default;
26
27 public:
28 explicit ErrorOr(ErrorT err) : mRef((T*) RTL_ALLOCA(sizeof(T))), mId(err) {}
29 explicit ErrorOr(nullPtr) {}
30 explicit ErrorOr(T* klass) : mRef(klass) {}
31 explicit ErrorOr(T klass) : mRef(klass) {}
32
33 ErrorOr& operator=(const ErrorOr&) = default;
34 ErrorOr(const ErrorOr&) = default;
35
36 ErrorOr& operator=(const Ref<T>& refErr) {
37 mRef = refErr;
38 return *this;
39 }
40
41 const T& Value() { return mRef.TryLeak(); }
42
43 Ref<T>& Leak() { return mRef; }
44
45 ErrorT Error() { return mId; }
46
48 operator bool() { return mRef; }
49
50 BOOL HasError() { return this->mId < 0; }
51
52 private:
55};
56
58
59} // namespace Kernel
#define RTL_ALLOCA(sz)
#define BOOL
ErrorOr class for error handling.
Definition ErrorOr.h:22
ErrorOr & operator=(const ErrorOr &)=default
~ErrorOr()=default
const T & Value()
Definition ErrorOr.h:41
ErrorOr(T *klass)
Definition ErrorOr.h:30
ErrorOr(T klass)
Definition ErrorOr.h:31
ErrorOr(nullPtr)
Definition ErrorOr.h:29
ErrorOr(const ErrorOr &)=default
ErrorT mId
Definition ErrorOr.h:54
ErrorOr(ErrorT err)
Definition ErrorOr.h:28
BOOL HasError()
Definition ErrorOr.h:50
ErrorT Error()
Definition ErrorOr.h:45
Ref< voidPtr > mRef
Definition ErrorOr.h:53
ErrorOr & operator=(const Ref< T > &refErr)
Definition ErrorOr.h:36
Ref< T > & Leak()
Definition ErrorOr.h:43
ErrorOr()=default
Reference wrapper class. /// =========================================================== ///.
Definition Ref.h:22
UPS inline definitions.
Definition Device.h:12
ErrorOr< voidPtr > ErrorOrAny
Definition ErrorOr.h:57
decltype(nullptr) nullPtr
Definition Config.h:34
__INT32_TYPE__ Int32
Definition Config.h:38
Int32 ErrorT
Definition ErrorOr.h:16