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