summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar bunnei2021-05-01 12:36:21 -0700
committerGravatar bunnei2021-05-05 16:40:53 -0700
commit25538db150a90f01356c238e65548ccf3e797cd8 (patch)
treee869a008fa8851dea774aef7d5e5fb7d51ac19b0 /src
parentfixup! hle: kernel: Add initial impl. of KAutoObject. (diff)
downloadyuzu-25538db150a90f01356c238e65548ccf3e797cd8.tar.gz
yuzu-25538db150a90f01356c238e65548ccf3e797cd8.tar.xz
yuzu-25538db150a90f01356c238e65548ccf3e797cd8.zip
fixup! hle: kernel: Add initial impl. of KAutoObject.
Diffstat (limited to 'src')
-rw-r--r--src/core/hle/kernel/k_auto_object.h92
1 files changed, 46 insertions, 46 deletions
diff --git a/src/core/hle/kernel/k_auto_object.h b/src/core/hle/kernel/k_auto_object.h
index 452325f73..765e46670 100644
--- a/src/core/hle/kernel/k_auto_object.h
+++ b/src/core/hle/kernel/k_auto_object.h
@@ -5,6 +5,7 @@
5#pragma once 5#pragma once
6 6
7#include <atomic> 7#include <atomic>
8#include <string>
8 9
9#include "common/assert.h" 10#include "common/assert.h"
10#include "common/common_funcs.h" 11#include "common/common_funcs.h"
@@ -45,19 +46,15 @@ public:
45 return GetStaticTypeName(); \ 46 return GetStaticTypeName(); \
46 } \ 47 } \
47 \ 48 \
48private: 49private: \
50 constexpr bool operator!=(const TypeObj& rhs)
49 51
50class KAutoObject { 52class KAutoObject {
51protected: 53protected:
52 class TypeObj { 54 class TypeObj {
53 private:
54 const char* m_name;
55 ClassTokenType m_class_token;
56
57 public: 55 public:
58 constexpr explicit TypeObj(const char* n, ClassTokenType tok) 56 constexpr explicit TypeObj(const char* n, ClassTokenType tok)
59 : m_name(n), m_class_token(tok) { // ... 57 : m_name(n), m_class_token(tok) {}
60 }
61 58
62 constexpr const char* GetName() const { 59 constexpr const char* GetName() const {
63 return m_name; 60 return m_name;
@@ -66,35 +63,31 @@ protected:
66 return m_class_token; 63 return m_class_token;
67 } 64 }
68 65
69 constexpr bool operator==(const TypeObj& rhs) { 66 constexpr bool operator==(const TypeObj& rhs) const {
70 return this->GetClassToken() == rhs.GetClassToken(); 67 return this->GetClassToken() == rhs.GetClassToken();
71 } 68 }
72 69
73 constexpr bool operator!=(const TypeObj& rhs) { 70 constexpr bool operator!=(const TypeObj& rhs) const {
74 return this->GetClassToken() != rhs.GetClassToken(); 71 return this->GetClassToken() != rhs.GetClassToken();
75 } 72 }
76 73
77 constexpr bool IsDerivedFrom(const TypeObj& rhs) { 74 constexpr bool IsDerivedFrom(const TypeObj& rhs) const {
78 return (this->GetClassToken() | rhs.GetClassToken()) == this->GetClassToken(); 75 return (this->GetClassToken() | rhs.GetClassToken()) == this->GetClassToken();
79 } 76 }
77
78 private:
79 const char* m_name;
80 ClassTokenType m_class_token;
80 }; 81 };
81 82
82private: 83private:
83 KERNEL_AUTOOBJECT_TRAITS(KAutoObject, KAutoObject); 84 KERNEL_AUTOOBJECT_TRAITS(KAutoObject, KAutoObject);
84 85
85private:
86 std::atomic<u32> m_ref_count{};
87
88protected:
89 KernelCore& kernel;
90 std::string name;
91
92public:
93 static KAutoObject* Create(KAutoObject* ptr);
94
95public: 86public:
96 explicit KAutoObject(KernelCore& kernel_) : kernel(kernel_) {} 87 explicit KAutoObject(KernelCore& kernel_) : kernel(kernel_) {}
97 virtual ~KAutoObject() {} 88 virtual ~KAutoObject() = default;
89
90 static KAutoObject* Create(KAutoObject* ptr);
98 91
99 // Destroy is responsible for destroying the auto object's resources when ref_count hits zero. 92 // Destroy is responsible for destroying the auto object's resources when ref_count hits zero.
100 virtual void Destroy() { 93 virtual void Destroy() {
@@ -122,8 +115,8 @@ public:
122 115
123 template <typename Derived> 116 template <typename Derived>
124 Derived DynamicCast() { 117 Derived DynamicCast() {
125 static_assert(std::is_pointer<Derived>::value); 118 static_assert(std::is_pointer_v<Derived>);
126 using DerivedType = typename std::remove_pointer<Derived>::type; 119 using DerivedType = std::remove_pointer_t<Derived>;
127 120
128 if (this->IsDerivedFrom(DerivedType::GetStaticTypeObj())) { 121 if (this->IsDerivedFrom(DerivedType::GetStaticTypeObj())) {
129 return static_cast<Derived>(this); 122 return static_cast<Derived>(this);
@@ -134,8 +127,8 @@ public:
134 127
135 template <typename Derived> 128 template <typename Derived>
136 const Derived DynamicCast() const { 129 const Derived DynamicCast() const {
137 static_assert(std::is_pointer<Derived>::value); 130 static_assert(std::is_pointer_v<Derived>);
138 using DerivedType = typename std::remove_pointer<Derived>::type; 131 using DerivedType = std::remove_pointer_t<Derived>;
139 132
140 if (this->IsDerivedFrom(DerivedType::GetStaticTypeObj())) { 133 if (this->IsDerivedFrom(DerivedType::GetStaticTypeObj())) {
141 return static_cast<Derived>(this); 134 return static_cast<Derived>(this);
@@ -171,20 +164,18 @@ public:
171 this->Destroy(); 164 this->Destroy();
172 } 165 }
173 } 166 }
174};
175
176class KAutoObjectWithListContainer;
177 167
178class KAutoObjectWithList : public KAutoObject { 168protected:
179private: 169 KernelCore& kernel;
180 friend class KAutoObjectWithListContainer; 170 std::string name;
181 171
182private: 172private:
183 Common::IntrusiveRedBlackTreeNode list_node; 173 std::atomic<u32> m_ref_count{};
174};
184 175
185protected: 176class KAutoObjectWithListContainer;
186 KernelCore& kernel;
187 177
178class KAutoObjectWithList : public KAutoObject {
188public: 179public:
189 explicit KAutoObjectWithList(KernelCore& kernel_) : KAutoObject(kernel_), kernel(kernel_) {} 180 explicit KAutoObjectWithList(KernelCore& kernel_) : KAutoObject(kernel_), kernel(kernel_) {}
190 181
@@ -209,23 +200,20 @@ public:
209 virtual const std::string& GetName() const { 200 virtual const std::string& GetName() const {
210 return name; 201 return name;
211 } 202 }
212};
213
214template <typename T>
215class KScopedAutoObject {
216 YUZU_NON_COPYABLE(KScopedAutoObject);
217 203
218private: 204private:
219 template <typename U> 205 friend class KAutoObjectWithListContainer;
220 friend class KScopedAutoObject;
221 206
222private: 207private:
223 T* m_obj{}; 208 Common::IntrusiveRedBlackTreeNode list_node;
224 209
225private: 210protected:
226 constexpr void Swap(KScopedAutoObject& rhs) { 211 KernelCore& kernel;
227 std::swap(m_obj, rhs.m_obj); 212};
228 } 213
214template <typename T>
215class KScopedAutoObject {
216 YUZU_NON_COPYABLE(KScopedAutoObject);
229 217
230public: 218public:
231 constexpr KScopedAutoObject() = default; 219 constexpr KScopedAutoObject() = default;
@@ -301,6 +289,18 @@ public:
301 constexpr bool IsNotNull() const { 289 constexpr bool IsNotNull() const {
302 return m_obj != nullptr; 290 return m_obj != nullptr;
303 } 291 }
292
293private:
294 template <typename U>
295 friend class KScopedAutoObject;
296
297private:
298 T* m_obj{};
299
300private:
301 constexpr void Swap(KScopedAutoObject& rhs) noexcept {
302 std::swap(m_obj, rhs.m_obj);
303 }
304}; 304};
305 305
306} // namespace Kernel 306} // namespace Kernel