aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/private/SkTLogic.h
diff options
context:
space:
mode:
authorGravatar bungeman <bungeman@google.com>2015-08-28 07:09:20 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-08-28 07:09:20 -0700
commit761cf6186e291ee8e4761e1280634cfe9c42eccc (patch)
treeb56219f2b42b8816fac623ef3e549761f3d49f25 /include/private/SkTLogic.h
parent4204800cd8f311f11491cf2eb7e32ca681f05489 (diff)
Clean up SkTLogic.
This change regularizes Skia's type traits so that when <type_traits> can finally be used the transition is easier. Various traits are renamed to match <type_traits> and placed in the skstd namespace. Current users of these traits are updated. Review URL: https://codereview.chromium.org/1317593004
Diffstat (limited to 'include/private/SkTLogic.h')
-rw-r--r--include/private/SkTLogic.h197
1 files changed, 99 insertions, 98 deletions
diff --git a/include/private/SkTLogic.h b/include/private/SkTLogic.h
index f93f63b1a2..0a71d704dc 100644
--- a/include/private/SkTLogic.h
+++ b/include/private/SkTLogic.h
@@ -8,11 +8,6 @@
* This header provides some of the helpers (std::integral_constant) and
* type transformations (std::conditional) which will become available with
* C++11 in the type_traits header.
- *
- * Because we lack constexpr, we cannot mimic
- * std::integral_constant::'constexpr operator T()'.
- * As a result we introduce SkTBool and SkTIf similar to Boost in order to
- * minimize the visual noise of many uses of '::value'.
*/
#ifndef SkTLogic_DEFINED
@@ -20,71 +15,125 @@
#include <stdint.h>
-/** Represents a templated integer constant.
- * Pre-C++11 version of std::integral_constant.
- */
-template <typename T, T v> struct SkTIntegralConstant {
- static const T value = v;
- typedef T value_type;
- typedef SkTIntegralConstant<T, v> type;
+namespace skstd {
+
+template <typename T, T v> struct integral_constant {
+ static const/*expr*/ T value = v;
+ using value_type = T;
+ using type = integral_constant<T, v>;
+ //constexpr operator value_type() const noexcept { return value; }
+ //constexpr value_type operator()() const noexcept { return value; }
};
-/** Convenience specialization of SkTIntegralConstant. */
-template <bool b> struct SkTBool : SkTIntegralConstant<bool, b> { };
+template <bool B> using bool_constant = integral_constant<bool, B>;
+
+using true_type = bool_constant<true>;
+using false_type = bool_constant<false>;
+
+template <bool B, typename T, typename F> struct conditional { using type = T; };
+template <typename T, typename F> struct conditional<false, T, F> { using type = F; };
+template <bool B, typename T, typename F> using conditional_t = typename conditional<B, T, F>::type;
+
+template <bool B, typename T = void> struct enable_if { using type = T; };
+template <typename T> struct enable_if<false, T> {};
+template <bool B, typename T = void> using enable_if_t = typename enable_if<B, T>::type;
+
+template <typename T> struct remove_const { using type = T; };
+template <typename T> struct remove_const<const T> { using type = T; };
+template <typename T> using remove_const_t = typename remove_const<T>::type;
+
+template <typename T> struct remove_volatile { using type = T; };
+template <typename T> struct remove_volatile<volatile T> { using type = T; };
+template <typename T> using remove_volatile_t = typename remove_volatile<T>::type;
+
+template <typename T> struct remove_cv { using type = remove_volatile_t<remove_const_t<T>>; };
+template <typename T> using remove_cv_t = typename remove_cv<T>::type;
+
+template <typename T> struct remove_reference { using type = T; };
+template <typename T> struct remove_reference<T&> { using type = T; };
+template <typename T> struct remove_reference<T&&> { using type = T; };
+template <typename T> using remove_reference_t = typename remove_reference<T>::type;
+
+template <typename T, typename U> struct is_same : false_type {};
+template <typename T> struct is_same<T, T> : true_type {};
+
+template <typename T> struct is_void : is_same<void, remove_cv_t<T>> {};
+
+template <typename T> struct is_const : false_type {};
+template <typename T> struct is_const<const T> : true_type {};
-/** Pre-C++11 version of std::is_empty<T>. */
-template <typename T>
-class SkTIsEmpty {
- struct Derived : public T { char unused; };
-public:
+template <typename T> struct is_volatile : false_type {};
+template <typename T> struct is_volatile<volatile T> : true_type {};
+
+template <typename T> struct is_reference : false_type {};
+template <typename T> struct is_reference<T&> : true_type {};
+template <typename T> struct is_reference<T&&> : true_type {};
+
+template <typename T> struct is_lvalue_reference : false_type {};
+template <typename T> struct is_lvalue_reference<T&> : true_type {};
+
+template <typename T> struct is_empty_detector {
+ struct Derived : public remove_cv_t<T> { char unused; };
static const bool value = sizeof(Derived) == sizeof(char);
};
+template <typename T> struct is_empty : bool_constant<is_empty_detector<T>::value> {};
-/** Pre-C++11 version of std::true_type. */
-typedef SkTBool<true> SkTrue;
+template <typename T> struct add_const { using type = const T; };
+template <typename T> using add_const_t = typename add_const<T>::type;
-/** Pre-C++11 version of std::false_type. */
-typedef SkTBool<false> SkFalse;
+template <typename T> struct add_volatile { using type = volatile T; };
+template <typename T> using add_volatile_t = typename add_volatile<T>::type;
-/** SkTIf_c::type = (condition) ? T : F;
- * Pre-C++11 version of std::conditional.
- */
-template <bool condition, typename T, typename F> struct SkTIf_c {
- typedef F type;
-};
-template <typename T, typename F> struct SkTIf_c<true, T, F> {
- typedef T type;
+template <typename T> struct add_cv { using type = add_volatile_t<add_const_t<T>>; };
+template <typename T> using add_cv_t = typename add_cv<T>::type;
+
+template <typename T> struct add_rvalue_reference {
+ using type = conditional_t<is_void<T>::value || is_reference<T>::value, T, T&&>;
};
+template <typename T> using add_rvalue_reference_t = typename add_rvalue_reference<T>::type;
-/** SkTIf::type = (Condition::value) ? T : F; */
-template <typename Condition, typename T, typename F> struct SkTIf {
- typedef typename SkTIf_c<static_cast<bool>(Condition::value), T, F>::type type;
+} // namespace skstd
+
+// The sknonstd namespace contains things we would like to be proposed and feel std-ish.
+namespace sknonstd {
+
+// The name 'copy' here is fraught with peril. In this case it means 'append', not 'overwrite'.
+// Alternate proposed names are 'propagate', 'augment', or 'append' (and 'add', but already taken).
+// std::experimental::propagate_const already exists for other purposes in TSv2.
+// These also follow the <dest, source> pattern used by boost.
+template <typename D, typename S> struct copy_const {
+ using type = skstd::conditional_t<skstd::is_const<S>::value, skstd::add_const_t<D>, D>;
};
+template <typename D, typename S> using copy_const_t = typename copy_const<D, S>::type;
-/** SkTMux::type = (a && b) ? Both : (a) ? A : (b) ? B : Neither; */
-template <typename a, typename b, typename Both, typename A, typename B, typename Neither>
-struct SkTMux {
- typedef typename SkTIf<a, typename SkTIf<b, Both, A>::type,
- typename SkTIf<b, B, Neither>::type>::type type;
+template <typename D, typename S> struct copy_volatile {
+ using type = skstd::conditional_t<skstd::is_volatile<S>::value, skstd::add_volatile_t<D>, D>;
};
+template <typename D, typename S> using copy_volatile_t = typename copy_volatile<D, S>::type;
-/** SkTEnableIf_c::type = (condition) ? T : [does not exist]; */
-template <bool condition, class T = void> struct SkTEnableIf_c { };
-template <class T> struct SkTEnableIf_c<true, T> {
- typedef T type;
+template <typename D, typename S> struct copy_cv {
+ using type = copy_volatile_t<copy_const_t<D, S>, S>;
};
+template <typename D, typename S> using copy_cv_t = typename copy_cv<D, S>::type;
+
+// The name 'same' here means 'overwrite'.
+// Alternate proposed names are 'replace', 'transfer', or 'qualify_from'.
+// same_xxx<D, S> can be written as copy_xxx<remove_xxx_t<D>, S>
+template <typename D, typename S> using same_const = copy_const<skstd::remove_const_t<D>, S>;
+template <typename D, typename S> using same_const_t = typename same_const<D, S>::type;
+template <typename D, typename S> using same_volatile =copy_volatile<skstd::remove_volatile_t<D>,S>;
+template <typename D, typename S> using same_volatile_t = typename same_volatile<D, S>::type;
+template <typename D, typename S> using same_cv = copy_cv<skstd::remove_cv_t<D>, S>;
+template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type;
-/** SkTEnableIf::type = (Condition::value) ? T : [does not exist]; */
-template <class Condition, class T = void> struct SkTEnableIf
- : public SkTEnableIf_c<static_cast<bool>(Condition::value), T> { };
+} // namespace sknonstd
/** Use as a return type to enable a function only when cond_type::value is true,
* like C++14's std::enable_if_t. E.g. (N.B. this is a dumb example.)
- * SK_WHEN(SkTrue, int) f(void* ptr) { return 1; }
- * SK_WHEN(!SkTrue, int) f(void* ptr) { return 2; }
+ * SK_WHEN(true_type, int) f(void* ptr) { return 1; }
+ * SK_WHEN(!true_type, int) f(void* ptr) { return 2; }
*/
-#define SK_WHEN(cond_prefix, T) typename SkTEnableIf_c<cond_prefix::value, T>::type
-#define SK_WHEN_C(cond, T) typename SkTEnableIf_c<cond, T>::type
+#define SK_WHEN(cond_prefix, T) skstd::enable_if_t<cond_prefix::value, T>
// See http://en.wikibooks.org/wiki/More_C++_Idioms/Member_Detector
#define SK_CREATE_MEMBER_DETECTOR(member) \
@@ -110,52 +159,4 @@ public: \
static const bool value = sizeof(func<T>(NULL)) == sizeof(uint8_t); \
}
-namespace skstd {
-
-template <typename T> struct remove_const { using type = T; };
-template <typename T> struct remove_const<const T> { using type = T; };
-template <typename T> using remove_const_t = typename remove_const<T>::type;
-
-template <typename T> struct remove_volatile { using type = T; };
-template <typename T> struct remove_volatile<volatile T> { using type = T; };
-template <typename T> using remove_volatile_t = typename remove_volatile<T>::type;
-
-template <typename T> struct remove_cv { using type = remove_volatile_t<remove_const_t<T>>; };
-template <typename T> using remove_cv_t = typename remove_cv<T>::type;
-
-template <typename T> struct remove_reference { using type = T; };
-template <typename T> struct remove_reference<T&> { using type = T; };
-template <typename T> struct remove_reference<T&&> { using type = T; };
-template <typename T> using remove_reference_t = typename remove_reference<T>::type;
-
-template <typename T, typename U> struct is_same : SkFalse {};
-template <typename T> struct is_same<T, T> : SkTrue {};
-
-template <typename T> struct is_void : is_same<void, remove_cv_t<T>> {};
-
-template <typename T> struct is_reference : SkFalse {};
-template <typename T> struct is_reference<T&> : SkTrue {};
-template <typename T> struct is_reference<T&&> : SkTrue {};
-
-template <typename T> struct is_lvalue_reference : SkFalse {};
-template <typename T> struct is_lvalue_reference<T&> : SkTrue {};
-
-template <typename T> struct add_rvalue_reference {
- using type = typename SkTIf_c<is_void<T>::value || is_reference<T>::value, T, T&&>::type;
-};
-template <typename T> using add_rvalue_reference_t = typename add_rvalue_reference<T>::type;
-
-} // namespace skstd
-
-/**
- * SkTIsConst<T>::value is true if the type T is const.
- * The type T is constrained not to be an array or reference type.
- */
-template <typename T> struct SkTIsConst {
- static T* t;
- static uint16_t test(const volatile void*);
- static uint32_t test(volatile void *);
- static const bool value = (sizeof(uint16_t) == sizeof(test(t)));
-};
-
#endif