1f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com/* 2f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * Copyright 2013 Google Inc. 3f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * 4f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * Use of this source code is governed by a BSD-style license that can be 5f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * found in the LICENSE file. 6f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * 7f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * 8f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * This header provides some of the helpers (std::integral_constant) and 9f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * type transformations (std::conditional) which will become available with 10f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * C++11 in the type_traits header. 11f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * 12f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * Because we lack constexpr, we cannot mimic 13f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * std::integral_constant::'constexpr operator T()'. 14f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * As a result we introduce SkTBool and SkTIf similar to Boost in order to 15f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * minimize the visual noise of many uses of '::value'. 16f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com */ 17f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com 18f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com#ifndef SkTLogic_DEFINED 19f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com#define SkTLogic_DEFINED 20f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com 21f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com/** Represents a templated integer constant. 22f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * Pre-C++11 version of std::integral_constant. 23f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com */ 24f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.comtemplate <typename T, T v> struct SkTIntegralConstant { 25f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com static const T value = v; 26f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com typedef T value_type; 27f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com typedef SkTIntegralConstant<T, v> type; 28f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com}; 29f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com 30f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com/** Convenience specialization of SkTIntegralConstant. */ 31f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.comtemplate <bool b> struct SkTBool : SkTIntegralConstant<bool, b> { }; 32f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com 3308bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.org/** Pre-C++11 version of std::is_empty<T>. */ 3408bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.orgtemplate <typename T> 3508bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.orgclass SkTIsEmpty { 3608bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.org struct Derived : public T { char unused; }; 3708bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.orgpublic: 3808bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.org static const bool value = sizeof(Derived) == sizeof(char); 3908bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.org}; 4008bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.org 41f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com/** Pre-C++11 version of std::true_type. */ 42f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.comtypedef SkTBool<true> SkTrue; 43f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com 44f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com/** Pre-C++11 version of std::false_type. */ 45f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.comtypedef SkTBool<false> SkFalse; 46f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com 47f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com/** SkTIf_c::type = (condition) ? T : F; 48f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com * Pre-C++11 version of std::conditional. 49f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com */ 50f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.comtemplate <bool condition, typename T, typename F> struct SkTIf_c { 51f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com typedef F type; 52f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com}; 53f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.comtemplate <typename T, typename F> struct SkTIf_c<true, T, F> { 54f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com typedef T type; 55f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com}; 56f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com 57f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com/** SkTIf::type = (Condition::value) ? T : F; */ 58f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.comtemplate <typename Condition, typename T, typename F> struct SkTIf { 59f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com typedef typename SkTIf_c<static_cast<bool>(Condition::value), T, F>::type type; 60f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com}; 61f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com 62f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com/** SkTMux::type = (a && b) ? Both : (a) ? A : (b) ? B : Neither; */ 63f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.comtemplate <typename a, typename b, typename Both, typename A, typename B, typename Neither> 64f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.comstruct SkTMux { 65f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com typedef typename SkTIf<a, typename SkTIf<b, Both, A>::type, 66f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com typename SkTIf<b, B, Neither>::type>::type type; 67f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com}; 68f5cc5b140c1c00c536e02b5cfbe158bb2d5c2c15bungeman@google.com 692e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org/** SkTEnableIf_c::type = (condition) ? T : [does not exist]; */ 702e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.orgtemplate <bool condition, class T = void> struct SkTEnableIf_c { }; 712e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.orgtemplate <class T> struct SkTEnableIf_c<true, T> { 722e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org typedef T type; 732e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org}; 742e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 752e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org/** SkTEnableIf::type = (Condition::value) ? T : [does not exist]; */ 762e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.orgtemplate <class Condition, class T = void> struct SkTEnableIf 772e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org : public SkTEnableIf_c<static_cast<bool>(Condition::value), T> { }; 782e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 792e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org/** Use as a return type to enable a function only when cond_type::value is true, 802e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org * like C++14's std::enable_if_t. E.g. (N.B. this is a dumb example.) 812e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org * SK_WHEN(SkTrue, int) f(void* ptr) { return 1; } 822e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org * SK_WHEN(!SkTrue, int) f(void* ptr) { return 2; } 832e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org */ 842e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org#define SK_WHEN(cond_prefix, T) typename SkTEnableIf_c<cond_prefix::value, T>::type 852e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 862e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org// See http://en.wikibooks.org/wiki/More_C++_Idioms/Member_Detector 872e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org#define SK_CREATE_MEMBER_DETECTOR(member) \ 882e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.orgtemplate <typename T> \ 892e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.orgclass HasMember_##member { \ 902e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org struct Fallback { int member; }; \ 912e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org struct Derived : T, Fallback {}; \ 922e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org template <typename U, U> struct Check; \ 932e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org template <typename U> static uint8_t func(Check<int Fallback::*, &U::member>*); \ 942e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org template <typename U> static uint16_t func(...); \ 952e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.orgpublic: \ 962e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org typedef HasMember_##member type; \ 972e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org static const bool value = sizeof(func<Derived>(NULL)) == sizeof(uint16_t); \ 982e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org} 992e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 10073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// Same sort of thing as SK_CREATE_MEMBER_DETECTOR, but checks for the existence of a nested type. 10173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org#define SK_CREATE_TYPE_DETECTOR(type) \ 10273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgtemplate <typename T> \ 10373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgclass HasType_##type { \ 10473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org template <typename U> static uint8_t func(typename U::type*); \ 10573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org template <typename U> static uint16_t func(...); \ 10673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgpublic: \ 10773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org static const bool value = sizeof(func<T>(NULL)) == sizeof(uint8_t); \ 10873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org} 10973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org 1104b3ef5ad33aa3dcef083101177dd8a91978cc7debungeman@google.com#endif 111