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. */
38struct hb_empty_t {};
39
40/* https://en.cppreference.com/w/cpp/types/void_t */
41template<typename... Ts> struct _hb_void_t { typedef void type; };
42template<typename... Ts> using hb_void_t = typename _hb_void_t<Ts...>::type;
43
44template<typename Head, typename... Ts> struct _hb_head_t { typedef Head type; };
45template<typename... Ts> using hb_head_t = typename _hb_head_t<Ts...>::type;
46
47template <typename T, T v> struct hb_integral_constant { static constexpr T value = v; };
48template <bool b> using hb_bool_constant = hb_integral_constant<bool, b>;
49using hb_true_type = hb_bool_constant<true>;
50using hb_false_type = hb_bool_constant<false>;
51
52
53/* Basic type SFINAE. */
54
55template <bool B, typename T = void> struct hb_enable_if {};
56template <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
61template <typename T, typename T2> struct hb_is_same : hb_false_type {};
62template <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
71template <unsigned Pri> struct hb_priority : hb_priority<Pri - 1> {};
72template <> struct hb_priority<0> {};
73#define hb_prioritize hb_priority<16> ()
74
75#define HB_FUNCOBJ(x) static_const x HB_UNUSED
76
77
78template <typename T> struct hb_type_identity_t { typedef T type; };
79template <typename T> using hb_type_identity = typename hb_type_identity_t<T>::type;
80
81struct
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}
95HB_FUNCOBJ (hb_addressof);
96
97template <typename T> static inline T hb_declval ();
98#define hb_declval(T) (hb_declval<T> ())
99
100template <typename T> struct hb_match_const : hb_type_identity_t<T>, hb_bool_constant<false>{};
101template <typename T> struct hb_match_const<const T> : hb_type_identity_t<T>, hb_bool_constant<true> {};
102template <typename T> using hb_remove_const = typename hb_match_const<T>::type;
103template <typename T> using hb_add_const = const T;
104#define hb_is_const(T) hb_match_const<T>::value
105template <typename T> struct hb_match_reference : hb_type_identity_t<T>, hb_bool_constant<false>{};
106template <typename T> struct hb_match_reference<T &> : hb_type_identity_t<T>, hb_bool_constant<true> {};
107template <typename T> struct hb_match_reference<T &&> : hb_type_identity_t<T>, hb_bool_constant<true> {};
108template <typename T> using hb_remove_reference = typename hb_match_reference<T>::type;
109template <typename T> auto _hb_try_add_lvalue_reference (hb_priority<1>) -> hb_type_identity<T&>;
110template <typename T> auto _hb_try_add_lvalue_reference (hb_priority<0>) -> hb_type_identity<T>;
111template <typename T> using hb_add_lvalue_reference = decltype (_hb_try_add_lvalue_reference<T> (hb_prioritize));
112template <typename T> auto _hb_try_add_rvalue_reference (hb_priority<1>) -> hb_type_identity<T&&>;
113template <typename T> auto _hb_try_add_rvalue_reference (hb_priority<0>) -> hb_type_identity<T>;
114template <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
116template <typename T> struct hb_match_pointer : hb_type_identity_t<T>, hb_bool_constant<false>{};
117template <typename T> struct hb_match_pointer<T *> : hb_type_identity_t<T>, hb_bool_constant<true> {};
118template <typename T> using hb_remove_pointer = typename hb_match_pointer<T>::type;
119template <typename T> auto _hb_try_add_pointer (hb_priority<1>) -> hb_type_identity<hb_remove_reference<T>*>;
120template <typename T> auto _hb_try_add_pointer (hb_priority<1>) -> hb_type_identity<T>;
121template <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. */
126template <typename T> using hb_decay = hb_remove_const<hb_remove_reference<T>>;
127
128
129template<bool B, class T, class F>
130struct _hb_conditional { typedef T type; };
131template<class T, class F>
132struct _hb_conditional<false, T, F> { typedef F type; };
133template<bool B, class T, class F>
134using hb_conditional = typename _hb_conditional<B, T, F>::type;
135
136
137template <typename From, typename To>
138struct 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
159template <typename Base, typename Derived>
160using 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
163template <typename From, typename To>
164using 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
173template <typename T>
174static constexpr hb_remove_reference<T>&& hb_move (T&& t) { return (hb_remove_reference<T>&&) (t); }
175
176template <typename T>
177static constexpr T&& hb_forward (hb_remove_reference<T>& t) { return (T&&) t; }
178template <typename T>
179static constexpr T&& hb_forward (hb_remove_reference<T>&& t) { return (T&&) t; }
180
181struct
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}
189HB_FUNCOBJ (hb_deref);
190
191struct
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}
199HB_FUNCOBJ (hb_ref);
200
201template <typename T>
202struct 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};
211template <typename T>
212struct 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
223template <typename T>
224using 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
239template <typename T>
240using 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
247template <typename T>
248using 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
256template <typename T>
257using 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
261template <typename T>
262using 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
267template <typename T> struct hb_int_min;
268template <> struct hb_int_min<char> : hb_integral_constant<char, CHAR_MIN> {};
269template <> struct hb_int_min<signed char> : hb_integral_constant<signed char, SCHAR_MIN> {};
270template <> struct hb_int_min<unsigned char> : hb_integral_constant<unsigned char, 0> {};
271template <> struct hb_int_min<signed short> : hb_integral_constant<signed short, SHRT_MIN> {};
272template <> struct hb_int_min<unsigned short> : hb_integral_constant<unsigned short, 0> {};
273template <> struct hb_int_min<signed int> : hb_integral_constant<signed int, INT_MIN> {};
274template <> struct hb_int_min<unsigned int> : hb_integral_constant<unsigned int, 0> {};
275template <> struct hb_int_min<signed long> : hb_integral_constant<signed long, LONG_MIN> {};
276template <> struct hb_int_min<unsigned long> : hb_integral_constant<unsigned long, 0> {};
277template <> struct hb_int_min<signed long long> : hb_integral_constant<signed long long, LLONG_MIN> {};
278template <> 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
280template <typename T> struct hb_int_max;
281template <> struct hb_int_max<char> : hb_integral_constant<char, CHAR_MAX> {};
282template <> struct hb_int_max<signed char> : hb_integral_constant<signed char, SCHAR_MAX> {};
283template <> struct hb_int_max<unsigned char> : hb_integral_constant<unsigned char, UCHAR_MAX> {};
284template <> struct hb_int_max<signed short> : hb_integral_constant<signed short, SHRT_MAX> {};
285template <> struct hb_int_max<unsigned short> : hb_integral_constant<unsigned short, USHRT_MAX> {};
286template <> struct hb_int_max<signed int> : hb_integral_constant<signed int, INT_MAX> {};
287template <> struct hb_int_max<unsigned int> : hb_integral_constant<unsigned int, UINT_MAX> {};
288template <> struct hb_int_max<signed long> : hb_integral_constant<signed long, LONG_MAX> {};
289template <> struct hb_int_max<unsigned long> : hb_integral_constant<unsigned long, ULONG_MAX> {};
290template <> struct hb_int_max<signed long long> : hb_integral_constant<signed long long, LLONG_MAX> {};
291template <> 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
296template <typename T, typename>
297struct _hb_is_destructible : hb_false_type {};
298template <typename T>
299struct _hb_is_destructible<T, hb_void_t<decltype (hb_declval (T).~T ())>> : hb_true_type {};
300template <typename T>
301using hb_is_destructible = _hb_is_destructible<T, void>;
302#define hb_is_destructible(T) hb_is_destructible<T>::value
303
304template <typename T, typename, typename ...Ts>
305struct _hb_is_constructible : hb_false_type {};
306template <typename T, typename ...Ts>
307struct _hb_is_constructible<T, hb_void_t<decltype (T (hb_declval (Ts)...))>, Ts...> : hb_true_type {};
308template <typename T, typename ...Ts>
309using hb_is_constructible = _hb_is_constructible<T, void, Ts...>;
310#define hb_is_constructible(...) hb_is_constructible<__VA_ARGS__>::value
311
312template <typename T>
313using hb_is_default_constructible = hb_is_constructible<T>;
314#define hb_is_default_constructible(T) hb_is_default_constructible<T>::value
315
316template <typename T>
317using 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
320template <typename T>
321using 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
324template <typename T, typename U, typename>
325struct _hb_is_assignable : hb_false_type {};
326template <typename T, typename U>
327struct _hb_is_assignable<T, U, hb_void_t<decltype (hb_declval (T) = hb_declval (U))>> : hb_true_type {};
328template <typename T, typename U>
329using hb_is_assignable = _hb_is_assignable<T, U, void>;
330#define hb_is_assignable(T,U) hb_is_assignable<T, U>::value
331
332template <typename T>
333using 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
337template <typename T>
338using 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
344template <typename T> union hb_trivial { T value; };
345
346template <typename T>
347using hb_is_trivially_destructible= hb_is_destructible<hb_trivial<T>>;
348#define hb_is_trivially_destructible(T) hb_is_trivially_destructible<T>::value
349
350/* Don't know how to do the following. */
351//template <typename T, typename ...Ts>
352//using hb_is_trivially_constructible= hb_is_constructible<hb_trivial<T>, hb_trivial<Ts>...>;
353//#define hb_is_trivially_constructible(...) hb_is_trivially_constructible<__VA_ARGS__>::value
354
355template <typename T>
356using hb_is_trivially_default_constructible= hb_is_default_constructible<hb_trivial<T>>;
357#define hb_is_trivially_default_constructible(T) hb_is_trivially_default_constructible<T>::value
358
359template <typename T>
360using hb_is_trivially_copy_constructible= hb_is_copy_constructible<hb_trivial<T>>;
361#define hb_is_trivially_copy_constructible(T) hb_is_trivially_copy_constructible<T>::value
362
363template <typename T>
364using hb_is_trivially_move_constructible= hb_is_move_constructible<hb_trivial<T>>;
365#define hb_is_trivially_move_constructible(T) hb_is_trivially_move_constructible<T>::value
366
367/* Don't know how to do the following. */
368//template <typename T, typename U>
369//using hb_is_trivially_assignable= hb_is_assignable<hb_trivial<T>, hb_trivial<U>>;
370//#define hb_is_trivially_assignable(T,U) hb_is_trivially_assignable<T, U>::value
371
372template <typename T>
373using hb_is_trivially_copy_assignable= hb_is_copy_assignable<hb_trivial<T>>;
374#define hb_is_trivially_copy_assignable(T) hb_is_trivially_copy_assignable<T>::value
375
376template <typename T>
377using hb_is_trivially_move_assignable= hb_is_move_assignable<hb_trivial<T>>;
378#define hb_is_trivially_move_assignable(T) hb_is_trivially_move_assignable<T>::value
379
380template <typename T>
381using hb_is_trivially_copyable= hb_bool_constant<
382 hb_is_trivially_destructible (T) &&
383 (!hb_is_move_assignable (T) || hb_is_trivially_move_assignable (T)) &&
384 (!hb_is_move_constructible (T) || hb_is_trivially_move_constructible (T)) &&
385 (!hb_is_copy_assignable (T) || hb_is_trivially_copy_assignable (T)) &&
386 (!hb_is_copy_constructible (T) || hb_is_trivially_copy_constructible (T)) &&
387 true
388>;
389#define hb_is_trivially_copyable(T) hb_is_trivially_copyable<T>::value
390
391template <typename T>
392using hb_is_trivial= hb_bool_constant<
393 hb_is_trivially_copyable (T) &&
394 hb_is_trivially_default_constructible (T)
395>;
396#define hb_is_trivial(T) hb_is_trivial<T>::value
397
398/* hb_unwrap_type (T)
399 * If T has no T::type, returns T. Otherwise calls itself on T::type recursively.
400 */
401
402template <typename T, typename>
403struct _hb_unwrap_type : hb_type_identity_t<T> {};
404template <typename T>
405struct _hb_unwrap_type<T, hb_void_t<typename T::type>> : _hb_unwrap_type<typename T::type, void> {};
406template <typename T>
407using hb_unwrap_type = _hb_unwrap_type<T, void>;
408#define hb_unwrap_type(T) typename hb_unwrap_type<T>::type
409
410#endif /* HB_META_HH */
411