blob: a210ab79605bd340ce7a7d9f85688e00a4e3e6b7 [file] [log] [blame]
Behdad Esfahbode0f3cab2022-06-01 11:51:43 -06001/*
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
30HB_BEGIN_DECLS
31HB_END_DECLS
32
33#ifdef __cplusplus
34
Behdad Esfahbod7e7a4a82022-06-02 09:59:41 -060035#include <functional>
Behdad Esfahbode0f3cab2022-06-01 11:51:43 -060036#include <utility>
37
38#if 0
39#if !(__cplusplus >= 201103L)
40#error "HarfBuzz C++ helpers require C++11"
41#endif
42#endif
43
44namespace hb {
45
46
47template <typename T>
48struct vtable;
49
50template <typename T>
51struct shared_ptr
52{
Behdad Esfahbod0d3d5b62022-06-02 08:00:08 -060053 using element_type = T;
54
Behdad Esfahbode0f3cab2022-06-01 11:51:43 -060055 using v = vtable<T>;
56
Behdad Esfahbode0f3cab2022-06-01 11:51:43 -060057 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; }
Behdad Esfahbodd7785a62022-06-02 12:42:24 -060061 shared_ptr& operator = (shared_ptr &&o) { v::destroy (p); p = o.p; o.p = nullptr; return *this; }
Behdad Esfahboda42a7032022-06-02 11:51:20 -060062 ~shared_ptr () { v::destroy (p); p = nullptr; }
Behdad Esfahbode0f3cab2022-06-01 11:51:43 -060063
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 (); }
Behdad Esfahbod486fc222022-09-28 15:11:23 -060072 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; }
Behdad Esfahbode0f3cab2022-06-01 11:51:43 -060075
76 static T* get_empty() { return v::get_empty (); }
Behdad Esfahbod4d646772022-06-02 10:02:44 -060077 T* reference() { return v::reference (p); }
Behdad Esfahbode0f3cab2022-06-01 11:51:43 -060078 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
Behdad Esfahbod0d3d5b62022-06-02 08:00:08 -060089template<typename T> struct is_shared_ptr : std::false_type {};
90template<typename T> struct is_shared_ptr<shared_ptr<T>> : std::true_type {};
91
Behdad Esfahbodd7785a62022-06-02 12:42:24 -060092template <typename T>
93struct 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
121template<typename T> struct is_unique_ptr : std::false_type {};
122template<typename T> struct is_unique_ptr<unique_ptr<T>> : std::true_type {};
123
Behdad Esfahbode0f3cab2022-06-01 11:51:43 -0600124template <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),
Behdad Esfahbod1945b402022-07-25 10:45:55 -0600133 void * (*_get_user_data) (const T *,
Behdad Esfahbode0f3cab2022-06-01 11:51:43 -0600134 hb_user_data_key_t *)>
135struct 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
154HB_DEFINE_VTABLE (buffer);
155HB_DEFINE_VTABLE (blob);
156HB_DEFINE_VTABLE (face);
157HB_DEFINE_VTABLE (font);
158HB_DEFINE_VTABLE (font_funcs);
159HB_DEFINE_VTABLE (map);
160HB_DEFINE_VTABLE (set);
161HB_DEFINE_VTABLE (shape_plan);
162HB_DEFINE_VTABLE (unicode_funcs);
163
164#undef HB_DEFINE_VTABLE
165
166
Behdad Esfahbod1945b402022-07-25 10:45:55 -0600167#ifdef HB_SUBSET_H
168
169#define HB_DEFINE_VTABLE(name) \
170 template<> \
171 struct vtable<hb_##name##_t> \
172 : vtable_t<hb_##name##_t, \
173 nullptr, \
174 &hb_##name##_reference, \
175 &hb_##name##_destroy, \
176 &hb_##name##_set_user_data, \
177 &hb_##name##_get_user_data> {}
178
179
180HB_DEFINE_VTABLE (subset_input);
181HB_DEFINE_VTABLE (subset_plan);
182
183#undef HB_DEFINE_VTABLE
184
185#endif
186
187
Behdad Esfahbode0f3cab2022-06-01 11:51:43 -0600188} // namespace hb
189
Kleis Auke Wolthuizen7b51bc92022-07-06 12:58:15 +0200190/* Workaround for GCC < 7, see:
191 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
192 * https://stackoverflow.com/a/25594741 */
193namespace std {
194
195
Behdad Esfahboda089d912022-06-02 09:55:43 -0600196template<typename T>
Kleis Auke Wolthuizen7b51bc92022-07-06 12:58:15 +0200197struct hash<hb::shared_ptr<T>>
Behdad Esfahboda089d912022-06-02 09:55:43 -0600198{
199 std::size_t operator()(const hb::shared_ptr<T>& v) const noexcept
200 {
201 std::size_t h = std::hash<decltype (v.get ())>{}(v.get ());
202 return h;
203 }
204};
205
Behdad Esfahbodd7785a62022-06-02 12:42:24 -0600206template<typename T>
Kleis Auke Wolthuizen7b51bc92022-07-06 12:58:15 +0200207struct hash<hb::unique_ptr<T>>
Behdad Esfahbodd7785a62022-06-02 12:42:24 -0600208{
209 std::size_t operator()(const hb::unique_ptr<T>& v) const noexcept
210 {
211 std::size_t h = std::hash<decltype (v.get ())>{}(v.get ());
212 return h;
213 }
214};
215
Behdad Esfahboda089d912022-06-02 09:55:43 -0600216
Kleis Auke Wolthuizen7b51bc92022-07-06 12:58:15 +0200217} // namespace std
218
Behdad Esfahbode0f3cab2022-06-01 11:51:43 -0600219#endif /* __cplusplus */
220
221#endif /* HB_CPLUSPLUS_HH */