summaryrefslogtreecommitdiff
path: root/src/weak_ptr.hh
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2021-11-17 22:34:57 +0100
committerJoel Klinghed <the_jk@spawned.biz>2021-11-17 22:34:57 +0100
commit6232d13f5321b87ddf12a1aa36b4545da45f173d (patch)
tree23f3316470a14136debd9d02f9e920ca2b06f4cc /src/weak_ptr.hh
Travel3: Simple image and video display site
Reads the images and videos from filesystem and builds a site in memroy.
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