ASPiK SDK
cairoutils.h
1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #pragma once
6 
7 #include <cairo/cairo.h>
8 #include <utility>
9 
10 //-----------------------------------------------------------------------------
11 namespace VSTGUI {
12 namespace Cairo {
13 
14 //-----------------------------------------------------------------------------
15 template <typename Type, typename RetainProcType, RetainProcType RetainProc,
16  typename ReleaseProcType, ReleaseProcType ReleaseProc>
17 class Handle
18 {
19 public:
20  Handle () {}
21  explicit Handle (Type h) : handle (h) {}
22  ~Handle () { reset (); }
23  Handle (Handle&& o) { *this = std::move (o); }
24  Handle& operator= (Handle&& o)
25  {
26  reset ();
27  std::swap (handle, o.handle);
28  return *this;
29  }
30 
31  Handle (const Handle& o) { *this = o; }
32  Handle& operator= (const Handle& o)
33  {
34  reset ();
35  if (o.handle)
36  {
37  handle = RetainProc (o.handle);
38  }
39  return *this;
40  }
41 
42  void assign (Type h)
43  {
44  reset ();
45  handle = h;
46  }
47 
48  void reset ()
49  {
50  if (handle)
51  {
52  ReleaseProc (handle);
53  handle = nullptr;
54  }
55  }
56 
57  operator Type () const { return handle; }
58  operator bool () const { return handle != nullptr; }
59 
60 private:
61  Type handle {nullptr};
62 };
63 
64 using ContextHandle = Handle<cairo_t*, decltype (&cairo_reference), cairo_reference,
65  decltype (&cairo_destroy), cairo_destroy>;
66 
67 using SurfaceHandle =
68  Handle<cairo_surface_t*, decltype (&cairo_surface_reference), cairo_surface_reference,
69  decltype (&cairo_surface_destroy), cairo_surface_destroy>;
70 
71 using PatternHandle =
72  Handle<cairo_pattern_t*, decltype (&cairo_pattern_reference), cairo_pattern_reference,
73  decltype (&cairo_pattern_destroy), cairo_pattern_destroy>;
74 
75 using ScaledFontHandle = Handle<cairo_scaled_font_t*, decltype (&cairo_scaled_font_reference),
76  cairo_scaled_font_reference, decltype (&cairo_scaled_font_destroy),
77  cairo_scaled_font_destroy>;
78 
79 //-----------------------------------------------------------------------------
80 } // Cairo
81 } // VSTGUI
Definition: cairoutils.h:17
Definition: customcontrols.cpp:8