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.
|
|
|
|
*
|
|
|
|
*
|
2016-01-05 22:59:40 +00:00
|
|
|
* 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.
|
2013-07-12 18:22:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkTLogic_DEFINED
|
|
|
|
#define SkTLogic_DEFINED
|
|
|
|
|
2015-09-07 19:45:52 +00:00
|
|
|
#include "SkTypes.h"
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2015-08-05 19:09:57 +00:00
|
|
|
#include <stdint.h>
|
2016-01-05 22:59:40 +00:00
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
2015-08-05 19:09:57 +00:00
|
|
|
|
2015-08-28 14:09:20 +00:00
|
|
|
namespace skstd {
|
|
|
|
|
2016-01-05 22:59:40 +00:00
|
|
|
template <bool B> using bool_constant = std::integral_constant<bool, B>;
|
2015-09-07 19:45:52 +00:00
|
|
|
|
2016-01-05 22:59:40 +00:00
|
|
|
template <bool B, typename T, typename F> using conditional_t = typename std::conditional<B, T, F>::type;
|
|
|
|
template <bool B, typename T = void> using enable_if_t = typename std::enable_if<B, T>::type;
|
2015-08-28 14:09:20 +00:00
|
|
|
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename T> using remove_const_t = typename std::remove_const<T>::type;
|
|
|
|
template <typename T> using remove_volatile_t = typename std::remove_volatile<T>::type;
|
|
|
|
template <typename T> using remove_cv_t = typename std::remove_cv<T>::type;
|
|
|
|
template <typename T> using remove_reference_t = typename std::remove_reference<T>::type;
|
|
|
|
template <typename T> using remove_extent_t = typename std::remove_extent<T>::type;
|
2015-08-28 14:09:20 +00:00
|
|
|
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename T, typename U> struct is_same : std::false_type {};
|
|
|
|
template <typename T> struct is_same<T, T> : std::true_type {};
|
2015-08-28 14:09:20 +00:00
|
|
|
|
|
|
|
template <typename T> struct is_void : is_same<void, remove_cv_t<T>> {};
|
|
|
|
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename T> struct is_const : std::false_type {};
|
|
|
|
template <typename T> struct is_const<const T> : std::true_type {};
|
2013-07-12 18:22:49 +00:00
|
|
|
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename T> struct is_volatile : std::false_type {};
|
|
|
|
template <typename T> struct is_volatile<volatile T> : std::true_type {};
|
2015-08-28 14:09:20 +00:00
|
|
|
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename T> struct is_pointer_detector : std::false_type {};
|
|
|
|
template <typename T> struct is_pointer_detector<T*> : std::true_type {};
|
2015-09-07 19:45:52 +00:00
|
|
|
template <typename T> struct is_pointer : is_pointer_detector<remove_cv_t<T>> {};
|
|
|
|
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename T> struct is_reference : std::false_type {};
|
|
|
|
template <typename T> struct is_reference<T&> : std::true_type {};
|
|
|
|
template <typename T> struct is_reference<T&&> : std::true_type {};
|
2015-08-28 14:09:20 +00:00
|
|
|
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename T> struct is_lvalue_reference : std::false_type {};
|
|
|
|
template <typename T> struct is_lvalue_reference<T&> : std::true_type {};
|
2015-08-28 14:09:20 +00:00
|
|
|
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename T> struct is_rvalue_reference : std::false_type {};
|
|
|
|
template <typename T> struct is_rvalue_reference<T&&> : std::true_type {};
|
2015-09-07 19:45:52 +00:00
|
|
|
|
|
|
|
template <typename T> struct is_class_detector {
|
|
|
|
using yes_type = uint8_t;
|
|
|
|
using no_type = uint16_t;
|
|
|
|
template <typename U> static yes_type clazz(int U::*);
|
|
|
|
template <typename U> static no_type clazz(...);
|
|
|
|
static const/*expr*/ bool value = sizeof(clazz<T>(0)) == sizeof(yes_type) /*&& !is_union<T>::value*/;
|
|
|
|
};
|
|
|
|
template <typename T> struct is_class : bool_constant<is_class_detector<T>::value> {};
|
|
|
|
|
|
|
|
template <typename T, bool = is_class<T>::value> struct is_empty_detector {
|
|
|
|
struct Derived : public T { char unused; };
|
|
|
|
static const/*expr*/ bool value = sizeof(Derived) == sizeof(char);
|
|
|
|
};
|
|
|
|
template <typename T> struct is_empty_detector<T, false> {
|
|
|
|
static const/*expr*/ bool value = false;
|
2014-05-07 18:01:57 +00:00
|
|
|
};
|
2015-08-28 14:09:20 +00:00
|
|
|
template <typename T> struct is_empty : bool_constant<is_empty_detector<T>::value> {};
|
2014-05-07 18:01:57 +00:00
|
|
|
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename T> struct is_array : std::false_type {};
|
|
|
|
template <typename T> struct is_array<T[]> : std::true_type {};
|
|
|
|
template <typename T, size_t N> struct is_array<T[N]> : std::true_type {};
|
2015-09-07 19:45:52 +00:00
|
|
|
|
|
|
|
// template<typename R, typename... Args> struct is_function<
|
2016-01-05 22:59:40 +00:00
|
|
|
// R [calling-convention] (Args...[, ...]) [const] [volatile] [&|&&]> : std::true_type {};
|
2015-09-07 19:45:52 +00:00
|
|
|
// 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.
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename> struct is_function : std::false_type {};
|
2015-09-07 19:45:52 +00:00
|
|
|
#if !defined(SK_BUILD_FOR_WIN)
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename R, typename... Args> struct is_function<R(Args...)> : std::true_type {};
|
2015-09-07 19:45:52 +00:00
|
|
|
#else
|
|
|
|
#if defined(_M_IX86)
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename R, typename... Args> struct is_function<R __cdecl (Args...)> : std::true_type {};
|
|
|
|
template <typename R, typename... Args> struct is_function<R __stdcall (Args...)> : std::true_type {};
|
|
|
|
template <typename R, typename... Args> struct is_function<R __fastcall (Args...)> : std::true_type {};
|
2015-12-01 15:21:09 +00:00
|
|
|
#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename R, typename... Args> struct is_function<R __vectorcall (Args...)> : std::true_type {};
|
2015-12-01 15:21:09 +00:00
|
|
|
#endif
|
2015-09-07 19:45:52 +00:00
|
|
|
#else
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename R, typename... Args> struct is_function<R __cdecl (Args...)> : std::true_type {};
|
|
|
|
template <typename R, typename... Args> struct is_function<R __vectorcall (Args...)> : std::true_type {};
|
2015-09-07 19:45:52 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename R, typename... Args> struct is_function<R(Args..., ...)> : std::true_type {};
|
2013-07-12 18:22:49 +00:00
|
|
|
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename T> using add_const_t = typename std::add_const<T>::type;
|
|
|
|
template <typename T> using add_volatile_t = typename std::add_volatile<T>::type;
|
|
|
|
template <typename T> using add_cv_t = typename std::add_cv<T>::type;
|
|
|
|
template <typename T> using add_pointer_t = typename std::add_pointer<T>::type;
|
2015-09-07 19:45:52 +00:00
|
|
|
|
|
|
|
template <typename T, bool=is_void<T>::value> struct add_lvalue_reference_init { using type = T; };
|
|
|
|
template <typename T> struct add_lvalue_reference_init<T, false> { using type = T&; };
|
2016-01-05 22:59:40 +00:00
|
|
|
template <typename T> struct add_lvalue_reference : add_lvalue_reference_init<T> {};
|
2015-09-07 19:45:52 +00:00
|
|
|
template <typename T> using add_lvalue_reference_t = typename add_lvalue_reference<T>::type;
|
|
|
|
|
|
|
|
template <typename T, bool=is_void<T>::value> struct add_rvalue_reference_init { using type = T; };
|
|
|
|
template <typename T> struct add_rvalue_reference_init<T, false> { using type = T&&; };
|
|
|
|
template <typename T> struct add_rvalue_reference : add_rvalue_reference_init<T> {};
|
2015-08-28 14:09:20 +00:00
|
|
|
template <typename T> using add_rvalue_reference_t = typename add_rvalue_reference<T>::type;
|
2013-07-12 18:22:49 +00:00
|
|
|
|
2015-09-07 19:45:52 +00:00
|
|
|
template <typename S, typename D, bool=is_void<S>::value||is_function<D>::value||is_array<D>::value>
|
|
|
|
struct is_convertible_detector {
|
|
|
|
static const/*expr*/ bool value = is_void<D>::value;
|
|
|
|
};
|
|
|
|
template <typename S, typename D> struct is_convertible_detector<S, D, false> {
|
|
|
|
using yes_type = uint8_t;
|
|
|
|
using no_type = uint16_t;
|
|
|
|
|
|
|
|
template <typename To> static void param_convertable_to(To);
|
|
|
|
|
|
|
|
template <typename From, typename To>
|
2016-01-05 22:59:40 +00:00
|
|
|
static decltype(param_convertable_to<To>(std::declval<From>()), yes_type()) convertible(int);
|
2015-09-07 19:45:52 +00:00
|
|
|
|
|
|
|
template <typename, typename> static no_type convertible(...);
|
|
|
|
|
|
|
|
static const/*expr*/ bool value = sizeof(convertible<S, D>(0)) == sizeof(yes_type);
|
|
|
|
};
|
|
|
|
template<typename S, typename D> struct is_convertible
|
2016-01-05 22:59:40 +00:00
|
|
|
: bool_constant<is_convertible_detector<S, D>::value> {};
|
2015-09-07 19:45:52 +00:00
|
|
|
|
|
|
|
template <typename T> struct decay {
|
|
|
|
using U = remove_reference_t<T>;
|
|
|
|
using type = conditional_t<is_array<U>::value,
|
|
|
|
remove_extent_t<U>*,
|
|
|
|
conditional_t<is_function<U>::value, add_pointer_t<U>, remove_cv_t<U>>>;
|
|
|
|
};
|
|
|
|
template <typename T> using decay_t = typename decay<T>::type;
|
|
|
|
|
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 {
|
|
|
|
using type = skstd::conditional_t<skstd::is_const<S>::value, skstd::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 {
|
|
|
|
using type = skstd::conditional_t<skstd::is_volatile<S>::value, skstd::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>
|
|
|
|
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;
|
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
|
|
|
|
2015-09-28 17:33:02 +00:00
|
|
|
// Just a pithier wrapper for enable_if_t.
|
|
|
|
#define SK_WHEN(condition, T) skstd::enable_if_t<!!(condition), T>
|
Add pattern matchers for SkRecord
This is a mid-level library for finding patterns of commands in an SkRecord. At the API level, it's a bit regex inspired. Some examples:
- Pattern1<Is<DrawRect>> matches a single DrawRect
- Pattern1<Star<Is<DrawRect>>> matches 0 or more DrawRects
- Pattern2<Is<ClipRect>, Is<DrawRect>> matches a single clip rect followed by a single draw rect
- Pattern3<Is<Save>, Star<IsDraw>, Is<Restore>> matches a single Save, followed by any number of Draws, followed by Restore
- Pattern1<Or<Is<DrawRect>, Is<ClipRect>>> matches a DrawRect or a ClipRect
- Pattern1<Not<Is<ClipRect>>> matches a command that's notClipRect.
Once you have a pattern, you can call .search() on it to step through ranges of matching commands. This means patterns can replace most of the custom iteration logic for optimization passes: the generic pattern searching steps through all the optimization candidates, which optimization-specific code further inspects and mutates.
SkRecordTraits is now unused. Bye bye!
Generated code and performance of SkRecordOpts is very similar to what it was before. (I had to use SK_ALWAYS_INLINE in a few places to make this so.)
BUG=skia:2378
R=fmalita@chromium.org, bungeman@google.com, mtklein@google.com
Author: mtklein@chromium.org
Review URL: https://codereview.chromium.org/263063002
git-svn-id: http://skia.googlecode.com/svn/trunk@14582 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-05-05 21:59:52 +00:00
|
|
|
|
2013-07-12 18:31:59 +00:00
|
|
|
#endif
|