Behdad Esfahbod | e0f3cab | 2022-06-01 11:51:43 -0600 | [diff] [blame] | 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 | |
Behdad Esfahbod | 7e7a4a8 | 2022-06-02 09:59:41 -0600 | [diff] [blame] | 35 | #include <functional> |
Behdad Esfahbod | e0f3cab | 2022-06-01 11:51:43 -0600 | [diff] [blame] | 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 | { |
Behdad Esfahbod | 0d3d5b6 | 2022-06-02 08:00:08 -0600 | [diff] [blame] | 53 | using element_type = T; |
| 54 | |
Behdad Esfahbod | e0f3cab | 2022-06-01 11:51:43 -0600 | [diff] [blame] | 55 | using v = vtable<T>; |
| 56 | |
Behdad Esfahbod | e0f3cab | 2022-06-01 11:51:43 -0600 | [diff] [blame] | 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; } |
Behdad Esfahbod | d7785a6 | 2022-06-02 12:42:24 -0600 | [diff] [blame] | 61 | shared_ptr& operator = (shared_ptr &&o) { v::destroy (p); p = o.p; o.p = nullptr; return *this; } |
Behdad Esfahbod | a42a703 | 2022-06-02 11:51:20 -0600 | [diff] [blame] | 62 | ~shared_ptr () { v::destroy (p); p = nullptr; } |
Behdad Esfahbod | e0f3cab | 2022-06-01 11:51:43 -0600 | [diff] [blame] | 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 (); } |
Behdad Esfahbod | 486fc22 | 2022-09-28 15:11:23 -0600 | [diff] [blame] | 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; } |
Behdad Esfahbod | e0f3cab | 2022-06-01 11:51:43 -0600 | [diff] [blame] | 75 | |
| 76 | static T* get_empty() { return v::get_empty (); } |
Behdad Esfahbod | 4d64677 | 2022-06-02 10:02:44 -0600 | [diff] [blame] | 77 | T* reference() { return v::reference (p); } |
Behdad Esfahbod | e0f3cab | 2022-06-01 11:51:43 -0600 | [diff] [blame] | 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 | |
Behdad Esfahbod | 0d3d5b6 | 2022-06-02 08:00:08 -0600 | [diff] [blame] | 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 | |
Behdad Esfahbod | d7785a6 | 2022-06-02 12:42:24 -0600 | [diff] [blame] | 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 | |
Behdad Esfahbod | e0f3cab | 2022-06-01 11:51:43 -0600 | [diff] [blame] | 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), |
Behdad Esfahbod | 1945b40 | 2022-07-25 10:45:55 -0600 | [diff] [blame] | 133 | void * (*_get_user_data) (const T *, |
Behdad Esfahbod | e0f3cab | 2022-06-01 11:51:43 -0600 | [diff] [blame] | 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 | |
| 164 | #undef HB_DEFINE_VTABLE |
| 165 | |
| 166 | |
Behdad Esfahbod | 1945b40 | 2022-07-25 10:45:55 -0600 | [diff] [blame] | 167 | #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 | |
| 180 | HB_DEFINE_VTABLE (subset_input); |
| 181 | HB_DEFINE_VTABLE (subset_plan); |
| 182 | |
| 183 | #undef HB_DEFINE_VTABLE |
| 184 | |
| 185 | #endif |
| 186 | |
| 187 | |
Behdad Esfahbod | e0f3cab | 2022-06-01 11:51:43 -0600 | [diff] [blame] | 188 | } // namespace hb |
| 189 | |
Kleis Auke Wolthuizen | 7b51bc9 | 2022-07-06 12:58:15 +0200 | [diff] [blame] | 190 | /* Workaround for GCC < 7, see: |
| 191 | * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480 |
| 192 | * https://stackoverflow.com/a/25594741 */ |
| 193 | namespace std { |
| 194 | |
| 195 | |
Behdad Esfahbod | a089d91 | 2022-06-02 09:55:43 -0600 | [diff] [blame] | 196 | template<typename T> |
Kleis Auke Wolthuizen | 7b51bc9 | 2022-07-06 12:58:15 +0200 | [diff] [blame] | 197 | struct hash<hb::shared_ptr<T>> |
Behdad Esfahbod | a089d91 | 2022-06-02 09:55:43 -0600 | [diff] [blame] | 198 | { |
| 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 Esfahbod | d7785a6 | 2022-06-02 12:42:24 -0600 | [diff] [blame] | 206 | template<typename T> |
Kleis Auke Wolthuizen | 7b51bc9 | 2022-07-06 12:58:15 +0200 | [diff] [blame] | 207 | struct hash<hb::unique_ptr<T>> |
Behdad Esfahbod | d7785a6 | 2022-06-02 12:42:24 -0600 | [diff] [blame] | 208 | { |
| 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 Esfahbod | a089d91 | 2022-06-02 09:55:43 -0600 | [diff] [blame] | 216 | |
Kleis Auke Wolthuizen | 7b51bc9 | 2022-07-06 12:58:15 +0200 | [diff] [blame] | 217 | } // namespace std |
| 218 | |
Behdad Esfahbod | e0f3cab | 2022-06-01 11:51:43 -0600 | [diff] [blame] | 219 | #endif /* __cplusplus */ |
| 220 | |
| 221 | #endif /* HB_CPLUSPLUS_HH */ |