1 | /* |
2 | * Copyright © 2022 Behdad Esfahbod |
3 | * |
4 | * This is part of HarfBuzz, a text shaping library. |
5 | * |
6 | * Permission is hereby granted, without written agreement and without |
7 | * license or royalty fees, to use, copy, modify, and distribute this |
8 | * software and its documentation for any purpose, provided that the |
9 | * above copyright notice and the following two paragraphs appear in |
10 | * all copies of this software. |
11 | * |
12 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
13 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
14 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
15 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
16 | * DAMAGE. |
17 | * |
18 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
19 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
20 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
21 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
22 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
23 | */ |
24 | |
25 | #ifndef HB_CPLUSPLUS_HH |
26 | #define HB_CPLUSPLUS_HH |
27 | |
28 | #include "hb.h" |
29 | |
30 | HB_BEGIN_DECLS |
31 | HB_END_DECLS |
32 | |
33 | #ifdef __cplusplus |
34 | |
35 | #include <functional> |
36 | #include <utility> |
37 | |
38 | #if 0 |
39 | #if !(__cplusplus >= 201103L) |
40 | #error "HarfBuzz C++ helpers require C++11" |
41 | #endif |
42 | #endif |
43 | |
44 | namespace hb { |
45 | |
46 | |
47 | template <typename T> |
48 | struct vtable; |
49 | |
50 | template <typename T> |
51 | struct shared_ptr |
52 | { |
53 | using element_type = T; |
54 | |
55 | using v = vtable<T>; |
56 | |
57 | explicit shared_ptr (T *p = nullptr) : p (p) {} |
58 | shared_ptr (const shared_ptr &o) : p (v::reference (o.p)) {} |
59 | shared_ptr (shared_ptr &&o) : p (o.p) { o.p = nullptr; } |
60 | shared_ptr& operator = (const shared_ptr &o) { if (p != o.p) { destroy (); p = o.p; reference (); } return *this; } |
61 | shared_ptr& operator = (shared_ptr &&o) { v::destroy (p); p = o.p; o.p = nullptr; return *this; } |
62 | ~shared_ptr () { v::destroy (p); p = nullptr; } |
63 | |
64 | T* get() const { return p; } |
65 | |
66 | void swap (shared_ptr &o) { std::swap (p, o.p); } |
67 | friend void swap (shared_ptr &a, shared_ptr &b) { std::swap (a.p, b.p); } |
68 | |
69 | operator T * () const { return p; } |
70 | T& operator * () const { return *get (); } |
71 | T* operator -> () const { return get (); } |
72 | operator bool () const { return p; } |
73 | bool operator == (const shared_ptr &o) const { return p == o.p; } |
74 | bool operator != (const shared_ptr &o) const { return p != o.p; } |
75 | |
76 | static T* get_empty() { return v::get_empty (); } |
77 | T* reference() { return v::reference (p); } |
78 | void destroy() { v::destroy (p); } |
79 | void set_user_data (hb_user_data_key_t *key, |
80 | void *value, |
81 | hb_destroy_func_t destroy, |
82 | hb_bool_t replace) { v::set_user_data (p, key, value, destroy, replace); } |
83 | void * get_user_data (hb_user_data_key_t *key) { return v::get_user_data (p, key); } |
84 | |
85 | private: |
86 | T *p; |
87 | }; |
88 | |
89 | template<typename T> struct is_shared_ptr : std::false_type {}; |
90 | template<typename T> struct is_shared_ptr<shared_ptr<T>> : std::true_type {}; |
91 | |
92 | template <typename T> |
93 | struct unique_ptr |
94 | { |
95 | using element_type = T; |
96 | |
97 | using v = vtable<T>; |
98 | |
99 | explicit unique_ptr (T *p = nullptr) : p (p) {} |
100 | unique_ptr (const unique_ptr &o) = delete; |
101 | unique_ptr (unique_ptr &&o) : p (o.p) { o.p = nullptr; } |
102 | unique_ptr& operator = (const unique_ptr &o) = delete; |
103 | unique_ptr& operator = (unique_ptr &&o) { v::destroy (p); p = o.p; o.p = nullptr; return *this; } |
104 | ~unique_ptr () { v::destroy (p); p = nullptr; } |
105 | |
106 | T* get() const { return p; } |
107 | T* release () { T* v = p; p = nullptr; return v; } |
108 | |
109 | void swap (unique_ptr &o) { std::swap (p, o.p); } |
110 | friend void swap (unique_ptr &a, unique_ptr &b) { std::swap (a.p, b.p); } |
111 | |
112 | operator T * () const { return p; } |
113 | T& operator * () const { return *get (); } |
114 | T* operator -> () const { return get (); } |
115 | operator bool () { return p; } |
116 | |
117 | private: |
118 | T *p; |
119 | }; |
120 | |
121 | template<typename T> struct is_unique_ptr : std::false_type {}; |
122 | template<typename T> struct is_unique_ptr<unique_ptr<T>> : std::true_type {}; |
123 | |
124 | template <typename T, |
125 | T * (*_get_empty) (void), |
126 | T * (*_reference) (T *), |
127 | void (*_destroy) (T *), |
128 | hb_bool_t (*_set_user_data) (T *, |
129 | hb_user_data_key_t *, |
130 | void *, |
131 | hb_destroy_func_t, |
132 | hb_bool_t), |
133 | void * (*_get_user_data) (const T *, |
134 | hb_user_data_key_t *)> |
135 | struct vtable_t |
136 | { |
137 | static constexpr auto get_empty = _get_empty; |
138 | static constexpr auto reference = _reference; |
139 | static constexpr auto destroy = _destroy; |
140 | static constexpr auto set_user_data = _set_user_data; |
141 | static constexpr auto get_user_data = _get_user_data; |
142 | }; |
143 | |
144 | #define HB_DEFINE_VTABLE(name) \ |
145 | template<> \ |
146 | struct vtable<hb_##name##_t> \ |
147 | : vtable_t<hb_##name##_t, \ |
148 | &hb_##name##_get_empty, \ |
149 | &hb_##name##_reference, \ |
150 | &hb_##name##_destroy, \ |
151 | &hb_##name##_set_user_data, \ |
152 | &hb_##name##_get_user_data> {} |
153 | |
154 | HB_DEFINE_VTABLE (buffer); |
155 | HB_DEFINE_VTABLE (blob); |
156 | HB_DEFINE_VTABLE (face); |
157 | HB_DEFINE_VTABLE (font); |
158 | HB_DEFINE_VTABLE (font_funcs); |
159 | HB_DEFINE_VTABLE (map); |
160 | HB_DEFINE_VTABLE (set); |
161 | HB_DEFINE_VTABLE (shape_plan); |
162 | HB_DEFINE_VTABLE (unicode_funcs); |
163 | HB_DEFINE_VTABLE (draw_funcs); |
164 | HB_DEFINE_VTABLE (paint_funcs); |
165 | |
166 | #undef HB_DEFINE_VTABLE |
167 | |
168 | |
169 | #ifdef HB_SUBSET_H |
170 | |
171 | #define HB_DEFINE_VTABLE(name) \ |
172 | template<> \ |
173 | struct vtable<hb_##name##_t> \ |
174 | : vtable_t<hb_##name##_t, \ |
175 | nullptr, \ |
176 | &hb_##name##_reference, \ |
177 | &hb_##name##_destroy, \ |
178 | &hb_##name##_set_user_data, \ |
179 | &hb_##name##_get_user_data> {} |
180 | |
181 | |
182 | HB_DEFINE_VTABLE (subset_input); |
183 | HB_DEFINE_VTABLE (subset_plan); |
184 | |
185 | #undef HB_DEFINE_VTABLE |
186 | |
187 | #endif |
188 | |
189 | |
190 | } // namespace hb |
191 | |
192 | /* Workaround for GCC < 7, see: |
193 | * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480 |
194 | * https://stackoverflow.com/a/25594741 */ |
195 | namespace std { |
196 | |
197 | |
198 | template<typename T> |
199 | struct hash<hb::shared_ptr<T>> |
200 | { |
201 | std::size_t operator()(const hb::shared_ptr<T>& v) const noexcept |
202 | { |
203 | std::size_t h = std::hash<decltype (v.get ())>{}(v.get ()); |
204 | return h; |
205 | } |
206 | }; |
207 | |
208 | template<typename T> |
209 | struct hash<hb::unique_ptr<T>> |
210 | { |
211 | std::size_t operator()(const hb::unique_ptr<T>& v) const noexcept |
212 | { |
213 | std::size_t h = std::hash<decltype (v.get ())>{}(v.get ()); |
214 | return h; |
215 | } |
216 | }; |
217 | |
218 | |
219 | } // namespace std |
220 | |
221 | #endif /* __cplusplus */ |
222 | |
223 | #endif /* HB_CPLUSPLUS_HH */ |
224 | |