1/*
2 * Copyright (C) 2015 Igalia S.L
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef XUniqueResource_h
27#define XUniqueResource_h
28
29#if PLATFORM(X11)
30
31#if USE(GLX)
32typedef unsigned long GLXPbuffer;
33typedef unsigned long GLXPixmap;
34#endif
35
36namespace WebCore {
37
38enum class XResource {
39 Colormap,
40#if PLATFORM(GTK)
41 Damage,
42#endif
43 Pixmap,
44 Window,
45#if USE(GLX)
46 GLXPbuffer,
47 GLXPixmap,
48#endif
49};
50
51template <XResource T> class XUniqueResource {
52public:
53 XUniqueResource()
54 {
55 }
56
57 XUniqueResource(unsigned long resource)
58 : m_resource(resource)
59 {
60 }
61
62 XUniqueResource(XUniqueResource&& uniqueResource)
63 : m_resource(uniqueResource.release())
64 {
65 }
66
67 XUniqueResource& operator=(XUniqueResource&& uniqueResource)
68 {
69 reset(uniqueResource.release());
70 return *this;
71 }
72
73 ~XUniqueResource()
74 {
75 reset();
76 }
77
78 unsigned long get() const { return m_resource; }
79 unsigned long release() { return std::exchange(m_resource, 0); }
80
81 void reset(unsigned long resource = 0)
82 {
83 std::swap(m_resource, resource);
84 deleteXResource(resource);
85 }
86
87 explicit operator bool() const { return m_resource; }
88
89private:
90 static void deleteXResource(unsigned long resource);
91
92 unsigned long m_resource { 0 };
93};
94
95using XUniqueColormap = XUniqueResource<XResource::Colormap>;
96#if PLATFORM(GTK)
97using XUniqueDamage = XUniqueResource<XResource::Damage>;
98#endif
99using XUniquePixmap = XUniqueResource<XResource::Pixmap>;
100using XUniqueWindow = XUniqueResource<XResource::Window>;
101#if USE(GLX)
102using XUniqueGLXPbuffer = XUniqueResource<XResource::GLXPbuffer>;
103using XUniqueGLXPixmap = XUniqueResource<XResource::GLXPixmap>;
104#endif
105
106} // namespace WebCore
107
108#endif // PLATFORM(X11)
109
110#endif // XUniqueResource_h
111