blob: c252001a00f1d4992281a5bd4dbcbf7a92c02bff [file] [log] [blame]
bungeman@google.comf5cc5b12013-07-12 18:22:49 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 *
7 *
8 * This header provides some of the helpers (std::integral_constant) and
9 * type transformations (std::conditional) which will become available with
10 * C++11 in the type_traits header.
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000011 */
12
13#ifndef SkTLogic_DEFINED
14#define SkTLogic_DEFINED
15
bungemana3434d82015-09-07 12:45:52 -070016#include "SkTypes.h"
17
18#include <stddef.h>
bungeman2bd02852015-08-05 12:09:57 -070019#include <stdint.h>
20
bungeman761cf612015-08-28 07:09:20 -070021namespace skstd {
22
bungemana3434d82015-09-07 12:45:52 -070023using nullptr_t = decltype(nullptr);
24
bungeman761cf612015-08-28 07:09:20 -070025template <typename T, T v> struct integral_constant {
26 static const/*expr*/ T value = v;
27 using value_type = T;
28 using type = integral_constant<T, v>;
29 //constexpr operator value_type() const noexcept { return value; }
30 //constexpr value_type operator()() const noexcept { return value; }
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000031};
32
bungeman761cf612015-08-28 07:09:20 -070033template <bool B> using bool_constant = integral_constant<bool, B>;
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000034
bungeman761cf612015-08-28 07:09:20 -070035using true_type = bool_constant<true>;
36using false_type = bool_constant<false>;
37
38template <bool B, typename T, typename F> struct conditional { using type = T; };
39template <typename T, typename F> struct conditional<false, T, F> { using type = F; };
40template <bool B, typename T, typename F> using conditional_t = typename conditional<B, T, F>::type;
41
42template <bool B, typename T = void> struct enable_if { using type = T; };
43template <typename T> struct enable_if<false, T> {};
44template <bool B, typename T = void> using enable_if_t = typename enable_if<B, T>::type;
45
46template <typename T> struct remove_const { using type = T; };
47template <typename T> struct remove_const<const T> { using type = T; };
48template <typename T> using remove_const_t = typename remove_const<T>::type;
49
50template <typename T> struct remove_volatile { using type = T; };
51template <typename T> struct remove_volatile<volatile T> { using type = T; };
52template <typename T> using remove_volatile_t = typename remove_volatile<T>::type;
53
54template <typename T> struct remove_cv { using type = remove_volatile_t<remove_const_t<T>>; };
55template <typename T> using remove_cv_t = typename remove_cv<T>::type;
56
57template <typename T> struct remove_reference { using type = T; };
58template <typename T> struct remove_reference<T&> { using type = T; };
59template <typename T> struct remove_reference<T&&> { using type = T; };
60template <typename T> using remove_reference_t = typename remove_reference<T>::type;
61
bungemana3434d82015-09-07 12:45:52 -070062template <typename T> struct remove_extent { using type = T; };
63template <typename T> struct remove_extent<T[]> { using type = T; };
64template <typename T, size_t N> struct remove_extent<T[N]> { using type = T;};
65template <typename T> using remove_extent_t = typename remove_extent<T>::type;
66
bungeman761cf612015-08-28 07:09:20 -070067template <typename T, typename U> struct is_same : false_type {};
68template <typename T> struct is_same<T, T> : true_type {};
69
70template <typename T> struct is_void : is_same<void, remove_cv_t<T>> {};
71
72template <typename T> struct is_const : false_type {};
73template <typename T> struct is_const<const T> : true_type {};
74
75template <typename T> struct is_volatile : false_type {};
76template <typename T> struct is_volatile<volatile T> : true_type {};
77
bungemana3434d82015-09-07 12:45:52 -070078template <typename T> struct is_pointer_detector : false_type {};
79template <typename T> struct is_pointer_detector<T*> : true_type {};
80template <typename T> struct is_pointer : is_pointer_detector<remove_cv_t<T>> {};
81
bungeman761cf612015-08-28 07:09:20 -070082template <typename T> struct is_reference : false_type {};
83template <typename T> struct is_reference<T&> : true_type {};
84template <typename T> struct is_reference<T&&> : true_type {};
85
86template <typename T> struct is_lvalue_reference : false_type {};
87template <typename T> struct is_lvalue_reference<T&> : true_type {};
88
bungemana3434d82015-09-07 12:45:52 -070089template <typename T> struct is_rvalue_reference : false_type {};
90template <typename T> struct is_rvalue_reference<T&&> : true_type {};
91
92template <typename T> struct is_class_detector {
93 using yes_type = uint8_t;
94 using no_type = uint16_t;
95 template <typename U> static yes_type clazz(int U::*);
96 template <typename U> static no_type clazz(...);
97 static const/*expr*/ bool value = sizeof(clazz<T>(0)) == sizeof(yes_type) /*&& !is_union<T>::value*/;
98};
99template <typename T> struct is_class : bool_constant<is_class_detector<T>::value> {};
100
101template <typename T, bool = is_class<T>::value> struct is_empty_detector {
102 struct Derived : public T { char unused; };
103 static const/*expr*/ bool value = sizeof(Derived) == sizeof(char);
104};
105template <typename T> struct is_empty_detector<T, false> {
106 static const/*expr*/ bool value = false;
commit-bot@chromium.org08bf86c2014-05-07 18:01:57 +0000107};
bungeman761cf612015-08-28 07:09:20 -0700108template <typename T> struct is_empty : bool_constant<is_empty_detector<T>::value> {};
commit-bot@chromium.org08bf86c2014-05-07 18:01:57 +0000109
bungemana3434d82015-09-07 12:45:52 -0700110template <typename T> struct is_array : false_type {};
111template <typename T> struct is_array<T[]> : true_type {};
112template <typename T, size_t N> struct is_array<T[N]> : true_type {};
113
114// template<typename R, typename... Args> struct is_function<
115// R [calling-convention] (Args...[, ...]) [const] [volatile] [&|&&]> : true_type {};
116// The cv and ref-qualified versions are strange types we're currently avoiding, so not supported.
117// On all platforms, variadic functions only exist in the c calling convention.
118template <typename> struct is_function : false_type { };
119#if !defined(SK_BUILD_FOR_WIN)
120template <typename R, typename... Args> struct is_function<R(Args...)> : true_type {};
121#else
122#if defined(_M_IX86)
123template <typename R, typename... Args> struct is_function<R __cdecl (Args...)> : true_type {};
124template <typename R, typename... Args> struct is_function<R __stdcall (Args...)> : true_type {};
125template <typename R, typename... Args> struct is_function<R __fastcall (Args...)> : true_type {};
126template <typename R, typename... Args> struct is_function<R __vectorcall (Args...)> : true_type {};
127#else
128template <typename R, typename... Args> struct is_function<R __cdecl (Args...)> : true_type {};
129template <typename R, typename... Args> struct is_function<R __vectorcall (Args...)> : true_type {};
130#endif
131#endif
132template <typename R, typename... Args> struct is_function<R(Args..., ...)> : true_type {};
133
bungeman761cf612015-08-28 07:09:20 -0700134template <typename T> struct add_const { using type = const T; };
135template <typename T> using add_const_t = typename add_const<T>::type;
bungeman@google.comf5cc5b12013-07-12 18:22:49 +0000136
bungeman761cf612015-08-28 07:09:20 -0700137template <typename T> struct add_volatile { using type = volatile T; };
138template <typename T> using add_volatile_t = typename add_volatile<T>::type;
bungeman@google.comf5cc5b12013-07-12 18:22:49 +0000139
bungeman761cf612015-08-28 07:09:20 -0700140template <typename T> struct add_cv { using type = add_volatile_t<add_const_t<T>>; };
141template <typename T> using add_cv_t = typename add_cv<T>::type;
142
bungemana3434d82015-09-07 12:45:52 -0700143template <typename T> struct add_pointer { using type = remove_reference_t<T>*; };
144template <typename T> using add_pointer_t = typename add_pointer<T>::type;
145
146template <typename T, bool=is_void<T>::value> struct add_lvalue_reference_init { using type = T; };
147template <typename T> struct add_lvalue_reference_init<T, false> { using type = T&; };
148template <typename T> struct add_lvalue_reference : add_lvalue_reference_init<T> { };
149template <typename T> using add_lvalue_reference_t = typename add_lvalue_reference<T>::type;
150
151template <typename T, bool=is_void<T>::value> struct add_rvalue_reference_init { using type = T; };
152template <typename T> struct add_rvalue_reference_init<T, false> { using type = T&&; };
153template <typename T> struct add_rvalue_reference : add_rvalue_reference_init<T> {};
bungeman761cf612015-08-28 07:09:20 -0700154template <typename T> using add_rvalue_reference_t = typename add_rvalue_reference<T>::type;
bungeman@google.comf5cc5b12013-07-12 18:22:49 +0000155
bungemana3434d82015-09-07 12:45:52 -0700156/* This is 'just' a forward declaration. */
157template <typename T> add_rvalue_reference_t<T> declval() /*noexcept*/;
158
159template <typename S, typename D, bool=is_void<S>::value||is_function<D>::value||is_array<D>::value>
160struct is_convertible_detector {
161 static const/*expr*/ bool value = is_void<D>::value;
162};
163template <typename S, typename D> struct is_convertible_detector<S, D, false> {
164 using yes_type = uint8_t;
165 using no_type = uint16_t;
166
167 template <typename To> static void param_convertable_to(To);
168
169 template <typename From, typename To>
170 static decltype(param_convertable_to<To>(declval<From>()), yes_type()) convertible(int);
171
172 template <typename, typename> static no_type convertible(...);
173
174 static const/*expr*/ bool value = sizeof(convertible<S, D>(0)) == sizeof(yes_type);
175};
176template<typename S, typename D> struct is_convertible
177 : bool_constant<is_convertible_detector<S, D>::value> { };
178
179template <typename T> struct decay {
180 using U = remove_reference_t<T>;
181 using type = conditional_t<is_array<U>::value,
182 remove_extent_t<U>*,
183 conditional_t<is_function<U>::value, add_pointer_t<U>, remove_cv_t<U>>>;
184};
185template <typename T> using decay_t = typename decay<T>::type;
186
bungeman761cf612015-08-28 07:09:20 -0700187} // namespace skstd
bungeman@google.comf5cc5b12013-07-12 18:22:49 +0000188
bungeman761cf612015-08-28 07:09:20 -0700189// The sknonstd namespace contains things we would like to be proposed and feel std-ish.
190namespace sknonstd {
bungeman@google.comf5cc5b12013-07-12 18:22:49 +0000191
bungeman761cf612015-08-28 07:09:20 -0700192// The name 'copy' here is fraught with peril. In this case it means 'append', not 'overwrite'.
193// Alternate proposed names are 'propagate', 'augment', or 'append' (and 'add', but already taken).
194// std::experimental::propagate_const already exists for other purposes in TSv2.
195// These also follow the <dest, source> pattern used by boost.
196template <typename D, typename S> struct copy_const {
197 using type = skstd::conditional_t<skstd::is_const<S>::value, skstd::add_const_t<D>, D>;
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000198};
bungeman761cf612015-08-28 07:09:20 -0700199template <typename D, typename S> using copy_const_t = typename copy_const<D, S>::type;
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000200
bungeman761cf612015-08-28 07:09:20 -0700201template <typename D, typename S> struct copy_volatile {
202 using type = skstd::conditional_t<skstd::is_volatile<S>::value, skstd::add_volatile_t<D>, D>;
203};
204template <typename D, typename S> using copy_volatile_t = typename copy_volatile<D, S>::type;
205
206template <typename D, typename S> struct copy_cv {
207 using type = copy_volatile_t<copy_const_t<D, S>, S>;
208};
209template <typename D, typename S> using copy_cv_t = typename copy_cv<D, S>::type;
210
211// The name 'same' here means 'overwrite'.
212// Alternate proposed names are 'replace', 'transfer', or 'qualify_from'.
213// same_xxx<D, S> can be written as copy_xxx<remove_xxx_t<D>, S>
214template <typename D, typename S> using same_const = copy_const<skstd::remove_const_t<D>, S>;
215template <typename D, typename S> using same_const_t = typename same_const<D, S>::type;
216template <typename D, typename S> using same_volatile =copy_volatile<skstd::remove_volatile_t<D>,S>;
217template <typename D, typename S> using same_volatile_t = typename same_volatile<D, S>::type;
218template <typename D, typename S> using same_cv = copy_cv<skstd::remove_cv_t<D>, S>;
219template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type;
220
221} // namespace sknonstd
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000222
mtklein449d9b72015-09-28 10:33:02 -0700223// Just a pithier wrapper for enable_if_t.
224#define SK_WHEN(condition, T) skstd::enable_if_t<!!(condition), T>
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000225
bungeman@google.com4b3ef5a2013-07-12 18:31:59 +0000226#endif