/* * Copyright 2013 Google Inc. * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. * * * 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. */ #ifndef SkTLogic_DEFINED #define SkTLogic_DEFINED #include "SkTypes.h" #include #include namespace skstd { using nullptr_t = decltype(nullptr); template struct integral_constant { static const/*expr*/ T value = v; using value_type = T; using type = integral_constant; //constexpr operator value_type() const noexcept { return value; } //constexpr value_type operator()() const noexcept { return value; } }; template using bool_constant = integral_constant; using true_type = bool_constant; using false_type = bool_constant; template struct conditional { using type = T; }; template struct conditional { using type = F; }; template using conditional_t = typename conditional::type; template struct enable_if { using type = T; }; template struct enable_if {}; template using enable_if_t = typename enable_if::type; template struct remove_const { using type = T; }; template struct remove_const { using type = T; }; template using remove_const_t = typename remove_const::type; template struct remove_volatile { using type = T; }; template struct remove_volatile { using type = T; }; template using remove_volatile_t = typename remove_volatile::type; template struct remove_cv { using type = remove_volatile_t>; }; template using remove_cv_t = typename remove_cv::type; template struct remove_reference { using type = T; }; template struct remove_reference { using type = T; }; template struct remove_reference { using type = T; }; template using remove_reference_t = typename remove_reference::type; template struct remove_extent { using type = T; }; template struct remove_extent { using type = T; }; template struct remove_extent { using type = T;}; template using remove_extent_t = typename remove_extent::type; template struct is_same : false_type {}; template struct is_same : true_type {}; template struct is_void : is_same> {}; template struct is_const : false_type {}; template struct is_const : true_type {}; template struct is_volatile : false_type {}; template struct is_volatile : true_type {}; template struct is_pointer_detector : false_type {}; template struct is_pointer_detector : true_type {}; template struct is_pointer : is_pointer_detector> {}; template struct is_reference : false_type {}; template struct is_reference : true_type {}; template struct is_reference : true_type {}; template struct is_lvalue_reference : false_type {}; template struct is_lvalue_reference : true_type {}; template struct is_rvalue_reference : false_type {}; template struct is_rvalue_reference : true_type {}; template struct is_class_detector { using yes_type = uint8_t; using no_type = uint16_t; template static yes_type clazz(int U::*); template static no_type clazz(...); static const/*expr*/ bool value = sizeof(clazz(0)) == sizeof(yes_type) /*&& !is_union::value*/; }; template struct is_class : bool_constant::value> {}; template ::value> struct is_empty_detector { struct Derived : public T { char unused; }; static const/*expr*/ bool value = sizeof(Derived) == sizeof(char); }; template struct is_empty_detector { static const/*expr*/ bool value = false; }; template struct is_empty : bool_constant::value> {}; template struct is_array : false_type {}; template struct is_array : true_type {}; template struct is_array : true_type {}; // template struct is_function< // R [calling-convention] (Args...[, ...]) [const] [volatile] [&|&&]> : true_type {}; // The cv and ref-qualified versions are strange types we're currently avoiding, so not supported. // On all platforms, variadic functions only exist in the c calling convention. template struct is_function : false_type { }; #if !defined(SK_BUILD_FOR_WIN) template struct is_function : true_type {}; #else #if defined(_M_IX86) template struct is_function : true_type {}; template struct is_function : true_type {}; template struct is_function : true_type {}; #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 template struct is_function : true_type {}; #endif #else template struct is_function : true_type {}; template struct is_function : true_type {}; #endif #endif template struct is_function : true_type {}; template struct add_const { using type = const T; }; template using add_const_t = typename add_const::type; template struct add_volatile { using type = volatile T; }; template using add_volatile_t = typename add_volatile::type; template struct add_cv { using type = add_volatile_t>; }; template using add_cv_t = typename add_cv::type; template struct add_pointer { using type = remove_reference_t*; }; template using add_pointer_t = typename add_pointer::type; template ::value> struct add_lvalue_reference_init { using type = T; }; template struct add_lvalue_reference_init { using type = T&; }; template struct add_lvalue_reference : add_lvalue_reference_init { }; template using add_lvalue_reference_t = typename add_lvalue_reference::type; template ::value> struct add_rvalue_reference_init { using type = T; }; template struct add_rvalue_reference_init { using type = T&&; }; template struct add_rvalue_reference : add_rvalue_reference_init {}; template using add_rvalue_reference_t = typename add_rvalue_reference::type; /* This is 'just' a forward declaration. */ template add_rvalue_reference_t declval() /*noexcept*/; template ::value||is_function::value||is_array::value> struct is_convertible_detector { static const/*expr*/ bool value = is_void::value; }; template struct is_convertible_detector { using yes_type = uint8_t; using no_type = uint16_t; template static void param_convertable_to(To); template static decltype(param_convertable_to(declval()), yes_type()) convertible(int); template static no_type convertible(...); static const/*expr*/ bool value = sizeof(convertible(0)) == sizeof(yes_type); }; template struct is_convertible : bool_constant::value> { }; template struct decay { using U = remove_reference_t; using type = conditional_t::value, remove_extent_t*, conditional_t::value, add_pointer_t, remove_cv_t>>; }; template using decay_t = typename decay::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 pattern used by boost. template struct copy_const { using type = skstd::conditional_t::value, skstd::add_const_t, D>; }; template using copy_const_t = typename copy_const::type; template struct copy_volatile { using type = skstd::conditional_t::value, skstd::add_volatile_t, D>; }; template using copy_volatile_t = typename copy_volatile::type; template struct copy_cv { using type = copy_volatile_t, S>; }; template using copy_cv_t = typename copy_cv::type; // The name 'same' here means 'overwrite'. // Alternate proposed names are 'replace', 'transfer', or 'qualify_from'. // same_xxx can be written as copy_xxx, S> template using same_const = copy_const, S>; template using same_const_t = typename same_const::type; template using same_volatile =copy_volatile,S>; template using same_volatile_t = typename same_volatile::type; template using same_cv = copy_cv, S>; template using same_cv_t = typename same_cv::type; } // namespace sknonstd // Just a pithier wrapper for enable_if_t. #define SK_WHEN(condition, T) skstd::enable_if_t #endif