blob: a3bc55e14e26aee875a5736d5357d87554870a99 [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 <stddef.h>
bungeman2bd02852015-08-05 12:09:57 -070018#include <stdint.h>
bungeman221524d2016-01-05 14:59:40 -080019#include <type_traits>
20#include <utility>
bungeman2bd02852015-08-05 12:09:57 -070021
bungeman761cf612015-08-28 07:09:20 -070022namespace skstd {
23
bungeman221524d2016-01-05 14:59:40 -080024template <bool B> using bool_constant = std::integral_constant<bool, B>;
bungemana3434d82015-09-07 12:45:52 -070025
bungeman221524d2016-01-05 14:59:40 -080026template <bool B, typename T, typename F> using conditional_t = typename std::conditional<B, T, F>::type;
27template <bool B, typename T = void> using enable_if_t = typename std::enable_if<B, T>::type;
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000028
bungeman221524d2016-01-05 14:59:40 -080029template <typename T> using remove_const_t = typename std::remove_const<T>::type;
30template <typename T> using remove_volatile_t = typename std::remove_volatile<T>::type;
31template <typename T> using remove_cv_t = typename std::remove_cv<T>::type;
bungeman54e63082016-01-06 08:30:59 -080032template <typename T> using remove_pointer_t = typename std::remove_pointer<T>::type;
bungeman221524d2016-01-05 14:59:40 -080033template <typename T> using remove_reference_t = typename std::remove_reference<T>::type;
34template <typename T> using remove_extent_t = typename std::remove_extent<T>::type;
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000035
bungemana3434d82015-09-07 12:45:52 -070036// template<typename R, typename... Args> struct is_function<
bungeman221524d2016-01-05 14:59:40 -080037// R [calling-convention] (Args...[, ...]) [const] [volatile] [&|&&]> : std::true_type {};
bungemana3434d82015-09-07 12:45:52 -070038// The cv and ref-qualified versions are strange types we're currently avoiding, so not supported.
bungeman0e101662016-01-08 11:05:09 -080039// These aren't supported in msvc either until vs2015u2.
bungemana3434d82015-09-07 12:45:52 -070040// On all platforms, variadic functions only exist in the c calling convention.
bungeman0e101662016-01-08 11:05:09 -080041// mcvc 2013 introduced __vectorcall, but it wan't until 2015 that it was added to is_function.
bungeman221524d2016-01-05 14:59:40 -080042template <typename> struct is_function : std::false_type {};
bungeman0be9e802016-03-18 11:17:56 -070043#if !defined(WIN32)
bungeman221524d2016-01-05 14:59:40 -080044template <typename R, typename... Args> struct is_function<R(Args...)> : std::true_type {};
bungemana3434d82015-09-07 12:45:52 -070045#else
bungeman221524d2016-01-05 14:59:40 -080046template <typename R, typename... Args> struct is_function<R __cdecl (Args...)> : std::true_type {};
cjacekc7211962016-01-25 07:27:36 -080047#if defined(_M_IX86)
bungeman221524d2016-01-05 14:59:40 -080048template <typename R, typename... Args> struct is_function<R __stdcall (Args...)> : std::true_type {};
49template <typename R, typename... Args> struct is_function<R __fastcall (Args...)> : std::true_type {};
lsalzmanc19247f2015-12-01 07:21:09 -080050#endif
bungeman0be9e802016-03-18 11:17:56 -070051#if defined(_MSC_VER) && (_M_IX86_FP >= 2 || defined(_M_AMD64) || defined(_M_X64))
bungeman221524d2016-01-05 14:59:40 -080052template <typename R, typename... Args> struct is_function<R __vectorcall (Args...)> : std::true_type {};
bungemana3434d82015-09-07 12:45:52 -070053#endif
54#endif
bungeman221524d2016-01-05 14:59:40 -080055template <typename R, typename... Args> struct is_function<R(Args..., ...)> : std::true_type {};
bungemana3434d82015-09-07 12:45:52 -070056
bungeman221524d2016-01-05 14:59:40 -080057template <typename T> using add_const_t = typename std::add_const<T>::type;
58template <typename T> using add_volatile_t = typename std::add_volatile<T>::type;
59template <typename T> using add_cv_t = typename std::add_cv<T>::type;
60template <typename T> using add_pointer_t = typename std::add_pointer<T>::type;
bungeman0e101662016-01-08 11:05:09 -080061template <typename T> using add_lvalue_reference_t = typename std::add_lvalue_reference<T>::type;
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000062
bungemanc901c112016-03-08 08:35:23 -080063template <typename... T> using common_type_t = typename std::common_type<T...>::type;
bungeman0be9e802016-03-18 11:17:56 -070064template <typename T> using underlying_type_t = typename std::underlying_type<T>::type;
bungemanc901c112016-03-08 08:35:23 -080065
bungemande029d02016-01-07 12:39:11 -080066template <typename S, typename D,
67 bool=std::is_void<S>::value || is_function<D>::value || std::is_array<D>::value>
bungemana3434d82015-09-07 12:45:52 -070068struct is_convertible_detector {
bungemande029d02016-01-07 12:39:11 -080069 static const/*expr*/ bool value = std::is_void<D>::value;
bungemana3434d82015-09-07 12:45:52 -070070};
71template <typename S, typename D> struct is_convertible_detector<S, D, false> {
72 using yes_type = uint8_t;
73 using no_type = uint16_t;
74
75 template <typename To> static void param_convertable_to(To);
76
77 template <typename From, typename To>
bungeman221524d2016-01-05 14:59:40 -080078 static decltype(param_convertable_to<To>(std::declval<From>()), yes_type()) convertible(int);
bungemana3434d82015-09-07 12:45:52 -070079
80 template <typename, typename> static no_type convertible(...);
81
82 static const/*expr*/ bool value = sizeof(convertible<S, D>(0)) == sizeof(yes_type);
83};
bungeman0e101662016-01-08 11:05:09 -080084// std::is_convertable is known to be broken (not work with incomplete types) in Android clang NDK.
85// This is currently what prevents us from using std::unique_ptr.
bungemana3434d82015-09-07 12:45:52 -070086template<typename S, typename D> struct is_convertible
bungeman221524d2016-01-05 14:59:40 -080087 : bool_constant<is_convertible_detector<S, D>::value> {};
bungemana3434d82015-09-07 12:45:52 -070088
bungeman761cf612015-08-28 07:09:20 -070089} // namespace skstd
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000090
bungeman761cf612015-08-28 07:09:20 -070091// The sknonstd namespace contains things we would like to be proposed and feel std-ish.
92namespace sknonstd {
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000093
bungeman761cf612015-08-28 07:09:20 -070094// The name 'copy' here is fraught with peril. In this case it means 'append', not 'overwrite'.
95// Alternate proposed names are 'propagate', 'augment', or 'append' (and 'add', but already taken).
96// std::experimental::propagate_const already exists for other purposes in TSv2.
97// These also follow the <dest, source> pattern used by boost.
98template <typename D, typename S> struct copy_const {
bungemande029d02016-01-07 12:39:11 -080099 using type = skstd::conditional_t<std::is_const<S>::value, skstd::add_const_t<D>, D>;
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000100};
bungeman761cf612015-08-28 07:09:20 -0700101template <typename D, typename S> using copy_const_t = typename copy_const<D, S>::type;
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000102
bungeman761cf612015-08-28 07:09:20 -0700103template <typename D, typename S> struct copy_volatile {
bungemande029d02016-01-07 12:39:11 -0800104 using type = skstd::conditional_t<std::is_volatile<S>::value, skstd::add_volatile_t<D>, D>;
bungeman761cf612015-08-28 07:09:20 -0700105};
106template <typename D, typename S> using copy_volatile_t = typename copy_volatile<D, S>::type;
107
108template <typename D, typename S> struct copy_cv {
109 using type = copy_volatile_t<copy_const_t<D, S>, S>;
110};
111template <typename D, typename S> using copy_cv_t = typename copy_cv<D, S>::type;
112
113// The name 'same' here means 'overwrite'.
114// Alternate proposed names are 'replace', 'transfer', or 'qualify_from'.
115// same_xxx<D, S> can be written as copy_xxx<remove_xxx_t<D>, S>
116template <typename D, typename S> using same_const = copy_const<skstd::remove_const_t<D>, S>;
117template <typename D, typename S> using same_const_t = typename same_const<D, S>::type;
118template <typename D, typename S> using same_volatile =copy_volatile<skstd::remove_volatile_t<D>,S>;
119template <typename D, typename S> using same_volatile_t = typename same_volatile<D, S>::type;
120template <typename D, typename S> using same_cv = copy_cv<skstd::remove_cv_t<D>, S>;
121template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type;
122
123} // namespace sknonstd
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000124
mtklein449d9b72015-09-28 10:33:02 -0700125// Just a pithier wrapper for enable_if_t.
126#define SK_WHEN(condition, T) skstd::enable_if_t<!!(condition), T>
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000127
bungeman@google.com4b3ef5a2013-07-12 18:31:59 +0000128#endif