hb-meta.hh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #ifndef HB_META_HH
  27. #define HB_META_HH
  28. #include "hb.hh"
  29. #include <memory>
  30. #include <type_traits>
  31. #include <utility>
  32. /*
  33. * C++ template meta-programming & fundamentals used with them.
  34. */
  35. /* Void! For when we need a expression-type of void. */
  36. struct hb_empty_t {};
  37. /* https://en.cppreference.com/w/cpp/types/void_t */
  38. template<typename... Ts> struct _hb_void_t { typedef void type; };
  39. template<typename... Ts> using hb_void_t = typename _hb_void_t<Ts...>::type;
  40. template<typename Head, typename... Ts> struct _hb_head_t { typedef Head type; };
  41. template<typename... Ts> using hb_head_t = typename _hb_head_t<Ts...>::type;
  42. template <typename T, T v> struct hb_integral_constant { static constexpr T value = v; };
  43. template <bool b> using hb_bool_constant = hb_integral_constant<bool, b>;
  44. using hb_true_type = hb_bool_constant<true>;
  45. using hb_false_type = hb_bool_constant<false>;
  46. /* Static-assert as expression. */
  47. template <bool cond> struct static_assert_expr;
  48. template <> struct static_assert_expr<true> : hb_false_type {};
  49. #define static_assert_expr(C) static_assert_expr<C>::value
  50. /* Basic type SFINAE. */
  51. template <bool B, typename T = void> struct hb_enable_if {};
  52. template <typename T> struct hb_enable_if<true, T> { typedef T type; };
  53. #define hb_enable_if(Cond) typename hb_enable_if<(Cond)>::type* = nullptr
  54. /* Concepts/Requires alias: */
  55. #define hb_requires(Cond) hb_enable_if((Cond))
  56. template <typename T, typename T2> struct hb_is_same : hb_false_type {};
  57. template <typename T> struct hb_is_same<T, T> : hb_true_type {};
  58. #define hb_is_same(T, T2) hb_is_same<T, T2>::value
  59. /* Function overloading SFINAE and priority. */
  60. #define HB_RETURN(Ret, E) -> hb_head_t<Ret, decltype ((E))> { return (E); }
  61. #define HB_AUTO_RETURN(E) -> decltype ((E)) { return (E); }
  62. #define HB_VOID_RETURN(E) -> hb_void_t<decltype ((E))> { (E); }
  63. template <unsigned Pri> struct hb_priority : hb_priority<Pri - 1> {};
  64. template <> struct hb_priority<0> {};
  65. #define hb_prioritize hb_priority<16> ()
  66. #define HB_FUNCOBJ(x) static_const x HB_UNUSED
  67. template <typename T> struct hb_type_identity_t { typedef T type; };
  68. template <typename T> using hb_type_identity = typename hb_type_identity_t<T>::type;
  69. template <typename T> static inline T hb_declval ();
  70. #define hb_declval(T) (hb_declval<T> ())
  71. template <typename T> struct hb_match_const : hb_type_identity_t<T>, hb_false_type {};
  72. template <typename T> struct hb_match_const<const T> : hb_type_identity_t<T>, hb_true_type {};
  73. template <typename T> using hb_remove_const = typename hb_match_const<T>::type;
  74. template <typename T> struct hb_match_reference : hb_type_identity_t<T>, hb_false_type {};
  75. template <typename T> struct hb_match_reference<T &> : hb_type_identity_t<T>, hb_true_type {};
  76. template <typename T> struct hb_match_reference<T &&> : hb_type_identity_t<T>, hb_true_type {};
  77. template <typename T> using hb_remove_reference = typename hb_match_reference<T>::type;
  78. template <typename T> auto _hb_try_add_lvalue_reference (hb_priority<1>) -> hb_type_identity<T&>;
  79. template <typename T> auto _hb_try_add_lvalue_reference (hb_priority<0>) -> hb_type_identity<T>;
  80. template <typename T> using hb_add_lvalue_reference = decltype (_hb_try_add_lvalue_reference<T> (hb_prioritize));
  81. template <typename T> auto _hb_try_add_rvalue_reference (hb_priority<1>) -> hb_type_identity<T&&>;
  82. template <typename T> auto _hb_try_add_rvalue_reference (hb_priority<0>) -> hb_type_identity<T>;
  83. template <typename T> using hb_add_rvalue_reference = decltype (_hb_try_add_rvalue_reference<T> (hb_prioritize));
  84. template <typename T> struct hb_match_pointer : hb_type_identity_t<T>, hb_false_type {};
  85. template <typename T> struct hb_match_pointer<T *> : hb_type_identity_t<T>, hb_true_type {};
  86. template <typename T> using hb_remove_pointer = typename hb_match_pointer<T>::type;
  87. template <typename T> auto _hb_try_add_pointer (hb_priority<1>) -> hb_type_identity<hb_remove_reference<T>*>;
  88. template <typename T> auto _hb_try_add_pointer (hb_priority<1>) -> hb_type_identity<T>;
  89. template <typename T> using hb_add_pointer = decltype (_hb_try_add_pointer<T> (hb_prioritize));
  90. template <typename T> using hb_decay = typename std::decay<T>::type;
  91. #define hb_is_convertible(From,To) std::is_convertible<From, To>::value
  92. template <typename From, typename To>
  93. using hb_is_cr_convertible = hb_bool_constant<
  94. hb_is_same (hb_decay<From>, hb_decay<To>) &&
  95. (!std::is_const<From>::value || std::is_const<To>::value) &&
  96. (!std::is_reference<To>::value || std::is_const<To>::value || std::is_reference<To>::value)
  97. >;
  98. #define hb_is_cr_convertible(From,To) hb_is_cr_convertible<From, To>::value
  99. struct
  100. {
  101. template <typename T> constexpr auto
  102. operator () (T&& v) const HB_AUTO_RETURN (std::forward<T> (v))
  103. template <typename T> constexpr auto
  104. operator () (T *v) const HB_AUTO_RETURN (*v)
  105. template <typename T> constexpr auto
  106. operator () (const hb::shared_ptr<T>& v) const HB_AUTO_RETURN (*v)
  107. template <typename T> constexpr auto
  108. operator () (hb::shared_ptr<T>& v) const HB_AUTO_RETURN (*v)
  109. template <typename T> constexpr auto
  110. operator () (const hb::unique_ptr<T>& v) const HB_AUTO_RETURN (*v)
  111. template <typename T> constexpr auto
  112. operator () (hb::unique_ptr<T>& v) const HB_AUTO_RETURN (*v)
  113. }
  114. HB_FUNCOBJ (hb_deref);
  115. template <typename T>
  116. struct hb_reference_wrapper
  117. {
  118. hb_reference_wrapper (T v) : v (v) {}
  119. bool operator == (const hb_reference_wrapper& o) const { return v == o.v; }
  120. bool operator != (const hb_reference_wrapper& o) const { return v != o.v; }
  121. operator T () const { return v; }
  122. T get () const { return v; }
  123. T v;
  124. };
  125. template <typename T>
  126. struct hb_reference_wrapper<T&>
  127. {
  128. hb_reference_wrapper (T& v) : v (std::addressof (v)) {}
  129. bool operator == (const hb_reference_wrapper& o) const { return v == o.v; }
  130. bool operator != (const hb_reference_wrapper& o) const { return v != o.v; }
  131. operator T& () const { return *v; }
  132. T& get () const { return *v; }
  133. T* v;
  134. };
  135. /* Type traits */
  136. template <typename T> struct hb_int_min;
  137. template <> struct hb_int_min<char> : hb_integral_constant<char, CHAR_MIN> {};
  138. template <> struct hb_int_min<signed char> : hb_integral_constant<signed char, SCHAR_MIN> {};
  139. template <> struct hb_int_min<unsigned char> : hb_integral_constant<unsigned char, 0> {};
  140. template <> struct hb_int_min<signed short> : hb_integral_constant<signed short, SHRT_MIN> {};
  141. template <> struct hb_int_min<unsigned short> : hb_integral_constant<unsigned short, 0> {};
  142. template <> struct hb_int_min<signed int> : hb_integral_constant<signed int, INT_MIN> {};
  143. template <> struct hb_int_min<unsigned int> : hb_integral_constant<unsigned int, 0> {};
  144. template <> struct hb_int_min<signed long> : hb_integral_constant<signed long, LONG_MIN> {};
  145. template <> struct hb_int_min<unsigned long> : hb_integral_constant<unsigned long, 0> {};
  146. template <> struct hb_int_min<signed long long> : hb_integral_constant<signed long long, LLONG_MIN> {};
  147. template <> struct hb_int_min<unsigned long long> : hb_integral_constant<unsigned long long, 0> {};
  148. template <typename T> struct hb_int_min<T *> : hb_integral_constant<T *, nullptr> {};
  149. #define hb_int_min(T) hb_int_min<T>::value
  150. template <typename T> struct hb_int_max;
  151. template <> struct hb_int_max<char> : hb_integral_constant<char, CHAR_MAX> {};
  152. template <> struct hb_int_max<signed char> : hb_integral_constant<signed char, SCHAR_MAX> {};
  153. template <> struct hb_int_max<unsigned char> : hb_integral_constant<unsigned char, UCHAR_MAX> {};
  154. template <> struct hb_int_max<signed short> : hb_integral_constant<signed short, SHRT_MAX> {};
  155. template <> struct hb_int_max<unsigned short> : hb_integral_constant<unsigned short, USHRT_MAX> {};
  156. template <> struct hb_int_max<signed int> : hb_integral_constant<signed int, INT_MAX> {};
  157. template <> struct hb_int_max<unsigned int> : hb_integral_constant<unsigned int, UINT_MAX> {};
  158. template <> struct hb_int_max<signed long> : hb_integral_constant<signed long, LONG_MAX> {};
  159. template <> struct hb_int_max<unsigned long> : hb_integral_constant<unsigned long, ULONG_MAX> {};
  160. template <> struct hb_int_max<signed long long> : hb_integral_constant<signed long long, LLONG_MAX> {};
  161. template <> struct hb_int_max<unsigned long long> : hb_integral_constant<unsigned long long, ULLONG_MAX> {};
  162. #define hb_int_max(T) hb_int_max<T>::value
  163. #if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
  164. #define hb_is_trivially_copyable(T) __has_trivial_copy(T)
  165. #define hb_is_trivially_copy_assignable(T) __has_trivial_assign(T)
  166. #define hb_is_trivially_constructible(T) __has_trivial_constructor(T)
  167. #define hb_is_trivially_copy_constructible(T) __has_trivial_copy_constructor(T)
  168. #define hb_is_trivially_destructible(T) __has_trivial_destructor(T)
  169. #else
  170. #define hb_is_trivially_copyable(T) std::is_trivially_copyable<T>::value
  171. #define hb_is_trivially_copy_assignable(T) std::is_trivially_copy_assignable<T>::value
  172. #define hb_is_trivially_constructible(T) std::is_trivially_constructible<T>::value
  173. #define hb_is_trivially_copy_constructible(T) std::is_trivially_copy_constructible<T>::value
  174. #define hb_is_trivially_destructible(T) std::is_trivially_destructible<T>::value
  175. #endif
  176. /* Class traits. */
  177. #define HB_DELETE_COPY_ASSIGN(TypeName) \
  178. TypeName(const TypeName&) = delete; \
  179. void operator=(const TypeName&) = delete
  180. #define HB_DELETE_CREATE_COPY_ASSIGN(TypeName) \
  181. TypeName() = delete; \
  182. TypeName(const TypeName&) = delete; \
  183. void operator=(const TypeName&) = delete
  184. /* hb_unwrap_type (T)
  185. * If T has no T::type, returns T. Otherwise calls itself on T::type recursively.
  186. */
  187. template <typename T, typename>
  188. struct _hb_unwrap_type : hb_type_identity_t<T> {};
  189. template <typename T>
  190. struct _hb_unwrap_type<T, hb_void_t<typename T::type>> : _hb_unwrap_type<typename T::type, void> {};
  191. template <typename T>
  192. using hb_unwrap_type = _hb_unwrap_type<T, void>;
  193. #define hb_unwrap_type(T) typename hb_unwrap_type<T>::type
  194. #endif /* HB_META_HH */