Add reset() to wx_ptr and allow it to be null.
authorCarl Hetherington <cth@carlh.net>
Sat, 14 Jan 2023 21:50:09 +0000 (22:50 +0100)
committerCarl Hetherington <cth@carlh.net>
Sat, 14 Jan 2023 21:50:09 +0000 (22:50 +0100)
src/wx/wx_ptr.h

index de18846cc77f4ba7b69fa41be30a30352ee34fbe..7eff673a4e0d16e42519dec8b15b7d30f0f3e92c 100644 (file)
@@ -23,6 +23,7 @@
 #define DCPOMATIC_WX_PTR_H
 
 
+#include "lib/dcpomatic_assert.h"
 #include <utility>
 
 
@@ -30,6 +31,8 @@ template <class T>
 class wx_ptr
 {
 public:
+       wx_ptr() {}
+
        explicit wx_ptr(T* wx)
                : _wx(wx)
        {}
@@ -60,11 +63,21 @@ public:
        }
 
        T* operator->() {
+               DCPOMATIC_ASSERT(_wx);
                return _wx;
        }
 
+       template <typename... Args>
+       void reset(Args... args)
+       {
+               if (_wx) {
+                       _wx->Destroy();
+               }
+               _wx = new T(std::forward<Args>(args)...);
+       }
+
 private:
-       T* _wx;
+       T* _wx = nullptr;
 };