Imported Upstream version 2.6.7
[platform/upstream/harfbuzz.git] / src / hb-meta.hh
1 /*
2  * Copyright © 2018  Google, Inc.
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  * Google Author(s): Behdad Esfahbod
25  */
26
27 #ifndef HB_META_HH
28 #define HB_META_HH
29
30 #include "hb.hh"
31
32
33 /*
34  * C++ template meta-programming & fundamentals used with them.
35  */
36
37 /* Void!  For when we need a expression-type of void. */
38 struct hb_empty_t {};
39
40 /* https://en.cppreference.com/w/cpp/types/void_t */
41 template<typename... Ts> struct _hb_void_t { typedef void type; };
42 template<typename... Ts> using hb_void_t = typename _hb_void_t<Ts...>::type;
43
44 template<typename Head, typename... Ts> struct _hb_head_t { typedef Head type; };
45 template<typename... Ts> using hb_head_t = typename _hb_head_t<Ts...>::type;
46
47 template <typename T, T v> struct hb_integral_constant { static constexpr T value = v; };
48 template <bool b> using hb_bool_constant = hb_integral_constant<bool, b>;
49 using hb_true_type = hb_bool_constant<true>;
50 using hb_false_type = hb_bool_constant<false>;
51
52
53 /* Basic type SFINAE. */
54
55 template <bool B, typename T = void> struct hb_enable_if {};
56 template <typename T>                struct hb_enable_if<true, T> { typedef T type; };
57 #define hb_enable_if(Cond) typename hb_enable_if<(Cond)>::type* = nullptr
58 /* Concepts/Requires alias: */
59 #define hb_requires(Cond) hb_enable_if((Cond))
60
61 template <typename T, typename T2> struct hb_is_same : hb_false_type {};
62 template <typename T>              struct hb_is_same<T, T> : hb_true_type {};
63 #define hb_is_same(T, T2) hb_is_same<T, T2>::value
64
65 /* Function overloading SFINAE and priority. */
66
67 #define HB_RETURN(Ret, E) -> hb_head_t<Ret, decltype ((E))> { return (E); }
68 #define HB_AUTO_RETURN(E) -> decltype ((E)) { return (E); }
69 #define HB_VOID_RETURN(E) -> hb_void_t<decltype ((E))> { (E); }
70
71 template <unsigned Pri> struct hb_priority : hb_priority<Pri - 1> {};
72 template <>             struct hb_priority<0> {};
73 #define hb_prioritize hb_priority<16> ()
74
75 #define HB_FUNCOBJ(x) static_const x HB_UNUSED
76
77
78 template <typename T> struct hb_type_identity_t { typedef T type; };
79 template <typename T> using hb_type_identity = typename hb_type_identity_t<T>::type;
80
81 struct
82 {
83   template <typename T> constexpr T*
84   operator () (T& arg) const
85   {
86 #pragma GCC diagnostic push
87 #pragma GCC diagnostic ignored "-Wcast-align"
88     /* https://en.cppreference.com/w/cpp/memory/addressof */
89     return reinterpret_cast<T*> (
90              &const_cast<char&> (
91                 reinterpret_cast<const volatile char&> (arg)));
92 #pragma GCC diagnostic pop
93   }
94 }
95 HB_FUNCOBJ (hb_addressof);
96
97 template <typename T> static inline T hb_declval ();
98 #define hb_declval(T) (hb_declval<T> ())
99
100 template <typename T> struct hb_match_const             : hb_type_identity_t<T>, hb_bool_constant<false>{};
101 template <typename T> struct hb_match_const<const T>    : hb_type_identity_t<T>, hb_bool_constant<true> {};
102 template <typename T> using hb_remove_const = typename hb_match_const<T>::type;
103 template <typename T> using hb_add_const = const T;
104 #define hb_is_const(T) hb_match_const<T>::value
105 template <typename T> struct hb_match_reference         : hb_type_identity_t<T>, hb_bool_constant<false>{};
106 template <typename T> struct hb_match_reference<T &>    : hb_type_identity_t<T>, hb_bool_constant<true> {};
107 template <typename T> struct hb_match_reference<T &&>   : hb_type_identity_t<T>, hb_bool_constant<true> {};
108 template <typename T> using hb_remove_reference = typename hb_match_reference<T>::type;
109 template <typename T> auto _hb_try_add_lvalue_reference (hb_priority<1>) -> hb_type_identity<T&>;
110 template <typename T> auto _hb_try_add_lvalue_reference (hb_priority<0>) -> hb_type_identity<T>;
111 template <typename T> using hb_add_lvalue_reference = decltype (_hb_try_add_lvalue_reference<T> (hb_prioritize));
112 template <typename T> auto _hb_try_add_rvalue_reference (hb_priority<1>) -> hb_type_identity<T&&>;
113 template <typename T> auto _hb_try_add_rvalue_reference (hb_priority<0>) -> hb_type_identity<T>;
114 template <typename T> using hb_add_rvalue_reference = decltype (_hb_try_add_rvalue_reference<T> (hb_prioritize));
115 #define hb_is_reference(T) hb_match_reference<T>::value
116 template <typename T> struct hb_match_pointer           : hb_type_identity_t<T>, hb_bool_constant<false>{};
117 template <typename T> struct hb_match_pointer<T *>      : hb_type_identity_t<T>, hb_bool_constant<true> {};
118 template <typename T> using hb_remove_pointer = typename hb_match_pointer<T>::type;
119 template <typename T> auto _hb_try_add_pointer (hb_priority<1>) -> hb_type_identity<hb_remove_reference<T>*>;
120 template <typename T> auto _hb_try_add_pointer (hb_priority<1>) -> hb_type_identity<T>;
121 template <typename T> using hb_add_pointer = decltype (_hb_try_add_pointer<T> (hb_prioritize));
122 #define hb_is_pointer(T) hb_match_pointer<T>::value
123
124
125 /* TODO Add feature-parity to std::decay. */
126 template <typename T> using hb_decay = hb_remove_const<hb_remove_reference<T>>;
127
128
129 template<bool B, class T, class F>
130 struct _hb_conditional { typedef T type; };
131 template<class T, class F>
132 struct _hb_conditional<false, T, F> { typedef F type; };
133 template<bool B, class T, class F>
134 using hb_conditional = typename _hb_conditional<B, T, F>::type;
135
136
137 template <typename From, typename To>
138 struct hb_is_convertible
139 {
140   private:
141   static constexpr bool   from_void = hb_is_same (void, hb_decay<From>);
142   static constexpr bool     to_void = hb_is_same (void, hb_decay<To>  );
143   static constexpr bool either_void = from_void || to_void;
144   static constexpr bool   both_void = from_void && to_void;
145
146   static hb_true_type impl2 (hb_conditional<to_void, int, To>);
147
148   template <typename T>
149   static auto impl (hb_priority<1>) -> decltype (impl2 (hb_declval (T)));
150   template <typename T>
151   static hb_false_type impl (hb_priority<0>);
152   public:
153   static constexpr bool value = both_void ||
154                        (!either_void &&
155                         decltype (impl<hb_conditional<from_void, int, From>> (hb_prioritize))::value);
156 };
157 #define hb_is_convertible(From,To) hb_is_convertible<From, To>::value
158
159 template <typename Base, typename Derived>
160 using hb_is_base_of = hb_is_convertible<hb_decay<Derived> *, hb_decay<Base> *>;
161 #define hb_is_base_of(Base,Derived) hb_is_base_of<Base, Derived>::value
162
163 template <typename From, typename To>
164 using hb_is_cr_convertible = hb_bool_constant<
165   hb_is_same (hb_decay<From>, hb_decay<To>) &&
166   (!hb_is_const (From) || hb_is_const (To)) &&
167   (!hb_is_reference (To) || hb_is_const (To) || hb_is_reference (To))
168 >;
169 #define hb_is_cr_convertible(From,To) hb_is_cr_convertible<From, To>::value
170
171 /* std::move and std::forward */
172
173 template <typename T>
174 static constexpr hb_remove_reference<T>&& hb_move (T&& t) { return (hb_remove_reference<T>&&) (t); }
175
176 template <typename T>
177 static constexpr T&& hb_forward (hb_remove_reference<T>& t) { return (T&&) t; }
178 template <typename T>
179 static constexpr T&& hb_forward (hb_remove_reference<T>&& t) { return (T&&) t; }
180
181 struct
182 {
183   template <typename T> constexpr auto
184   operator () (T&& v) const HB_AUTO_RETURN (hb_forward<T> (v))
185
186   template <typename T> constexpr auto
187   operator () (T *v) const HB_AUTO_RETURN (*v)
188 }
189 HB_FUNCOBJ (hb_deref);
190
191 struct
192 {
193   template <typename T> constexpr auto
194   operator () (T&& v) const HB_AUTO_RETURN (hb_forward<T> (v))
195
196   template <typename T> constexpr auto
197   operator () (T& v) const HB_AUTO_RETURN (hb_addressof (v))
198 }
199 HB_FUNCOBJ (hb_ref);
200
201 template <typename T>
202 struct hb_reference_wrapper
203 {
204   hb_reference_wrapper (T v) : v (v) {}
205   bool operator == (const hb_reference_wrapper& o) const { return v == o.v; }
206   bool operator != (const hb_reference_wrapper& o) const { return v != o.v; }
207   operator T () const { return v; }
208   T get () const { return v; }
209   T v;
210 };
211 template <typename T>
212 struct hb_reference_wrapper<T&>
213 {
214   hb_reference_wrapper (T& v) : v (hb_addressof (v)) {}
215   bool operator == (const hb_reference_wrapper& o) const { return v == o.v; }
216   bool operator != (const hb_reference_wrapper& o) const { return v != o.v; }
217   operator T& () const { return *v; }
218   T& get () const { return *v; }
219   T* v;
220 };
221
222
223 template <typename T>
224 using hb_is_integral = hb_bool_constant<
225   hb_is_same (hb_decay<T>, char) ||
226   hb_is_same (hb_decay<T>, signed char) ||
227   hb_is_same (hb_decay<T>, unsigned char) ||
228   hb_is_same (hb_decay<T>, signed int) ||
229   hb_is_same (hb_decay<T>, unsigned int) ||
230   hb_is_same (hb_decay<T>, signed short) ||
231   hb_is_same (hb_decay<T>, unsigned short) ||
232   hb_is_same (hb_decay<T>, signed long) ||
233   hb_is_same (hb_decay<T>, unsigned long) ||
234   hb_is_same (hb_decay<T>, signed long long) ||
235   hb_is_same (hb_decay<T>, unsigned long long) ||
236   false
237 >;
238 #define hb_is_integral(T) hb_is_integral<T>::value
239 template <typename T>
240 using hb_is_floating_point = hb_bool_constant<
241   hb_is_same (hb_decay<T>, float) ||
242   hb_is_same (hb_decay<T>, double) ||
243   hb_is_same (hb_decay<T>, long double) ||
244   false
245 >;
246 #define hb_is_floating_point(T) hb_is_floating_point<T>::value
247 template <typename T>
248 using hb_is_arithmetic = hb_bool_constant<
249   hb_is_integral (T) ||
250   hb_is_floating_point (T) ||
251   false
252 >;
253 #define hb_is_arithmetic(T) hb_is_arithmetic<T>::value
254
255
256 template <typename T>
257 using hb_is_signed = hb_conditional<hb_is_arithmetic (T),
258                                     hb_bool_constant<(T) -1 < (T) 0>,
259                                     hb_false_type>;
260 #define hb_is_signed(T) hb_is_signed<T>::value
261 template <typename T>
262 using hb_is_unsigned = hb_conditional<hb_is_arithmetic (T),
263                                       hb_bool_constant<(T) 0 < (T) -1>,
264                                       hb_false_type>;
265 #define hb_is_unsigned(T) hb_is_unsigned<T>::value
266
267 template <typename T> struct hb_int_min;
268 template <> struct hb_int_min<char>                     : hb_integral_constant<char,                    CHAR_MIN>       {};
269 template <> struct hb_int_min<signed char>              : hb_integral_constant<signed char,             SCHAR_MIN>      {};
270 template <> struct hb_int_min<unsigned char>            : hb_integral_constant<unsigned char,           0>              {};
271 template <> struct hb_int_min<signed short>             : hb_integral_constant<signed short,            SHRT_MIN>       {};
272 template <> struct hb_int_min<unsigned short>           : hb_integral_constant<unsigned short,          0>              {};
273 template <> struct hb_int_min<signed int>               : hb_integral_constant<signed int,              INT_MIN>        {};
274 template <> struct hb_int_min<unsigned int>             : hb_integral_constant<unsigned int,            0>              {};
275 template <> struct hb_int_min<signed long>              : hb_integral_constant<signed long,             LONG_MIN>       {};
276 template <> struct hb_int_min<unsigned long>            : hb_integral_constant<unsigned long,           0>              {};
277 template <> struct hb_int_min<signed long long>         : hb_integral_constant<signed long long,        LLONG_MIN>      {};
278 template <> struct hb_int_min<unsigned long long>       : hb_integral_constant<unsigned long long,      0>              {};
279 #define hb_int_min(T) hb_int_min<T>::value
280 template <typename T> struct hb_int_max;
281 template <> struct hb_int_max<char>                     : hb_integral_constant<char,                    CHAR_MAX>       {};
282 template <> struct hb_int_max<signed char>              : hb_integral_constant<signed char,             SCHAR_MAX>      {};
283 template <> struct hb_int_max<unsigned char>            : hb_integral_constant<unsigned char,           UCHAR_MAX>      {};
284 template <> struct hb_int_max<signed short>             : hb_integral_constant<signed short,            SHRT_MAX>       {};
285 template <> struct hb_int_max<unsigned short>           : hb_integral_constant<unsigned short,          USHRT_MAX>      {};
286 template <> struct hb_int_max<signed int>               : hb_integral_constant<signed int,              INT_MAX>        {};
287 template <> struct hb_int_max<unsigned int>             : hb_integral_constant<unsigned int,            UINT_MAX>       {};
288 template <> struct hb_int_max<signed long>              : hb_integral_constant<signed long,             LONG_MAX>       {};
289 template <> struct hb_int_max<unsigned long>            : hb_integral_constant<unsigned long,           ULONG_MAX>      {};
290 template <> struct hb_int_max<signed long long>         : hb_integral_constant<signed long long,        LLONG_MAX>      {};
291 template <> struct hb_int_max<unsigned long long>       : hb_integral_constant<unsigned long long,      ULLONG_MAX>     {};
292 #define hb_int_max(T) hb_int_max<T>::value
293
294
295
296 template <typename T, typename>
297 struct _hb_is_destructible : hb_false_type {};
298 template <typename T>
299 struct _hb_is_destructible<T, hb_void_t<decltype (hb_declval (T).~T ())>> : hb_true_type {};
300 template <typename T>
301 using hb_is_destructible = _hb_is_destructible<T, void>;
302 #define hb_is_destructible(T) hb_is_destructible<T>::value
303
304 template <typename T, typename, typename ...Ts>
305 struct _hb_is_constructible : hb_false_type {};
306 template <typename T, typename ...Ts>
307 struct _hb_is_constructible<T, hb_void_t<decltype (T (hb_declval (Ts)...))>, Ts...> : hb_true_type {};
308 template <typename T, typename ...Ts>
309 using hb_is_constructible = _hb_is_constructible<T, void, Ts...>;
310 #define hb_is_constructible(...) hb_is_constructible<__VA_ARGS__>::value
311
312 template <typename T>
313 using hb_is_default_constructible = hb_is_constructible<T>;
314 #define hb_is_default_constructible(T) hb_is_default_constructible<T>::value
315
316 template <typename T>
317 using hb_is_copy_constructible = hb_is_constructible<T, hb_add_lvalue_reference<hb_add_const<T>>>;
318 #define hb_is_copy_constructible(T) hb_is_copy_constructible<T>::value
319
320 template <typename T>
321 using hb_is_move_constructible = hb_is_constructible<T, hb_add_rvalue_reference<hb_add_const<T>>>;
322 #define hb_is_move_constructible(T) hb_is_move_constructible<T>::value
323
324 template <typename T, typename U, typename>
325 struct _hb_is_assignable : hb_false_type {};
326 template <typename T, typename U>
327 struct _hb_is_assignable<T, U, hb_void_t<decltype (hb_declval (T) = hb_declval (U))>> : hb_true_type {};
328 template <typename T, typename U>
329 using hb_is_assignable = _hb_is_assignable<T, U, void>;
330 #define hb_is_assignable(T,U) hb_is_assignable<T, U>::value
331
332 template <typename T>
333 using hb_is_copy_assignable = hb_is_assignable<hb_add_lvalue_reference<T>,
334                                                hb_add_lvalue_reference<hb_add_const<T>>>;
335 #define hb_is_copy_assignable(T) hb_is_copy_assignable<T>::value
336
337 template <typename T>
338 using hb_is_move_assignable = hb_is_assignable<hb_add_lvalue_reference<T>,
339                                                hb_add_rvalue_reference<T>>;
340 #define hb_is_move_assignable(T) hb_is_move_assignable<T>::value
341
342 /* Trivial versions. */
343
344 template <typename T> union hb_trivial { T value; };
345
346 /* Don't know how to do the following. */
347 template <typename T>
348 using hb_is_trivially_destructible= hb_is_destructible<hb_trivial<T>>;
349 #define hb_is_trivially_destructible(T) hb_is_trivially_destructible<T>::value
350
351 /* Don't know how to do the following. */
352 //template <typename T, typename ...Ts>
353 //using hb_is_trivially_constructible= hb_is_constructible<hb_trivial<T>, hb_trivial<Ts>...>;
354 //#define hb_is_trivially_constructible(...) hb_is_trivially_constructible<__VA_ARGS__>::value
355
356 template <typename T>
357 using hb_is_trivially_default_constructible= hb_is_default_constructible<hb_trivial<T>>;
358 #define hb_is_trivially_default_constructible(T) hb_is_trivially_default_constructible<T>::value
359
360 template <typename T>
361 using hb_is_trivially_copy_constructible= hb_is_copy_constructible<hb_trivial<T>>;
362 #define hb_is_trivially_copy_constructible(T) hb_is_trivially_copy_constructible<T>::value
363
364 template <typename T>
365 using hb_is_trivially_move_constructible= hb_is_move_constructible<hb_trivial<T>>;
366 #define hb_is_trivially_move_constructible(T) hb_is_trivially_move_constructible<T>::value
367
368 /* Don't know how to do the following. */
369 //template <typename T, typename U>
370 //using hb_is_trivially_assignable= hb_is_assignable<hb_trivial<T>, hb_trivial<U>>;
371 //#define hb_is_trivially_assignable(T,U) hb_is_trivially_assignable<T, U>::value
372
373 template <typename T>
374 using hb_is_trivially_copy_assignable= hb_is_copy_assignable<hb_trivial<T>>;
375 #define hb_is_trivially_copy_assignable(T) hb_is_trivially_copy_assignable<T>::value
376
377 template <typename T>
378 using hb_is_trivially_move_assignable= hb_is_move_assignable<hb_trivial<T>>;
379 #define hb_is_trivially_move_assignable(T) hb_is_trivially_move_assignable<T>::value
380
381 template <typename T>
382 using hb_is_trivially_copyable= hb_bool_constant<
383   hb_is_trivially_destructible (T) &&
384   (!hb_is_move_assignable (T) || hb_is_trivially_move_assignable (T)) &&
385   (!hb_is_move_constructible (T) || hb_is_trivially_move_constructible (T)) &&
386   (!hb_is_copy_assignable (T) || hb_is_trivially_copy_assignable (T)) &&
387   (!hb_is_copy_constructible (T) || hb_is_trivially_copy_constructible (T)) &&
388   true
389 >;
390 #define hb_is_trivially_copyable(T) hb_is_trivially_copyable<T>::value
391
392 template <typename T>
393 using hb_is_trivial= hb_bool_constant<
394   hb_is_trivially_copyable (T) &&
395   hb_is_trivially_default_constructible (T)
396 >;
397 #define hb_is_trivial(T) hb_is_trivial<T>::value
398
399 /* hb_unwrap_type (T)
400  * If T has no T::type, returns T. Otherwise calls itself on T::type recursively.
401  */
402
403 template <typename T, typename>
404 struct _hb_unwrap_type : hb_type_identity_t<T> {};
405 template <typename T>
406 struct _hb_unwrap_type<T, hb_void_t<typename T::type>> : _hb_unwrap_type<typename T::type, void> {};
407 template <typename T>
408 using hb_unwrap_type = _hb_unwrap_type<T, void>;
409 #define hb_unwrap_type(T) typename hb_unwrap_type<T>::type
410
411 #endif /* HB_META_HH */