File indexing completed on 2024-12-22 05:09:25

0001 /*
0002     SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 #ifndef WAYLAND_POINTER_P_H
0007 #define WAYLAND_POINTER_P_H
0008 
0009 struct wl_proxy;
0010 
0011 namespace KWayland
0012 {
0013 namespace Client
0014 {
0015 template<typename Pointer, void (*deleter)(Pointer *)>
0016 class WaylandPointer
0017 {
0018 public:
0019     WaylandPointer() = default;
0020     WaylandPointer(Pointer *p)
0021         : m_pointer(p)
0022     {
0023     }
0024     WaylandPointer(const WaylandPointer &other) = delete;
0025     virtual ~WaylandPointer()
0026     {
0027         release();
0028     }
0029 
0030     void setup(Pointer *pointer, bool foreign = false)
0031     {
0032         Q_ASSERT(pointer);
0033         Q_ASSERT(!m_pointer);
0034         m_pointer = pointer;
0035         m_foreign = foreign;
0036     }
0037 
0038     void release()
0039     {
0040         if (!m_pointer) {
0041             return;
0042         }
0043         if (!m_foreign) {
0044             deleter(m_pointer);
0045         }
0046         m_pointer = nullptr;
0047     }
0048 
0049     void destroy()
0050     {
0051         if (!m_pointer) {
0052             return;
0053         }
0054         if (!m_foreign) {
0055             free(m_pointer);
0056         }
0057         m_pointer = nullptr;
0058     }
0059 
0060     bool isValid() const
0061     {
0062         return m_pointer != nullptr;
0063     }
0064 
0065     operator Pointer *()
0066     {
0067         return m_pointer;
0068     }
0069 
0070     operator Pointer *() const
0071     {
0072         return m_pointer;
0073     }
0074 
0075     operator wl_proxy *()
0076     {
0077         return reinterpret_cast<wl_proxy *>(m_pointer);
0078     }
0079 
0080     Pointer *operator->()
0081     {
0082         return m_pointer;
0083     }
0084 
0085     operator bool()
0086     {
0087         return isValid();
0088     }
0089 
0090     operator bool() const
0091     {
0092         return isValid();
0093     }
0094 
0095 private:
0096     Pointer *m_pointer = nullptr;
0097     bool m_foreign = false;
0098 };
0099 
0100 }
0101 }
0102 
0103 #endif