2013-07-12 18:22:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*
|
|
|
|
*
|
2020-04-30 11:45:25 +00:00
|
|
|
* This header provides some std:: features early in the skstd namespace
|
|
|
|
* and several Skia-specific additions in the sknonstd namespace.
|
2013-07-12 18:22:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkTLogic_DEFINED
|
|
|
|
#define SkTLogic_DEFINED
|
|
|
|
|
2019-04-04 22:00:05 +00:00
|
|
|
#include <cstddef>
|
2016-01-05 22:59:40 +00:00
|
|
|
#include <type_traits>
|
2020-04-30 11:45:25 +00:00
|
|
|
#include <utility>
|
2021-05-07 21:20:06 +00:00
|
|
|
#include "include/private/SkTo.h"
|
2015-08-05 19:09:57 +00:00
|
|
|
|
2015-08-28 14:09:20 +00:00
|
|
|
namespace skstd {
|
|
|
|
|
2020-04-30 11:45:25 +00:00
|
|
|
// C++17, <variant>
|
2019-07-03 22:05:12 +00:00
|
|
|
struct monostate {};
|
|
|
|
|
2020-04-30 11:45:25 +00:00
|
|
|
// C++17, <type_traits>
|
2019-10-07 14:41:30 +00:00
|
|
|
template<typename...> struct conjunction : std::true_type { };
|
2019-10-21 22:20:20 +00:00
|
|
|
template<typename T> struct conjunction<T> : T { };
|
|
|
|
template<typename T, typename... Ts>
|
|
|
|
struct conjunction<T, Ts...> : std::conditional<bool(T::value), conjunction<Ts...>, T>::type { };
|
2020-04-30 11:45:25 +00:00
|
|
|
|
2020-05-26 16:58:59 +00:00
|
|
|
// C++17, std::data, std::size
|
|
|
|
template<typename Container>
|
|
|
|
constexpr auto data(Container& c) -> decltype(c.data()) { return c.data(); }
|
|
|
|
template<typename Container>
|
|
|
|
constexpr auto data(const Container& c) -> decltype(c.data()) { return c.data(); }
|
|
|
|
template<typename Array, size_t N>
|
|
|
|
constexpr auto data(Array(&a)[N]) -> decltype(a) { return a; }
|
|
|
|
template<typename T>
|
|
|
|
constexpr const T* data(std::initializer_list<T> i) { return i.begin(); }
|
|
|
|
|
|
|
|
template<typename Container>
|
|
|
|
constexpr auto size(Container& c) -> decltype(c.size()) { return c.size(); }
|
|
|
|
template<typename Array, size_t N>
|
|
|
|
constexpr size_t size(Array(&)[N]) { return N; }
|
|
|
|
template<typename T>
|
|
|
|
constexpr const T* size(std::initializer_list<T> i) { return i.end() - i.begin(); }
|
2015-08-28 14:09:20 +00:00
|
|
|
} // 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 {
|
2020-04-30 11:45:25 +00:00
|
|
|
using type = std::conditional_t<std::is_const<S>::value, std::add_const_t<D>, D>;
|
2013-07-12 18:22:49 +00:00
|
|
|
};
|
2015-08-28 14:09:20 +00:00
|
|
|
template <typename D, typename S> using copy_const_t = typename copy_const<D, S>::type;
|
2013-07-12 18:22:49 +00:00
|
|
|
|
2015-08-28 14:09:20 +00:00
|
|
|
template <typename D, typename S> struct copy_volatile {
|
2020-04-30 11:45:25 +00:00
|
|
|
using type = std::conditional_t<std::is_volatile<S>::value, std::add_volatile_t<D>, D>;
|
2013-07-12 18:22:49 +00:00
|
|
|
};
|
2015-08-28 14:09:20 +00:00
|
|
|
template <typename D, typename S> using copy_volatile_t = typename copy_volatile<D, S>::type;
|
2013-07-12 18:22:49 +00:00
|
|
|
|
2015-08-28 14:09:20 +00:00
|
|
|
template <typename D, typename S> struct copy_cv {
|
|
|
|
using type = copy_volatile_t<copy_const_t<D, S>, S>;
|
2014-04-28 16:19:45 +00:00
|
|
|
};
|
2015-08-28 14:09:20 +00:00
|
|
|
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>
|
2020-04-30 11:45:25 +00:00
|
|
|
template <typename D, typename S> using same_const = copy_const<std::remove_const_t<D>, S>;
|
2015-08-28 14:09:20 +00:00
|
|
|
template <typename D, typename S> using same_const_t = typename same_const<D, S>::type;
|
2020-04-30 11:45:25 +00:00
|
|
|
template <typename D, typename S> using same_volatile =copy_volatile<std::remove_volatile_t<D>,S>;
|
2015-08-28 14:09:20 +00:00
|
|
|
template <typename D, typename S> using same_volatile_t = typename same_volatile<D, S>::type;
|
2020-04-30 11:45:25 +00:00
|
|
|
template <typename D, typename S> using same_cv = copy_cv<std::remove_cv_t<D>, S>;
|
2015-08-28 14:09:20 +00:00
|
|
|
template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type;
|
2014-04-28 16:19:45 +00:00
|
|
|
|
2015-08-28 14:09:20 +00:00
|
|
|
} // namespace sknonstd
|
2014-04-28 16:19:45 +00:00
|
|
|
|
2021-05-07 21:20:06 +00:00
|
|
|
template <typename Container>
|
|
|
|
constexpr int SkCount(const Container& c) { return SkTo<int>(skstd::size(c)); }
|
|
|
|
|
2013-07-12 18:31:59 +00:00
|
|
|
#endif
|