• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <type_traits>
33 #include <utility>
34 
35 
36 /*
37  * C++ template meta-programming & fundamentals used with them.
38  */
39 
40 /* Void!  For when we need a expression-type of void. */
41 struct hb_empty_t {};
42 
43 /* https://en.cppreference.com/w/cpp/types/void_t */
44 template<typename... Ts> struct _hb_void_t { typedef void type; };
45 template<typename... Ts> using hb_void_t = typename _hb_void_t<Ts...>::type;
46 
47 template<typename Head, typename... Ts> struct _hb_head_t { typedef Head type; };
48 template<typename... Ts> using hb_head_t = typename _hb_head_t<Ts...>::type;
49 
50 template <typename T, T v> struct hb_integral_constant { static constexpr T value = v; };
51 template <bool b> using hb_bool_constant = hb_integral_constant<bool, b>;
52 using hb_true_type = hb_bool_constant<true>;
53 using hb_false_type = hb_bool_constant<false>;
54 
55 /* Static-assert as expression. */
56 template <bool cond> struct static_assert_expr;
57 template <> struct static_assert_expr<true> : hb_false_type {};
58 #define static_assert_expr(C) static_assert_expr<C>::value
59 
60 /* Basic type SFINAE. */
61 
62 template <bool B, typename T = void> struct hb_enable_if {};
63 template <typename T>                struct hb_enable_if<true, T> { typedef T type; };
64 #define hb_enable_if(Cond) typename hb_enable_if<(Cond)>::type* = nullptr
65 /* Concepts/Requires alias: */
66 #define hb_requires(Cond) hb_enable_if((Cond))
67 
68 template <typename T, typename T2> struct hb_is_same : hb_false_type {};
69 template <typename T>              struct hb_is_same<T, T> : hb_true_type {};
70 #define hb_is_same(T, T2) hb_is_same<T, T2>::value
71 
72 /* Function overloading SFINAE and priority. */
73 
74 #define HB_RETURN(Ret, E) -> hb_head_t<Ret, decltype ((E))> { return (E); }
75 #define HB_AUTO_RETURN(E) -> decltype ((E)) { return (E); }
76 #define HB_VOID_RETURN(E) -> hb_void_t<decltype ((E))> { (E); }
77 
78 template <unsigned Pri> struct hb_priority : hb_priority<Pri - 1> {};
79 template <>             struct hb_priority<0> {};
80 #define hb_prioritize hb_priority<16> ()
81 
82 #define HB_FUNCOBJ(x) static_const x HB_UNUSED
83 
84 
85 template <typename T> struct hb_type_identity_t { typedef T type; };
86 template <typename T> using hb_type_identity = typename hb_type_identity_t<T>::type;
87 
88 struct
89 {
90   template <typename T> constexpr T*
operator ()__anon68fac5d2010891   operator () (T& arg) const
92   {
93 #pragma GCC diagnostic push
94 #pragma GCC diagnostic ignored "-Wcast-align"
95     /* https://en.cppreference.com/w/cpp/memory/addressof */
96     return reinterpret_cast<T*> (
97 	     &const_cast<char&> (
98 		reinterpret_cast<const volatile char&> (arg)));
99 #pragma GCC diagnostic pop
100   }
101 }
102 HB_FUNCOBJ (hb_addressof);
103 
104 template <typename T> static inline T hb_declval ();
105 #define hb_declval(T) (hb_declval<T> ())
106 
107 template <typename T> struct hb_match_const		: hb_type_identity_t<T>, hb_false_type	{};
108 template <typename T> struct hb_match_const<const T>	: hb_type_identity_t<T>, hb_true_type	{};
109 template <typename T> using hb_remove_const = typename hb_match_const<T>::type;
110 template <typename T> using hb_add_const = const T;
111 #define hb_is_const(T) hb_match_const<T>::value
112 template <typename T> struct hb_match_reference		: hb_type_identity_t<T>, hb_false_type	{};
113 template <typename T> struct hb_match_reference<T &>	: hb_type_identity_t<T>, hb_true_type	{};
114 template <typename T> struct hb_match_reference<T &&>	: hb_type_identity_t<T>, hb_true_type	{};
115 template <typename T> using hb_remove_reference = typename hb_match_reference<T>::type;
116 template <typename T> auto _hb_try_add_lvalue_reference (hb_priority<1>) -> hb_type_identity<T&>;
117 template <typename T> auto _hb_try_add_lvalue_reference (hb_priority<0>) -> hb_type_identity<T>;
118 template <typename T> using hb_add_lvalue_reference = decltype (_hb_try_add_lvalue_reference<T> (hb_prioritize));
119 template <typename T> auto _hb_try_add_rvalue_reference (hb_priority<1>) -> hb_type_identity<T&&>;
120 template <typename T> auto _hb_try_add_rvalue_reference (hb_priority<0>) -> hb_type_identity<T>;
121 template <typename T> using hb_add_rvalue_reference = decltype (_hb_try_add_rvalue_reference<T> (hb_prioritize));
122 #define hb_is_reference(T) hb_match_reference<T>::value
123 template <typename T> struct hb_match_pointer		: hb_type_identity_t<T>, hb_false_type	{};
124 template <typename T> struct hb_match_pointer<T *>	: hb_type_identity_t<T>, hb_true_type	{};
125 template <typename T> using hb_remove_pointer = typename hb_match_pointer<T>::type;
126 template <typename T> auto _hb_try_add_pointer (hb_priority<1>) -> hb_type_identity<hb_remove_reference<T>*>;
127 template <typename T> auto _hb_try_add_pointer (hb_priority<1>) -> hb_type_identity<T>;
128 template <typename T> using hb_add_pointer = decltype (_hb_try_add_pointer<T> (hb_prioritize));
129 #define hb_is_pointer(T) hb_match_pointer<T>::value
130 
131 
132 /* TODO Add feature-parity to std::decay. */
133 template <typename T> using hb_decay = hb_remove_const<hb_remove_reference<T>>;
134 
135 #define hb_is_convertible(From,To) std::is_convertible<From, To>::value
136 
137 template <typename From, typename To>
138 using hb_is_cr_convertible = hb_bool_constant<
139   hb_is_same (hb_decay<From>, hb_decay<To>) &&
140   (!hb_is_const (From) || hb_is_const (To)) &&
141   (!hb_is_reference (To) || hb_is_const (To) || hb_is_reference (To))
142 >;
143 #define hb_is_cr_convertible(From,To) hb_is_cr_convertible<From, To>::value
144 
145 
146 struct
147 {
148   template <typename T> constexpr auto
149   operator () (T&& v) const HB_AUTO_RETURN (std::forward<T> (v))
150 
151   template <typename T> constexpr auto
152   operator () (T *v) const HB_AUTO_RETURN (*v)
153 }
154 HB_FUNCOBJ (hb_deref);
155 
156 struct
157 {
158   template <typename T> constexpr auto
159   operator () (T&& v) const HB_AUTO_RETURN (std::forward<T> (v))
160 
161   template <typename T> constexpr auto
162   operator () (T& v) const HB_AUTO_RETURN (hb_addressof (v))
163 }
164 HB_FUNCOBJ (hb_ref);
165 
166 template <typename T>
167 struct hb_reference_wrapper
168 {
hb_reference_wrapperhb_reference_wrapper169   hb_reference_wrapper (T v) : v (v) {}
operator ==hb_reference_wrapper170   bool operator == (const hb_reference_wrapper& o) const { return v == o.v; }
operator !=hb_reference_wrapper171   bool operator != (const hb_reference_wrapper& o) const { return v != o.v; }
operator Thb_reference_wrapper172   operator T () const { return v; }
gethb_reference_wrapper173   T get () const { return v; }
174   T v;
175 };
176 template <typename T>
177 struct hb_reference_wrapper<T&>
178 {
hb_reference_wrapperhb_reference_wrapper179   hb_reference_wrapper (T& v) : v (hb_addressof (v)) {}
operator ==hb_reference_wrapper180   bool operator == (const hb_reference_wrapper& o) const { return v == o.v; }
operator !=hb_reference_wrapper181   bool operator != (const hb_reference_wrapper& o) const { return v != o.v; }
operator T&hb_reference_wrapper182   operator T& () const { return *v; }
gethb_reference_wrapper183   T& get () const { return *v; }
184   T* v;
185 };
186 
187 
188 /* Type traits */
189 
190 template <typename T> struct hb_int_min;
191 template <> struct hb_int_min<char>			: hb_integral_constant<char,			CHAR_MIN>	{};
192 template <> struct hb_int_min<signed char>		: hb_integral_constant<signed char,		SCHAR_MIN>	{};
193 template <> struct hb_int_min<unsigned char>		: hb_integral_constant<unsigned char,		0>		{};
194 template <> struct hb_int_min<signed short>		: hb_integral_constant<signed short,		SHRT_MIN>	{};
195 template <> struct hb_int_min<unsigned short>		: hb_integral_constant<unsigned short,		0>		{};
196 template <> struct hb_int_min<signed int>		: hb_integral_constant<signed int,		INT_MIN>	{};
197 template <> struct hb_int_min<unsigned int>		: hb_integral_constant<unsigned int,		0>		{};
198 template <> struct hb_int_min<signed long>		: hb_integral_constant<signed long,		LONG_MIN>	{};
199 template <> struct hb_int_min<unsigned long>		: hb_integral_constant<unsigned long,		0>		{};
200 template <> struct hb_int_min<signed long long>		: hb_integral_constant<signed long long,	LLONG_MIN>	{};
201 template <> struct hb_int_min<unsigned long long>	: hb_integral_constant<unsigned long long,	0>		{};
202 template <typename T> struct hb_int_min<T *>		: hb_integral_constant<T *,			nullptr>	{};
203 #define hb_int_min(T) hb_int_min<T>::value
204 template <typename T> struct hb_int_max;
205 template <> struct hb_int_max<char>			: hb_integral_constant<char,			CHAR_MAX>	{};
206 template <> struct hb_int_max<signed char>		: hb_integral_constant<signed char,		SCHAR_MAX>	{};
207 template <> struct hb_int_max<unsigned char>		: hb_integral_constant<unsigned char,		UCHAR_MAX>	{};
208 template <> struct hb_int_max<signed short>		: hb_integral_constant<signed short,		SHRT_MAX>	{};
209 template <> struct hb_int_max<unsigned short>		: hb_integral_constant<unsigned short,		USHRT_MAX>	{};
210 template <> struct hb_int_max<signed int>		: hb_integral_constant<signed int,		INT_MAX>	{};
211 template <> struct hb_int_max<unsigned int>		: hb_integral_constant<unsigned int,		UINT_MAX>	{};
212 template <> struct hb_int_max<signed long>		: hb_integral_constant<signed long,		LONG_MAX>	{};
213 template <> struct hb_int_max<unsigned long>		: hb_integral_constant<unsigned long,		ULONG_MAX>	{};
214 template <> struct hb_int_max<signed long long>		: hb_integral_constant<signed long long,	LLONG_MAX>	{};
215 template <> struct hb_int_max<unsigned long long>	: hb_integral_constant<unsigned long long,	ULLONG_MAX>	{};
216 #define hb_int_max(T) hb_int_max<T>::value
217 
218 
219 /* Class traits. */
220 
221 #define HB_DELETE_COPY_ASSIGN(TypeName) \
222   TypeName(const TypeName&) = delete; \
223   void operator=(const TypeName&) = delete
224 #define HB_DELETE_CREATE_COPY_ASSIGN(TypeName) \
225   TypeName() = delete; \
226   TypeName(const TypeName&) = delete; \
227   void operator=(const TypeName&) = delete
228 
229 /* hb_unwrap_type (T)
230  * If T has no T::type, returns T. Otherwise calls itself on T::type recursively.
231  */
232 
233 template <typename T, typename>
234 struct _hb_unwrap_type : hb_type_identity_t<T> {};
235 template <typename T>
236 struct _hb_unwrap_type<T, hb_void_t<typename T::type>> : _hb_unwrap_type<typename T::type, void> {};
237 template <typename T>
238 using hb_unwrap_type = _hb_unwrap_type<T, void>;
239 #define hb_unwrap_type(T) typename hb_unwrap_type<T>::type
240 
241 #endif /* HB_META_HH */
242