blob: 3ca2c92918c3f133cf72a16cbee93ff96467b3d7 [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; }
61 shared_ptr& operator = (shared_ptr &&o) { destroy (); p = o.p; o.p = nullptr; return *this; }
62 ~shared_ptr () { v::destroy (p); }
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 () { return p; }
73 bool operator == (const shared_ptr &o) { return p == o.p; }
74 bool operator != (const shared_ptr &o) { return p != o.p; }
75
76 static T* get_empty() { return v::get_empty (); }
77 void reference() { 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
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 Esfahbode0f3cab2022-06-01 11:51:43 -060092template <typename T,
93 T * (*_get_empty) (void),
94 T * (*_reference) (T *),
95 void (*_destroy) (T *),
96 hb_bool_t (*_set_user_data) (T *,
97 hb_user_data_key_t *,
98 void *,
99 hb_destroy_func_t,
100 hb_bool_t),
101 void * (*_get_user_data) (T *,
102 hb_user_data_key_t *)>
103struct vtable_t
104{
105 static constexpr auto get_empty = _get_empty;
106 static constexpr auto reference = _reference;
107 static constexpr auto destroy = _destroy;
108 static constexpr auto set_user_data = _set_user_data;
109 static constexpr auto get_user_data = _get_user_data;
110};
111
112#define HB_DEFINE_VTABLE(name) \
113 template<> \
114 struct vtable<hb_##name##_t> \
115 : vtable_t<hb_##name##_t, \
116 &hb_##name##_get_empty, \
117 &hb_##name##_reference, \
118 &hb_##name##_destroy, \
119 &hb_##name##_set_user_data, \
120 &hb_##name##_get_user_data> {}
121
122HB_DEFINE_VTABLE (buffer);
123HB_DEFINE_VTABLE (blob);
124HB_DEFINE_VTABLE (face);
125HB_DEFINE_VTABLE (font);
126HB_DEFINE_VTABLE (font_funcs);
127HB_DEFINE_VTABLE (map);
128HB_DEFINE_VTABLE (set);
129HB_DEFINE_VTABLE (shape_plan);
130HB_DEFINE_VTABLE (unicode_funcs);
131
132#undef HB_DEFINE_VTABLE
133
134
135} // namespace hb
136
Behdad Esfahboda089d912022-06-02 09:55:43 -0600137template<typename T>
138struct std::hash<hb::shared_ptr<T>>
139{
140 std::size_t operator()(const hb::shared_ptr<T>& v) const noexcept
141 {
142 std::size_t h = std::hash<decltype (v.get ())>{}(v.get ());
143 return h;
144 }
145};
146
Behdad Esfahboda089d912022-06-02 09:55:43 -0600147
Behdad Esfahbode0f3cab2022-06-01 11:51:43 -0600148#endif /* __cplusplus */
149
150#endif /* HB_CPLUSPLUS_HH */