blob: d188242446d0345af89e2fdc45da86de7c49f45f [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
21/** Represents a templated integer constant.
22 * Pre-C++11 version of std::integral_constant.
23 */
24template <typename T, T v> struct SkTIntegralConstant {
25 static const T value = v;
26 typedef T value_type;
27 typedef SkTIntegralConstant<T, v> type;
28};
29
30/** Convenience specialization of SkTIntegralConstant. */
31template <bool b> struct SkTBool : SkTIntegralConstant<bool, b> { };
32
commit-bot@chromium.org08bf86c2014-05-07 18:01:57 +000033/** Pre-C++11 version of std::is_empty<T>. */
34template <typename T>
35class SkTIsEmpty {
36 struct Derived : public T { char unused; };
37public:
38 static const bool value = sizeof(Derived) == sizeof(char);
39};
40
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000041/** Pre-C++11 version of std::true_type. */
42typedef SkTBool<true> SkTrue;
43
44/** Pre-C++11 version of std::false_type. */
45typedef SkTBool<false> SkFalse;
46
47/** SkTIf_c::type = (condition) ? T : F;
48 * Pre-C++11 version of std::conditional.
49 */
50template <bool condition, typename T, typename F> struct SkTIf_c {
51 typedef F type;
52};
53template <typename T, typename F> struct SkTIf_c<true, T, F> {
54 typedef T type;
55};
56
57/** SkTIf::type = (Condition::value) ? T : F; */
58template <typename Condition, typename T, typename F> struct SkTIf {
59 typedef typename SkTIf_c<static_cast<bool>(Condition::value), T, F>::type type;
60};
61
62/** SkTMux::type = (a && b) ? Both : (a) ? A : (b) ? B : Neither; */
63template <typename a, typename b, typename Both, typename A, typename B, typename Neither>
64struct SkTMux {
65 typedef typename SkTIf<a, typename SkTIf<b, Both, A>::type,
66 typename SkTIf<b, B, Neither>::type>::type type;
67};
68
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000069/** SkTEnableIf_c::type = (condition) ? T : [does not exist]; */
70template <bool condition, class T = void> struct SkTEnableIf_c { };
71template <class T> struct SkTEnableIf_c<true, T> {
72 typedef T type;
73};
74
75/** SkTEnableIf::type = (Condition::value) ? T : [does not exist]; */
76template <class Condition, class T = void> struct SkTEnableIf
77 : public SkTEnableIf_c<static_cast<bool>(Condition::value), T> { };
78
79/** Use as a return type to enable a function only when cond_type::value is true,
80 * like C++14's std::enable_if_t. E.g. (N.B. this is a dumb example.)
81 * SK_WHEN(SkTrue, int) f(void* ptr) { return 1; }
82 * SK_WHEN(!SkTrue, int) f(void* ptr) { return 2; }
83 */
84#define SK_WHEN(cond_prefix, T) typename SkTEnableIf_c<cond_prefix::value, T>::type
mtklein97312d02015-04-01 08:11:16 -070085#define SK_WHEN_C(cond, T) typename SkTEnableIf_c<cond, T>::type
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000086
87// See http://en.wikibooks.org/wiki/More_C++_Idioms/Member_Detector
88#define SK_CREATE_MEMBER_DETECTOR(member) \
89template <typename T> \
90class HasMember_##member { \
91 struct Fallback { int member; }; \
92 struct Derived : T, Fallback {}; \
93 template <typename U, U> struct Check; \
94 template <typename U> static uint8_t func(Check<int Fallback::*, &U::member>*); \
95 template <typename U> static uint16_t func(...); \
96public: \
97 typedef HasMember_##member type; \
98 static const bool value = sizeof(func<Derived>(NULL)) == sizeof(uint16_t); \
99}
100
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000101// Same sort of thing as SK_CREATE_MEMBER_DETECTOR, but checks for the existence of a nested type.
102#define SK_CREATE_TYPE_DETECTOR(type) \
103template <typename T> \
104class HasType_##type { \
105 template <typename U> static uint8_t func(typename U::type*); \
106 template <typename U> static uint16_t func(...); \
107public: \
108 static const bool value = sizeof(func<T>(NULL)) == sizeof(uint8_t); \
109}
110
bungeman@google.com4b3ef5a2013-07-12 18:31:59 +0000111#endif