blob: f93f63b1a2eb16fd6ad713843cdbf4946dff8fc9 [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.
11 *
12 * Because we lack constexpr, we cannot mimic
13 * std::integral_constant::'constexpr operator T()'.
14 * As a result we introduce SkTBool and SkTIf similar to Boost in order to
15 * minimize the visual noise of many uses of '::value'.
16 */
17
18#ifndef SkTLogic_DEFINED
19#define SkTLogic_DEFINED
20
bungeman2bd02852015-08-05 12:09:57 -070021#include <stdint.h>
22
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000023/** Represents a templated integer constant.
24 * Pre-C++11 version of std::integral_constant.
25 */
26template <typename T, T v> struct SkTIntegralConstant {
27 static const T value = v;
28 typedef T value_type;
29 typedef SkTIntegralConstant<T, v> type;
30};
31
32/** Convenience specialization of SkTIntegralConstant. */
33template <bool b> struct SkTBool : SkTIntegralConstant<bool, b> { };
34
commit-bot@chromium.org08bf86c2014-05-07 18:01:57 +000035/** Pre-C++11 version of std::is_empty<T>. */
36template <typename T>
37class SkTIsEmpty {
38 struct Derived : public T { char unused; };
39public:
40 static const bool value = sizeof(Derived) == sizeof(char);
41};
42
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000043/** Pre-C++11 version of std::true_type. */
44typedef SkTBool<true> SkTrue;
45
46/** Pre-C++11 version of std::false_type. */
47typedef SkTBool<false> SkFalse;
48
49/** SkTIf_c::type = (condition) ? T : F;
50 * Pre-C++11 version of std::conditional.
51 */
52template <bool condition, typename T, typename F> struct SkTIf_c {
53 typedef F type;
54};
55template <typename T, typename F> struct SkTIf_c<true, T, F> {
56 typedef T type;
57};
58
59/** SkTIf::type = (Condition::value) ? T : F; */
60template <typename Condition, typename T, typename F> struct SkTIf {
61 typedef typename SkTIf_c<static_cast<bool>(Condition::value), T, F>::type type;
62};
63
64/** SkTMux::type = (a && b) ? Both : (a) ? A : (b) ? B : Neither; */
65template <typename a, typename b, typename Both, typename A, typename B, typename Neither>
66struct SkTMux {
67 typedef typename SkTIf<a, typename SkTIf<b, Both, A>::type,
68 typename SkTIf<b, B, Neither>::type>::type type;
69};
70
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000071/** SkTEnableIf_c::type = (condition) ? T : [does not exist]; */
72template <bool condition, class T = void> struct SkTEnableIf_c { };
73template <class T> struct SkTEnableIf_c<true, T> {
74 typedef T type;
75};
76
77/** SkTEnableIf::type = (Condition::value) ? T : [does not exist]; */
78template <class Condition, class T = void> struct SkTEnableIf
79 : public SkTEnableIf_c<static_cast<bool>(Condition::value), T> { };
80
81/** Use as a return type to enable a function only when cond_type::value is true,
82 * like C++14's std::enable_if_t. E.g. (N.B. this is a dumb example.)
83 * SK_WHEN(SkTrue, int) f(void* ptr) { return 1; }
84 * SK_WHEN(!SkTrue, int) f(void* ptr) { return 2; }
85 */
86#define SK_WHEN(cond_prefix, T) typename SkTEnableIf_c<cond_prefix::value, T>::type
mtklein97312d02015-04-01 08:11:16 -070087#define SK_WHEN_C(cond, T) typename SkTEnableIf_c<cond, T>::type
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000088
89// See http://en.wikibooks.org/wiki/More_C++_Idioms/Member_Detector
90#define SK_CREATE_MEMBER_DETECTOR(member) \
91template <typename T> \
92class HasMember_##member { \
93 struct Fallback { int member; }; \
94 struct Derived : T, Fallback {}; \
95 template <typename U, U> struct Check; \
96 template <typename U> static uint8_t func(Check<int Fallback::*, &U::member>*); \
97 template <typename U> static uint16_t func(...); \
98public: \
99 typedef HasMember_##member type; \
100 static const bool value = sizeof(func<Derived>(NULL)) == sizeof(uint16_t); \
101}
102
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000103// Same sort of thing as SK_CREATE_MEMBER_DETECTOR, but checks for the existence of a nested type.
104#define SK_CREATE_TYPE_DETECTOR(type) \
105template <typename T> \
106class HasType_##type { \
107 template <typename U> static uint8_t func(typename U::type*); \
108 template <typename U> static uint16_t func(...); \
109public: \
110 static const bool value = sizeof(func<T>(NULL)) == sizeof(uint8_t); \
111}
112
bungeman2bd02852015-08-05 12:09:57 -0700113namespace skstd {
114
bungemanafd7c742015-08-25 12:05:55 -0700115template <typename T> struct remove_const { using type = T; };
116template <typename T> struct remove_const<const T> { using type = T; };
117template <typename T> using remove_const_t = typename remove_const<T>::type;
118
119template <typename T> struct remove_volatile { using type = T; };
120template <typename T> struct remove_volatile<volatile T> { using type = T; };
121template <typename T> using remove_volatile_t = typename remove_volatile<T>::type;
122
123template <typename T> struct remove_cv { using type = remove_volatile_t<remove_const_t<T>>; };
124template <typename T> using remove_cv_t = typename remove_cv<T>::type;
125
126template <typename T> struct remove_reference { using type = T; };
127template <typename T> struct remove_reference<T&> { using type = T; };
128template <typename T> struct remove_reference<T&&> { using type = T; };
bungeman2bd02852015-08-05 12:09:57 -0700129template <typename T> using remove_reference_t = typename remove_reference<T>::type;
130
bungemanafd7c742015-08-25 12:05:55 -0700131template <typename T, typename U> struct is_same : SkFalse {};
132template <typename T> struct is_same<T, T> : SkTrue {};
133
134template <typename T> struct is_void : is_same<void, remove_cv_t<T>> {};
135
136template <typename T> struct is_reference : SkFalse {};
137template <typename T> struct is_reference<T&> : SkTrue {};
138template <typename T> struct is_reference<T&&> : SkTrue {};
139
bungeman2bd02852015-08-05 12:09:57 -0700140template <typename T> struct is_lvalue_reference : SkFalse {};
141template <typename T> struct is_lvalue_reference<T&> : SkTrue {};
142
bungemanafd7c742015-08-25 12:05:55 -0700143template <typename T> struct add_rvalue_reference {
144 using type = typename SkTIf_c<is_void<T>::value || is_reference<T>::value, T, T&&>::type;
145};
146template <typename T> using add_rvalue_reference_t = typename add_rvalue_reference<T>::type;
147
bungeman2bd02852015-08-05 12:09:57 -0700148} // namespace skstd
149
150/**
151 * SkTIsConst<T>::value is true if the type T is const.
152 * The type T is constrained not to be an array or reference type.
153 */
154template <typename T> struct SkTIsConst {
155 static T* t;
156 static uint16_t test(const volatile void*);
157 static uint32_t test(volatile void *);
158 static const bool value = (sizeof(uint16_t) == sizeof(test(t)));
159};
160
bungeman@google.com4b3ef5a2013-07-12 18:31:59 +0000161#endif