2 * Copyright © 2018 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
33 #include <type_traits>
38 * C++ template meta-programming & fundamentals used with them.
41 /* Void! For when we need a expression-type of void. */
44 /* https://en.cppreference.com/w/cpp/types/void_t */
45 template<typename... Ts> struct _hb_void_t { typedef void type; };
46 template<typename... Ts> using hb_void_t = typename _hb_void_t<Ts...>::type;
48 template<typename Head, typename... Ts> struct _hb_head_t { typedef Head type; };
49 template<typename... Ts> using hb_head_t = typename _hb_head_t<Ts...>::type;
51 template <typename T, T v> struct hb_integral_constant { static constexpr T value = v; };
52 template <bool b> using hb_bool_constant = hb_integral_constant<bool, b>;
53 using hb_true_type = hb_bool_constant<true>;
54 using hb_false_type = hb_bool_constant<false>;
56 /* Static-assert as expression. */
57 template <bool cond> struct static_assert_expr;
58 template <> struct static_assert_expr<true> : hb_false_type {};
59 #define static_assert_expr(C) static_assert_expr<C>::value
61 /* Basic type SFINAE. */
63 template <bool B, typename T = void> struct hb_enable_if {};
64 template <typename T> struct hb_enable_if<true, T> { typedef T type; };
65 #define hb_enable_if(Cond) typename hb_enable_if<(Cond)>::type* = nullptr
66 /* Concepts/Requires alias: */
67 #define hb_requires(Cond) hb_enable_if((Cond))
69 template <typename T, typename T2> struct hb_is_same : hb_false_type {};
70 template <typename T> struct hb_is_same<T, T> : hb_true_type {};
71 #define hb_is_same(T, T2) hb_is_same<T, T2>::value
73 /* Function overloading SFINAE and priority. */
75 #define HB_RETURN(Ret, E) -> hb_head_t<Ret, decltype ((E))> { return (E); }
76 #define HB_AUTO_RETURN(E) -> decltype ((E)) { return (E); }
77 #define HB_VOID_RETURN(E) -> hb_void_t<decltype ((E))> { (E); }
79 template <unsigned Pri> struct hb_priority : hb_priority<Pri - 1> {};
80 template <> struct hb_priority<0> {};
81 #define hb_prioritize hb_priority<16> ()
83 #define HB_FUNCOBJ(x) static_const x HB_UNUSED
86 template <typename T> struct hb_type_identity_t { typedef T type; };
87 template <typename T> using hb_type_identity = typename hb_type_identity_t<T>::type;
89 template <typename T> static inline T hb_declval ();
90 #define hb_declval(T) (hb_declval<T> ())
92 template <typename T> struct hb_match_const : hb_type_identity_t<T>, hb_false_type {};
93 template <typename T> struct hb_match_const<const T> : hb_type_identity_t<T>, hb_true_type {};
94 template <typename T> using hb_remove_const = typename hb_match_const<T>::type;
96 template <typename T> struct hb_match_reference : hb_type_identity_t<T>, hb_false_type {};
97 template <typename T> struct hb_match_reference<T &> : hb_type_identity_t<T>, hb_true_type {};
98 template <typename T> struct hb_match_reference<T &&> : hb_type_identity_t<T>, hb_true_type {};
99 template <typename T> using hb_remove_reference = typename hb_match_reference<T>::type;
100 template <typename T> auto _hb_try_add_lvalue_reference (hb_priority<1>) -> hb_type_identity<T&>;
101 template <typename T> auto _hb_try_add_lvalue_reference (hb_priority<0>) -> hb_type_identity<T>;
102 template <typename T> using hb_add_lvalue_reference = decltype (_hb_try_add_lvalue_reference<T> (hb_prioritize));
103 template <typename T> auto _hb_try_add_rvalue_reference (hb_priority<1>) -> hb_type_identity<T&&>;
104 template <typename T> auto _hb_try_add_rvalue_reference (hb_priority<0>) -> hb_type_identity<T>;
105 template <typename T> using hb_add_rvalue_reference = decltype (_hb_try_add_rvalue_reference<T> (hb_prioritize));
107 template <typename T> struct hb_match_pointer : hb_type_identity_t<T>, hb_false_type {};
108 template <typename T> struct hb_match_pointer<T *> : hb_type_identity_t<T>, hb_true_type {};
109 template <typename T> using hb_remove_pointer = typename hb_match_pointer<T>::type;
110 template <typename T> auto _hb_try_add_pointer (hb_priority<1>) -> hb_type_identity<hb_remove_reference<T>*>;
111 template <typename T> auto _hb_try_add_pointer (hb_priority<1>) -> hb_type_identity<T>;
112 template <typename T> using hb_add_pointer = decltype (_hb_try_add_pointer<T> (hb_prioritize));
115 /* TODO Add feature-parity to std::decay. */
116 template <typename T> using hb_decay = hb_remove_const<hb_remove_reference<T>>;
118 #define hb_is_convertible(From,To) std::is_convertible<From, To>::value
120 template <typename From, typename To>
121 using hb_is_cr_convertible = hb_bool_constant<
122 hb_is_same (hb_decay<From>, hb_decay<To>) &&
123 (!std::is_const<From>::value || std::is_const<To>::value) &&
124 (!std::is_reference<To>::value || std::is_const<To>::value || std::is_reference<To>::value)
126 #define hb_is_cr_convertible(From,To) hb_is_cr_convertible<From, To>::value
131 template <typename T> constexpr auto
132 operator () (T&& v) const HB_AUTO_RETURN (std::forward<T> (v))
134 template <typename T> constexpr auto
135 operator () (T *v) const HB_AUTO_RETURN (*v)
137 HB_FUNCOBJ (hb_deref);
139 template <typename T>
140 struct hb_reference_wrapper
142 hb_reference_wrapper (T v) : v (v) {}
143 bool operator == (const hb_reference_wrapper& o) const { return v == o.v; }
144 bool operator != (const hb_reference_wrapper& o) const { return v != o.v; }
145 operator T () const { return v; }
146 T get () const { return v; }
149 template <typename T>
150 struct hb_reference_wrapper<T&>
152 hb_reference_wrapper (T& v) : v (std::addressof (v)) {}
153 bool operator == (const hb_reference_wrapper& o) const { return v == o.v; }
154 bool operator != (const hb_reference_wrapper& o) const { return v != o.v; }
155 operator T& () const { return *v; }
156 T& get () const { return *v; }
163 template <typename T> struct hb_int_min;
164 template <> struct hb_int_min<char> : hb_integral_constant<char, CHAR_MIN> {};
165 template <> struct hb_int_min<signed char> : hb_integral_constant<signed char, SCHAR_MIN> {};
166 template <> struct hb_int_min<unsigned char> : hb_integral_constant<unsigned char, 0> {};
167 template <> struct hb_int_min<signed short> : hb_integral_constant<signed short, SHRT_MIN> {};
168 template <> struct hb_int_min<unsigned short> : hb_integral_constant<unsigned short, 0> {};
169 template <> struct hb_int_min<signed int> : hb_integral_constant<signed int, INT_MIN> {};
170 template <> struct hb_int_min<unsigned int> : hb_integral_constant<unsigned int, 0> {};
171 template <> struct hb_int_min<signed long> : hb_integral_constant<signed long, LONG_MIN> {};
172 template <> struct hb_int_min<unsigned long> : hb_integral_constant<unsigned long, 0> {};
173 template <> struct hb_int_min<signed long long> : hb_integral_constant<signed long long, LLONG_MIN> {};
174 template <> struct hb_int_min<unsigned long long> : hb_integral_constant<unsigned long long, 0> {};
175 template <typename T> struct hb_int_min<T *> : hb_integral_constant<T *, nullptr> {};
176 #define hb_int_min(T) hb_int_min<T>::value
177 template <typename T> struct hb_int_max;
178 template <> struct hb_int_max<char> : hb_integral_constant<char, CHAR_MAX> {};
179 template <> struct hb_int_max<signed char> : hb_integral_constant<signed char, SCHAR_MAX> {};
180 template <> struct hb_int_max<unsigned char> : hb_integral_constant<unsigned char, UCHAR_MAX> {};
181 template <> struct hb_int_max<signed short> : hb_integral_constant<signed short, SHRT_MAX> {};
182 template <> struct hb_int_max<unsigned short> : hb_integral_constant<unsigned short, USHRT_MAX> {};
183 template <> struct hb_int_max<signed int> : hb_integral_constant<signed int, INT_MAX> {};
184 template <> struct hb_int_max<unsigned int> : hb_integral_constant<unsigned int, UINT_MAX> {};
185 template <> struct hb_int_max<signed long> : hb_integral_constant<signed long, LONG_MAX> {};
186 template <> struct hb_int_max<unsigned long> : hb_integral_constant<unsigned long, ULONG_MAX> {};
187 template <> struct hb_int_max<signed long long> : hb_integral_constant<signed long long, LLONG_MAX> {};
188 template <> struct hb_int_max<unsigned long long> : hb_integral_constant<unsigned long long, ULLONG_MAX> {};
189 #define hb_int_max(T) hb_int_max<T>::value
194 #define HB_DELETE_COPY_ASSIGN(TypeName) \
195 TypeName(const TypeName&) = delete; \
196 void operator=(const TypeName&) = delete
197 #define HB_DELETE_CREATE_COPY_ASSIGN(TypeName) \
198 TypeName() = delete; \
199 TypeName(const TypeName&) = delete; \
200 void operator=(const TypeName&) = delete
202 /* hb_unwrap_type (T)
203 * If T has no T::type, returns T. Otherwise calls itself on T::type recursively.
206 template <typename T, typename>
207 struct _hb_unwrap_type : hb_type_identity_t<T> {};
208 template <typename T>
209 struct _hb_unwrap_type<T, hb_void_t<typename T::type>> : _hb_unwrap_type<typename T::type, void> {};
210 template <typename T>
211 using hb_unwrap_type = _hb_unwrap_type<T, void>;
212 #define hb_unwrap_type(T) typename hb_unwrap_type<T>::type
214 #endif /* HB_META_HH */