aboutsummaryrefslogtreecommitdiff
path: root/source/Common/RcPtr.hpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-06-03 23:30:01 -0700
committerrtk0c <[email protected]>2022-06-03 23:30:01 -0700
commit791b3f354b378769bffe623b05f1305c91b77101 (patch)
tree5409b311e6232eb4a6d3f8259b780d76b8ee1c59 /source/Common/RcPtr.hpp
parent60ccc62f4934e44ad5b905fdbcf458302b8d8a09 (diff)
Changeset: 64 [WIP] Rename directoriesmaster-switch-to-build2
Diffstat (limited to 'source/Common/RcPtr.hpp')
-rw-r--r--source/Common/RcPtr.hpp120
1 files changed, 0 insertions, 120 deletions
diff --git a/source/Common/RcPtr.hpp b/source/Common/RcPtr.hpp
deleted file mode 100644
index 130b2b2..0000000
--- a/source/Common/RcPtr.hpp
+++ /dev/null
@@ -1,120 +0,0 @@
-#pragma once
-
-#include "Macros.hpp"
-#include "TypeTraits.hpp"
-
-#include <cstddef>
-#include <cstdint>
-#include <optional>
-#include <type_traits>
-
-class RefCounted {
-public:
- // DO NOT MODIFY this field, unless explicitly documented the use
- uint32_t refCount = 0;
- uint32_t weakCount = 0; // TODO implement
-};
-
-template <class T, class TDeleter = DefaultDeleter<T>>
-class RcPtr : TDeleter {
-private:
- static_assert(std::is_base_of_v<RefCounted, T>);
- T* mPtr;
-
-public:
- RcPtr()
- : mPtr{ nullptr } {
- }
-
- explicit RcPtr(T* ptr)
- : mPtr{ ptr } {
- if (ptr) {
- ++ptr->RefCounted::refCount;
- }
- }
-
- ~RcPtr() {
- CleanUp();
- }
-
- void Attach(T* ptr) {
- CleanUp();
- mPtr = ptr;
- if (ptr) {
- ++ptr->RefCounted::refCount;
- }
- }
-
- void Detatch() {
- CleanUp();
- mPtr = nullptr;
- }
-
- RcPtr(const RcPtr& that)
- : mPtr{ that.mPtr } {
- if (mPtr) {
- ++mPtr->RefCounted::refCount;
- }
- }
-
- RcPtr& operator=(const RcPtr& that) {
- CleanUp();
- mPtr = that.mPtr;
- if (mPtr) {
- ++mPtr->RefCounted::refCount;
- }
- return *this;
- }
-
- RcPtr(RcPtr&& that)
- : mPtr{ that.mPtr } {
- that.mPtr = nullptr;
- }
-
- RcPtr& operator=(RcPtr&& that) {
- CleanUp();
- mPtr = that.mPtr;
- that.mPtr = nullptr;
- return *this;
- }
-
- template <class TBase>
- requires std::is_base_of_v<TBase, T>
- operator RcPtr<TBase>() const {
- return RcPtr<TBase>(mPtr);
- }
-
- bool operator==(std::nullptr_t ptr) const {
- return mPtr == nullptr;
- }
-
- bool operator==(const T* ptr) const {
- return mPtr == ptr;
- }
-
- bool operator==(T* ptr) const {
- return mPtr == ptr;
- }
-
- template <class TThat>
- bool operator==(const RcPtr<TThat>& ptr) const {
- return mPtr == ptr.Get();
- }
-
- T* Get() const {
- return mPtr;
- }
-
- T& operator*() const { return *mPtr; }
- T* operator->() const { return mPtr; }
-
-private:
- void CleanUp() {
- if (mPtr) {
- --mPtr->RefCounted::refCount;
- if (mPtr->RefCounted::refCount == 0) {
- TDeleter::operator()(mPtr);
- }
- }
- }
-};