summaryrefslogtreecommitdiff
path: root/src/weak_ptr.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/weak_ptr.hh')
-rw-r--r--src/weak_ptr.hh42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/weak_ptr.hh b/src/weak_ptr.hh
new file mode 100644
index 0000000..5859d0c
--- /dev/null
+++ b/src/weak_ptr.hh
@@ -0,0 +1,42 @@
+#ifndef WEAK_PTR_HH
+#define WEAK_PTR_HH
+
+#include <memory>
+
+template <typename T>
+class WeakPtr {
+public:
+ explicit WeakPtr(T* ptr)
+ : ptr_(ptr) {}
+
+ void unlink() {
+ ptr_ = nullptr;
+ }
+
+ T* get() {
+ return ptr_;
+ }
+
+private:
+ T* ptr_;
+};
+
+template<typename T>
+class WeakPtrOwner {
+public:
+ explicit WeakPtrOwner(T* ptr)
+ : ptr_(std::make_shared<WeakPtr<T>>(ptr)) {}
+
+ ~WeakPtrOwner() {
+ ptr_->unlink();
+ }
+
+ std::shared_ptr<WeakPtr<T>> get() {
+ return ptr_;
+ }
+
+private:
+ std::shared_ptr<WeakPtr<T>> ptr_;
+};
+
+#endif // WEAK_PTR_HH