Make reset() safer if the new throws.
[dcpomatic.git] / src / wx / wx_ptr.h
index 7eff673a4e0d16e42519dec8b15b7d30f0f3e92c..fcca8b18bafb602744c9befb8ab5971bace1a7a7 100644 (file)
@@ -62,16 +62,40 @@ public:
                }
        }
 
-       T* operator->() {
+       wx_ptr& operator=(T* ptr)
+       {
+               if (_wx) {
+                       _wx->Destroy();
+               }
+               _wx = ptr;
+               return *this;
+       }
+
+       T* operator->()
+       {
                DCPOMATIC_ASSERT(_wx);
                return _wx;
        }
 
+       operator bool() const
+       {
+               return _wx != nullptr;
+       }
+
+       void reset()
+       {
+               if (_wx) {
+                       _wx->Destroy();
+                       _wx = nullptr;
+               }
+       }
+
        template <typename... Args>
-       void reset(Args... args)
+       void reset(Args&&... args)
        {
                if (_wx) {
                        _wx->Destroy();
+                       _wx = nullptr;
                }
                _wx = new T(std::forward<Args>(args)...);
        }