2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
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.
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'.
18 #ifndef SkTLogic_DEFINED
19 #define SkTLogic_DEFINED
21 /** Represents a templated integer constant.
22 * Pre-C++11 version of std::integral_constant.
24 template <typename T, T v> struct SkTIntegralConstant {
25 static const T value = v;
27 typedef SkTIntegralConstant<T, v> type;
30 /** Convenience specialization of SkTIntegralConstant. */
31 template <bool b> struct SkTBool : SkTIntegralConstant<bool, b> { };
33 /** Pre-C++11 version of std::true_type. */
34 typedef SkTBool<true> SkTrue;
36 /** Pre-C++11 version of std::false_type. */
37 typedef SkTBool<false> SkFalse;
39 /** SkTIf_c::type = (condition) ? T : F;
40 * Pre-C++11 version of std::conditional.
42 template <bool condition, typename T, typename F> struct SkTIf_c {
45 template <typename T, typename F> struct SkTIf_c<true, T, F> {
49 /** SkTIf::type = (Condition::value) ? T : F; */
50 template <typename Condition, typename T, typename F> struct SkTIf {
51 typedef typename SkTIf_c<static_cast<bool>(Condition::value), T, F>::type type;
54 /** SkTMux::type = (a && b) ? Both : (a) ? A : (b) ? B : Neither; */
55 template <typename a, typename b, typename Both, typename A, typename B, typename Neither>
57 typedef typename SkTIf<a, typename SkTIf<b, Both, A>::type,
58 typename SkTIf<b, B, Neither>::type>::type type;
61 /** SkTEnableIf_c::type = (condition) ? T : [does not exist]; */
62 template <bool condition, class T = void> struct SkTEnableIf_c { };
63 template <class T> struct SkTEnableIf_c<true, T> {
67 /** SkTEnableIf::type = (Condition::value) ? T : [does not exist]; */
68 template <class Condition, class T = void> struct SkTEnableIf
69 : public SkTEnableIf_c<static_cast<bool>(Condition::value), T> { };
71 /** Use as a return type to enable a function only when cond_type::value is true,
72 * like C++14's std::enable_if_t. E.g. (N.B. this is a dumb example.)
73 * SK_WHEN(SkTrue, int) f(void* ptr) { return 1; }
74 * SK_WHEN(!SkTrue, int) f(void* ptr) { return 2; }
76 #define SK_WHEN(cond_prefix, T) typename SkTEnableIf_c<cond_prefix::value, T>::type
78 // See http://en.wikibooks.org/wiki/More_C++_Idioms/Member_Detector
79 #define SK_CREATE_MEMBER_DETECTOR(member) \
80 template <typename T> \
81 class HasMember_##member { \
82 struct Fallback { int member; }; \
83 struct Derived : T, Fallback {}; \
84 template <typename U, U> struct Check; \
85 template <typename U> static uint8_t func(Check<int Fallback::*, &U::member>*); \
86 template <typename U> static uint16_t func(...); \
88 typedef HasMember_##member type; \
89 static const bool value = sizeof(func<Derived>(NULL)) == sizeof(uint16_t); \
92 // Same sort of thing as SK_CREATE_MEMBER_DETECTOR, but checks for the existence of a nested type.
93 #define SK_CREATE_TYPE_DETECTOR(type) \
94 template <typename T> \
95 class HasType_##type { \
96 template <typename U> static uint8_t func(typename U::type*); \
97 template <typename U> static uint16_t func(...); \
99 static const bool value = sizeof(func<T>(NULL)) == sizeof(uint8_t); \