summaryrefslogtreecommitdiff
path: root/src/common/memory/shared_ptr.hpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-03-14 14:13:54 +0100
committerHampusM <hampus@hampusmat.com>2022-03-14 14:13:54 +0100
commit5b6427dde0bb8e3b466793243bbfc185f4739ac6 (patch)
treebd8f09220d5c2354824f231c214dc6d9364717ce /src/common/memory/shared_ptr.hpp
parent5aa818e65eaa3cc288e097ed3b1a134015215500 (diff)
refactor: implement & use shared ptr
Diffstat (limited to 'src/common/memory/shared_ptr.hpp')
-rw-r--r--src/common/memory/shared_ptr.hpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/common/memory/shared_ptr.hpp b/src/common/memory/shared_ptr.hpp
new file mode 100644
index 0000000..7e8a910
--- /dev/null
+++ b/src/common/memory/shared_ptr.hpp
@@ -0,0 +1,44 @@
+#pragma once
+
+#include <stddef.h>
+
+namespace common
+{
+template <class Target>
+class SharedPtr
+{
+public:
+ SharedPtr() noexcept;
+ SharedPtr(nullptr_t) noexcept; // NOLINT(google-explicit-constructor)
+
+ explicit SharedPtr(Target *target) noexcept;
+
+ SharedPtr(const SharedPtr &shared_ptr) noexcept;
+
+ SharedPtr(SharedPtr &&shared_ptr) noexcept;
+
+ ~SharedPtr() noexcept;
+
+ [[nodiscard]] unsigned int reference_cnt() const noexcept;
+
+ [[nodiscard]] bool is_disposable() const noexcept;
+
+ SharedPtr &operator=(const SharedPtr &rhs) noexcept;
+
+ SharedPtr &operator=(SharedPtr &&rhs) noexcept;
+
+ Target &operator*() const noexcept;
+ Target *operator->() const noexcept;
+
+private:
+ Target *_target = nullptr;
+
+ unsigned int *_reference_cnt;
+};
+
+template <typename Target, typename... Args>
+SharedPtr<Target> make_shared(Args &&...args) noexcept;
+
+} // namespace common
+
+#include "shared_ptr.tpp"