/* * 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 (like std::enable_if_t) which will * become available with C++14 in the type_traits header (in the skstd * namespace). This header also provides several Skia specific additions such * as SK_WHEN and the sknonstd namespace. */ #ifndef SkTLogic_DEFINED #define SkTLogic_DEFINED #include "SkTypes.h" #include #include #include #include namespace skstd { template using bool_constant = std::integral_constant; template using conditional_t = typename std::conditional::type; template using enable_if_t = typename std::enable_if::type; template using remove_const_t = typename std::remove_const::type; template using remove_volatile_t = typename std::remove_volatile::type; template using remove_cv_t = typename std::remove_cv::type; template using remove_pointer_t = typename std::remove_pointer::type; template using remove_reference_t = typename std::remove_reference::type; template using remove_extent_t = typename std::remove_extent::type; // template struct is_function< // R [calling-convention] (Args...[, ...]) [const] [volatile] [&|&&]> : std::true_type {}; // The cv and ref-qualified versions are strange types we're currently avoiding, so not supported. // These aren't supported in msvc either until vs2015u2. // On all platforms, variadic functions only exist in the c calling convention. // mcvc 2013 introduced __vectorcall, but it wan't until 2015 that it was added to is_function. template struct is_function : std::false_type {}; #if !defined(SK_BUILD_FOR_WIN) template struct is_function : std::true_type {}; #else template struct is_function : std::true_type {}; #if defined(_M_IX86) template struct is_function : std::true_type {}; template struct is_function : std::true_type {}; #endif #if defined(_MSC_VER) && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 template struct is_function : std::true_type {}; #endif #endif template struct is_function : std::true_type {}; template using add_const_t = typename std::add_const::type; template using add_volatile_t = typename std::add_volatile::type; template using add_cv_t = typename std::add_cv::type; template using add_pointer_t = typename std::add_pointer::type; template using add_lvalue_reference_t = typename std::add_lvalue_reference::type; template ::value || is_function::value || std::is_array::value> struct is_convertible_detector { static const/*expr*/ bool value = std::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(std::declval()), yes_type()) convertible(int); template static no_type convertible(...); static const/*expr*/ bool value = sizeof(convertible(0)) == sizeof(yes_type); }; // std::is_convertable is known to be broken (not work with incomplete types) in Android clang NDK. // This is currently what prevents us from using std::unique_ptr. template struct is_convertible : bool_constant::value> {}; } // 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