Add wx_ptr and use it instead of ScopeGuard in a lot of places.
[dcpomatic.git] / src / wx / wx_ptr.h
1 /*
2     Copyright (C) 2023 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #ifndef DCPOMATIC_WX_PTR_H
23 #define DCPOMATIC_WX_PTR_H
24
25
26 #include <utility>
27
28
29 template <class T>
30 class wx_ptr
31 {
32 public:
33         explicit wx_ptr(T* wx)
34                 : _wx(wx)
35         {}
36
37         wx_ptr(wx_ptr&) = delete;
38         wx_ptr& operator=(wx_ptr&) = delete;
39
40         wx_ptr(wx_ptr&& other)
41         {
42                 _wx = other._wx;
43                 other._wx = nullptr;
44         }
45
46         wx_ptr& operator=(wx_ptr&& other)
47         {
48                 if (this != &other) {
49                         _wx = other._wx;
50                         other._wx = nullptr;
51                 }
52                 return *this;
53         }
54
55         ~wx_ptr()
56         {
57                 if (_wx) {
58                         _wx->Destroy();
59                 }
60         }
61
62         T* operator->() {
63                 return _wx;
64         }
65
66 private:
67         T* _wx;
68 };
69
70
71
72 template <class T, typename... Args>
73 wx_ptr<T>
74 make_wx(Args... args)
75 {
76         return wx_ptr<T>(new T(std::forward<Args>(args)...));
77 }
78
79
80 #endif