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