blob: 8d07ba1d38d5e526a905a93565637df8c04f6aa8 [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 *
bungeman221524d2016-01-05 14:59:40 -08008 * This header provides some of the helpers (like std::enable_if_t) which will
9 * become available with C++14 in the type_traits header (in the skstd
10 * namespace). This header also provides several Skia specific additions such
11 * as SK_WHEN and the sknonstd namespace.
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000012 */
13
14#ifndef SkTLogic_DEFINED
15#define SkTLogic_DEFINED
16
bungemana3434d82015-09-07 12:45:52 -070017#include "SkTypes.h"
18
19#include <stddef.h>
bungeman2bd02852015-08-05 12:09:57 -070020#include <stdint.h>
bungeman221524d2016-01-05 14:59:40 -080021#include <type_traits>
22#include <utility>
bungeman2bd02852015-08-05 12:09:57 -070023
bungeman761cf612015-08-28 07:09:20 -070024namespace skstd {
25
bungeman221524d2016-01-05 14:59:40 -080026template <bool B> using bool_constant = std::integral_constant<bool, B>;
bungemana3434d82015-09-07 12:45:52 -070027
bungeman221524d2016-01-05 14:59:40 -080028template <bool B, typename T, typename F> using conditional_t = typename std::conditional<B, T, F>::type;
29template <bool B, typename T = void> using enable_if_t = typename std::enable_if<B, T>::type;
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000030
bungeman221524d2016-01-05 14:59:40 -080031template <typename T> using remove_const_t = typename std::remove_const<T>::type;
32template <typename T> using remove_volatile_t = typename std::remove_volatile<T>::type;
33template <typename T> using remove_cv_t = typename std::remove_cv<T>::type;
34template <typename T> using remove_reference_t = typename std::remove_reference<T>::type;
35template <typename T> using remove_extent_t = typename std::remove_extent<T>::type;
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000036
bungeman221524d2016-01-05 14:59:40 -080037template <typename T, typename U> struct is_same : std::false_type {};
38template <typename T> struct is_same<T, T> : std::true_type {};
bungeman761cf612015-08-28 07:09:20 -070039
40template <typename T> struct is_void : is_same<void, remove_cv_t<T>> {};
41
bungeman221524d2016-01-05 14:59:40 -080042template <typename T> struct is_const : std::false_type {};
43template <typename T> struct is_const<const T> : std::true_type {};
bungeman761cf612015-08-28 07:09:20 -070044
bungeman221524d2016-01-05 14:59:40 -080045template <typename T> struct is_volatile : std::false_type {};
46template <typename T> struct is_volatile<volatile T> : std::true_type {};
bungeman761cf612015-08-28 07:09:20 -070047
bungeman221524d2016-01-05 14:59:40 -080048template <typename T> struct is_pointer_detector : std::false_type {};
49template <typename T> struct is_pointer_detector<T*> : std::true_type {};
bungemana3434d82015-09-07 12:45:52 -070050template <typename T> struct is_pointer : is_pointer_detector<remove_cv_t<T>> {};
51
bungeman221524d2016-01-05 14:59:40 -080052template <typename T> struct is_reference : std::false_type {};
53template <typename T> struct is_reference<T&> : std::true_type {};
54template <typename T> struct is_reference<T&&> : std::true_type {};
bungeman761cf612015-08-28 07:09:20 -070055
bungeman221524d2016-01-05 14:59:40 -080056template <typename T> struct is_lvalue_reference : std::false_type {};
57template <typename T> struct is_lvalue_reference<T&> : std::true_type {};
bungeman761cf612015-08-28 07:09:20 -070058
bungeman221524d2016-01-05 14:59:40 -080059template <typename T> struct is_rvalue_reference : std::false_type {};
60template <typename T> struct is_rvalue_reference<T&&> : std::true_type {};
bungemana3434d82015-09-07 12:45:52 -070061
62template <typename T> struct is_class_detector {
63 using yes_type = uint8_t;
64 using no_type = uint16_t;
65 template <typename U> static yes_type clazz(int U::*);
66 template <typename U> static no_type clazz(...);
67 static const/*expr*/ bool value = sizeof(clazz<T>(0)) == sizeof(yes_type) /*&& !is_union<T>::value*/;
68};
69template <typename T> struct is_class : bool_constant<is_class_detector<T>::value> {};
70
71template <typename T, bool = is_class<T>::value> struct is_empty_detector {
72 struct Derived : public T { char unused; };
73 static const/*expr*/ bool value = sizeof(Derived) == sizeof(char);
74};
75template <typename T> struct is_empty_detector<T, false> {
76 static const/*expr*/ bool value = false;
commit-bot@chromium.org08bf86c2014-05-07 18:01:57 +000077};
bungeman761cf612015-08-28 07:09:20 -070078template <typename T> struct is_empty : bool_constant<is_empty_detector<T>::value> {};
commit-bot@chromium.org08bf86c2014-05-07 18:01:57 +000079
bungeman221524d2016-01-05 14:59:40 -080080template <typename T> struct is_array : std::false_type {};
81template <typename T> struct is_array<T[]> : std::true_type {};
82template <typename T, size_t N> struct is_array<T[N]> : std::true_type {};
bungemana3434d82015-09-07 12:45:52 -070083
84// template<typename R, typename... Args> struct is_function<
bungeman221524d2016-01-05 14:59:40 -080085// R [calling-convention] (Args...[, ...]) [const] [volatile] [&|&&]> : std::true_type {};
bungemana3434d82015-09-07 12:45:52 -070086// The cv and ref-qualified versions are strange types we're currently avoiding, so not supported.
87// On all platforms, variadic functions only exist in the c calling convention.
bungeman221524d2016-01-05 14:59:40 -080088template <typename> struct is_function : std::false_type {};
bungemana3434d82015-09-07 12:45:52 -070089#if !defined(SK_BUILD_FOR_WIN)
bungeman221524d2016-01-05 14:59:40 -080090template <typename R, typename... Args> struct is_function<R(Args...)> : std::true_type {};
bungemana3434d82015-09-07 12:45:52 -070091#else
92#if defined(_M_IX86)
bungeman221524d2016-01-05 14:59:40 -080093template <typename R, typename... Args> struct is_function<R __cdecl (Args...)> : std::true_type {};
94template <typename R, typename... Args> struct is_function<R __stdcall (Args...)> : std::true_type {};
95template <typename R, typename... Args> struct is_function<R __fastcall (Args...)> : std::true_type {};
lsalzmanc19247f2015-12-01 07:21:09 -080096#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
bungeman221524d2016-01-05 14:59:40 -080097template <typename R, typename... Args> struct is_function<R __vectorcall (Args...)> : std::true_type {};
lsalzmanc19247f2015-12-01 07:21:09 -080098#endif
bungemana3434d82015-09-07 12:45:52 -070099#else
bungeman221524d2016-01-05 14:59:40 -0800100template <typename R, typename... Args> struct is_function<R __cdecl (Args...)> : std::true_type {};
101template <typename R, typename... Args> struct is_function<R __vectorcall (Args...)> : std::true_type {};
bungemana3434d82015-09-07 12:45:52 -0700102#endif
103#endif
bungeman221524d2016-01-05 14:59:40 -0800104template <typename R, typename... Args> struct is_function<R(Args..., ...)> : std::true_type {};
bungemana3434d82015-09-07 12:45:52 -0700105
bungeman221524d2016-01-05 14:59:40 -0800106template <typename T> using add_const_t = typename std::add_const<T>::type;
107template <typename T> using add_volatile_t = typename std::add_volatile<T>::type;
108template <typename T> using add_cv_t = typename std::add_cv<T>::type;
109template <typename T> using add_pointer_t = typename std::add_pointer<T>::type;
bungemana3434d82015-09-07 12:45:52 -0700110
111template <typename T, bool=is_void<T>::value> struct add_lvalue_reference_init { using type = T; };
112template <typename T> struct add_lvalue_reference_init<T, false> { using type = T&; };
bungeman221524d2016-01-05 14:59:40 -0800113template <typename T> struct add_lvalue_reference : add_lvalue_reference_init<T> {};
bungemana3434d82015-09-07 12:45:52 -0700114template <typename T> using add_lvalue_reference_t = typename add_lvalue_reference<T>::type;
115
116template <typename T, bool=is_void<T>::value> struct add_rvalue_reference_init { using type = T; };
117template <typename T> struct add_rvalue_reference_init<T, false> { using type = T&&; };
118template <typename T> struct add_rvalue_reference : add_rvalue_reference_init<T> {};
bungeman761cf612015-08-28 07:09:20 -0700119template <typename T> using add_rvalue_reference_t = typename add_rvalue_reference<T>::type;
bungeman@google.comf5cc5b12013-07-12 18:22:49 +0000120
bungemana3434d82015-09-07 12:45:52 -0700121template <typename S, typename D, bool=is_void<S>::value||is_function<D>::value||is_array<D>::value>
122struct is_convertible_detector {
123 static const/*expr*/ bool value = is_void<D>::value;
124};
125template <typename S, typename D> struct is_convertible_detector<S, D, false> {
126 using yes_type = uint8_t;
127 using no_type = uint16_t;
128
129 template <typename To> static void param_convertable_to(To);
130
131 template <typename From, typename To>
bungeman221524d2016-01-05 14:59:40 -0800132 static decltype(param_convertable_to<To>(std::declval<From>()), yes_type()) convertible(int);
bungemana3434d82015-09-07 12:45:52 -0700133
134 template <typename, typename> static no_type convertible(...);
135
136 static const/*expr*/ bool value = sizeof(convertible<S, D>(0)) == sizeof(yes_type);
137};
138template<typename S, typename D> struct is_convertible
bungeman221524d2016-01-05 14:59:40 -0800139 : bool_constant<is_convertible_detector<S, D>::value> {};
bungemana3434d82015-09-07 12:45:52 -0700140
141template <typename T> struct decay {
142 using U = remove_reference_t<T>;
143 using type = conditional_t<is_array<U>::value,
144 remove_extent_t<U>*,
145 conditional_t<is_function<U>::value, add_pointer_t<U>, remove_cv_t<U>>>;
146};
147template <typename T> using decay_t = typename decay<T>::type;
148
bungeman761cf612015-08-28 07:09:20 -0700149} // namespace skstd
bungeman@google.comf5cc5b12013-07-12 18:22:49 +0000150
bungeman761cf612015-08-28 07:09:20 -0700151// The sknonstd namespace contains things we would like to be proposed and feel std-ish.
152namespace sknonstd {
bungeman@google.comf5cc5b12013-07-12 18:22:49 +0000153
bungeman761cf612015-08-28 07:09:20 -0700154// The name 'copy' here is fraught with peril. In this case it means 'append', not 'overwrite'.
155// Alternate proposed names are 'propagate', 'augment', or 'append' (and 'add', but already taken).
156// std::experimental::propagate_const already exists for other purposes in TSv2.
157// These also follow the <dest, source> pattern used by boost.
158template <typename D, typename S> struct copy_const {
159 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 +0000160};
bungeman761cf612015-08-28 07:09:20 -0700161template <typename D, typename S> using copy_const_t = typename copy_const<D, S>::type;
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000162
bungeman761cf612015-08-28 07:09:20 -0700163template <typename D, typename S> struct copy_volatile {
164 using type = skstd::conditional_t<skstd::is_volatile<S>::value, skstd::add_volatile_t<D>, D>;
165};
166template <typename D, typename S> using copy_volatile_t = typename copy_volatile<D, S>::type;
167
168template <typename D, typename S> struct copy_cv {
169 using type = copy_volatile_t<copy_const_t<D, S>, S>;
170};
171template <typename D, typename S> using copy_cv_t = typename copy_cv<D, S>::type;
172
173// The name 'same' here means 'overwrite'.
174// Alternate proposed names are 'replace', 'transfer', or 'qualify_from'.
175// same_xxx<D, S> can be written as copy_xxx<remove_xxx_t<D>, S>
176template <typename D, typename S> using same_const = copy_const<skstd::remove_const_t<D>, S>;
177template <typename D, typename S> using same_const_t = typename same_const<D, S>::type;
178template <typename D, typename S> using same_volatile =copy_volatile<skstd::remove_volatile_t<D>,S>;
179template <typename D, typename S> using same_volatile_t = typename same_volatile<D, S>::type;
180template <typename D, typename S> using same_cv = copy_cv<skstd::remove_cv_t<D>, S>;
181template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type;
182
183} // namespace sknonstd
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000184
mtklein449d9b72015-09-28 10:33:02 -0700185// Just a pithier wrapper for enable_if_t.
186#define SK_WHEN(condition, T) skstd::enable_if_t<!!(condition), T>
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000187
bungeman@google.com4b3ef5a2013-07-12 18:31:59 +0000188#endif