summaryrefslogtreecommitdiffstats
path: root/source/core/slang-smart-pointer.h
diff options
context:
space:
mode:
Diffstat (limited to 'source/core/slang-smart-pointer.h')
-rw-r--r--source/core/slang-smart-pointer.h21
1 files changed, 20 insertions, 1 deletions
diff --git a/source/core/slang-smart-pointer.h b/source/core/slang-smart-pointer.h
index ebc6f20b0..1bbcffe47 100644
--- a/source/core/slang-smart-pointer.h
+++ b/source/core/slang-smart-pointer.h
@@ -249,8 +249,27 @@ namespace Slang
private:
T* pointer;
-
};
+
+ // Helper type for implementing weak pointers. The object being pointed at weakly creates a WeakSink object
+ // that other objects can reference and share. When the object is destroyed it detaches the sink
+ // doing so will make other users call to 'get' return null. Thus any user of the WeakSink, must check if the weakly pointed to
+ // things pointer is nullptr before using.
+ template <typename T>
+ class WeakSink : public RefObject
+ {
+ public:
+ WeakSink(T* ptr):
+ m_ptr(ptr)
+ {
+ }
+
+ SLANG_FORCE_INLINE T* get() const { return m_ptr; }
+ SLANG_FORCE_INLINE void detach() { m_ptr = nullptr; }
+
+ private:
+ T* m_ptr;
+ };
}
#endif