NeKernel dev
Loading...
Searching...
No Matches
Ref.h
Go to the documentation of this file.
1
2/* ========================================
3
4 Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
5
6======================================== */
7
8#ifndef _NEKIT_REF_H_
9#define _NEKIT_REF_H_
10
12#include <KernelKit/HeapMgr.h>
13#include <NeKit/Config.h>
14#include <NeKit/KernelPanic.h>
15#include <NeKit/Vet.h>
16
17namespace Kernel {
21template <typename T>
22class Ref final {
23 public:
24 explicit Ref() = default;
25 ~Ref() = default;
26
27 public:
28 Ref(T* cls) : fClass(*cls) {}
29 Ref(T cls) : fClass(cls) {}
30
31 Ref& operator=(T ref) {
32 fClass = ref;
33 return *this;
34 }
35
37
38 public:
39 T operator->() const { return fClass; }
40
41 T& Leak() { return fClass; }
42
43 T& TryLeak() { return fClass; }
44
45 T operator*() { return fClass; }
46
47 operator bool() { return Vettable<T>::kValue; }
48 bool operator!() { return !Vettable<T>::kValue; }
49
50 private:
52};
53
54template <typename T>
55class NonNullRef final {
56 public:
57 NonNullRef() = delete;
59
60 NonNullRef(T* ref) : fRef(ref) { MUST_PASS(ref); }
61 NonNullRef(Ref<T> ref) : fRef(ref) { MUST_PASS(ref); }
62
65 return fRef;
66 }
67
68 NonNullRef& operator=(const NonNullRef<T>& ref) = delete;
69 NonNullRef(const NonNullRef<T>& ref) = default;
70
71 private:
73};
74
77} // namespace Kernel
78
79#endif // ifndef _NEKIT_REF_H_
#define NE_COPY_DEFAULT(KLASS)
Definition Detail.h:17
: Memory allocation support for the NeKernel.
#define MUST_PASS(EXPR)
Definition KernelPanic.h:37
Definition Ref.h:55
NonNullRef(const NonNullRef< T > &ref)=default
NonNullRef(T *ref)
Definition Ref.h:60
NonNullRef(Ref< T > ref)
Definition Ref.h:61
Ref< T > & operator->()
Definition Ref.h:63
Ref< Any > fRef
Definition Ref.h:72
NonNullRef & operator=(const NonNullRef< T > &ref)=delete
NonNullRef(nullPtr)=delete
Reference wrapper class. /// =========================================================== ///.
Definition Ref.h:22
Ref(T cls)
Definition Ref.h:29
T & Leak()
Definition Ref.h:41
Ref & operator=(T ref)
Definition Ref.h:31
Ref(T *cls)
Definition Ref.h:28
~Ref()=default
Ref()=default
USER_PROCESS fClass
Definition Ref.h:51
T & TryLeak()
Definition Ref.h:43
T operator->() const
Definition Ref.h:39
bool operator!()
Definition Ref.h:48
T operator*()
Definition Ref.h:45
UPS inline definitions.
Definition Device.h:12
NonNullRef< Any > NonNullRefAny
Definition Ref.h:76
decltype(nullptr) nullPtr
Definition Config.h:34
Ref< Any > RefAny
Definition Ref.h:75
static constexpr bool kValue
Definition Vet.h:29