blob: 7ccf0405a956053d82e50c99ade0cb8ea83844db (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org)
// Licensed under the Apache License, Version 2.0 (See accompanying
// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0)
// Official repository: https://github.com/nekernel-org/nectar
#ifndef NECTAR_COMPILERKIT_REF_H
#define NECTAR_COMPILERKIT_REF_H
#include <CompilerKit/Detail/Config.h>
namespace CompilerKit {
/// @author Amlal El Mahrouss
/// @brief Reference holder class, refers to a pointer of data in static memory.
template <typename T>
class StrongRef {
public:
StrongRef() = default;
virtual ~StrongRef() {
if (mStrong) {
MUST_PASS(mClass);
if (mClass) delete mClass;
mClass = nullptr;
}
}
NECTAR_COPY_DEFAULT(StrongRef)
using Type = T;
protected:
StrongRef(Type* cls, const bool strong) : mClass(cls), mStrong(strong) {}
public:
StrongRef(Type* cls) : mClass(cls), mStrong(true) {}
StrongRef& operator=(Type* ref) {
mClass = ref;
return *this;
}
public:
Type* operator->() const { return mClass; }
Type* Leak() { return mClass; }
Type* operator*() { return mClass; }
bool IsStrong() const { return mStrong; }
explicit operator bool() { return mClass != nullptr; }
private:
Type* mClass{nullptr};
bool mStrong{false};
};
template <typename T>
class WeakRef final : public StrongRef<T> {
public:
WeakRef() = delete;
~WeakRef() = default;
NECTAR_COPY_DEFAULT(WeakRef)
public:
using Type = T;
WeakRef(Type* cls) : StrongRef<Type>(cls, false) {}
};
/// @author Amlal El Mahrouss
/// @brief Non null reference holder class, refers to a pointer of data in static memory.
template <typename Type>
class NonNullRef final {
public:
explicit NonNullRef() = delete;
NonNullRef(Type* ref) : mRef(ref, true) {}
StrongRef<Type>& operator->() {
MUST_PASS(mRef);
return mRef;
}
NonNullRef& operator=(const NonNullRef<Type>& ref) = delete;
NonNullRef(const NonNullRef<Type>& ref) = default;
private:
StrongRef<Type> mRef{nullptr};
};
using StrongAny = StrongRef<VoidPtr>;
using WeakAny = WeakRef<VoidPtr>;
} // namespace CompilerKit
#endif // NECTAR_COMPILERKIT_REF_H
|