1 | /* |
2 | SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
5 | */ |
6 | #ifndef WAYLAND_POINTER_P_H |
7 | #define WAYLAND_POINTER_P_H |
8 | |
9 | struct wl_proxy; |
10 | |
11 | namespace KWayland |
12 | { |
13 | namespace Client |
14 | { |
15 | template<typename Pointer, void (*deleter)(Pointer *)> |
16 | class WaylandPointer |
17 | { |
18 | public: |
19 | WaylandPointer() = default; |
20 | WaylandPointer(Pointer *p) |
21 | : m_pointer(p) |
22 | { |
23 | } |
24 | WaylandPointer(const WaylandPointer &other) = delete; |
25 | virtual ~WaylandPointer() |
26 | { |
27 | release(); |
28 | } |
29 | |
30 | void setup(Pointer *pointer, bool foreign = false) |
31 | { |
32 | Q_ASSERT(pointer); |
33 | Q_ASSERT(!m_pointer); |
34 | m_pointer = pointer; |
35 | m_foreign = foreign; |
36 | } |
37 | |
38 | void release() |
39 | { |
40 | if (!m_pointer) { |
41 | return; |
42 | } |
43 | if (!m_foreign) { |
44 | deleter(m_pointer); |
45 | } |
46 | m_pointer = nullptr; |
47 | } |
48 | |
49 | void destroy() |
50 | { |
51 | if (!m_pointer) { |
52 | return; |
53 | } |
54 | if (!m_foreign) { |
55 | free(m_pointer); |
56 | } |
57 | m_pointer = nullptr; |
58 | } |
59 | |
60 | bool isValid() const |
61 | { |
62 | return m_pointer != nullptr; |
63 | } |
64 | |
65 | operator Pointer *() |
66 | { |
67 | return m_pointer; |
68 | } |
69 | |
70 | operator Pointer *() const |
71 | { |
72 | return m_pointer; |
73 | } |
74 | |
75 | operator wl_proxy *() |
76 | { |
77 | return reinterpret_cast<wl_proxy *>(m_pointer); |
78 | } |
79 | |
80 | Pointer *operator->() |
81 | { |
82 | return m_pointer; |
83 | } |
84 | |
85 | operator bool() |
86 | { |
87 | return isValid(); |
88 | } |
89 | |
90 | operator bool() const |
91 | { |
92 | return isValid(); |
93 | } |
94 | |
95 | private: |
96 | Pointer *m_pointer = nullptr; |
97 | bool m_foreign = false; |
98 | }; |
99 | |
100 | } |
101 | } |
102 | |
103 | #endif |
104 | |