OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkTFitsIn_DEFINED | 8 #ifndef SkTFitsIn_DEFINED |
9 #define SkTFitsIn_DEFINED | 9 #define SkTFitsIn_DEFINED |
10 | 10 |
11 #include "SkTypes.h" | 11 #include "SkTypes.h" |
12 #include "SkTLogic.h" | 12 #include "SkTLogic.h" |
13 #include <limits> | 13 #include <limits> |
14 | 14 |
bungeman-skia
2016/01/05 18:54:43
need <type_traits>
bungeman-skia
2016/01/05 18:59:41
Done.
| |
15 namespace sktfitsin { | 15 namespace sktfitsin { |
16 namespace Private { | 16 namespace Private { |
17 | 17 |
18 /** SkTMux::type = (a && b) ? Both : (a) ? A : (b) ? B : Neither; */ | 18 /** SkTMux::type = (a && b) ? Both : (a) ? A : (b) ? B : Neither; */ |
19 template <bool a, bool b, typename Both, typename A, typename B, typename Neithe r> | 19 template <bool a, bool b, typename Both, typename A, typename B, typename Neithe r> |
20 struct SkTMux { | 20 struct SkTMux { |
21 using type = skstd::conditional_t<a, skstd::conditional_t<b, Both, A>, | 21 using type = skstd::conditional_t<a, skstd::conditional_t<b, Both, A>, |
22 skstd::conditional_t<b, B, Neither>>; | 22 skstd::conditional_t<b, B, Neither>>; |
23 }; | 23 }; |
24 | 24 |
25 /** SkTHasMoreDigits = (digits(A) >= digits(B)) ? true_type : false_type. */ | 25 /** SkTHasMoreDigits = (digits(A) >= digits(B)) ? true_type : false_type. */ |
26 template<typename A, typename B> struct SkTHasMoreDigits | 26 template<typename A, typename B> struct SkTHasMoreDigits |
27 : skstd::bool_constant<std::numeric_limits<A>::digits >= std::numeric_limits <B>::digits> | 27 : skstd::bool_constant<std::numeric_limits<A>::digits >= std::numeric_limits <B>::digits> |
28 { }; | 28 { }; |
29 | 29 |
30 /** A high or low side predicate which is used when it is statically known | 30 /** A high or low side predicate which is used when it is statically known |
31 * that source values are in the range of the Destination. | 31 * that source values are in the range of the Destination. |
32 */ | 32 */ |
33 template <typename S> struct SkTOutOfRange_False { | 33 template <typename S> struct SkTOutOfRange_False { |
34 typedef skstd::false_type can_be_true; | 34 typedef std::false_type can_be_true; |
35 typedef S source_type; | 35 typedef S source_type; |
36 static bool apply(S s) { | 36 static bool apply(S s) { |
37 return false; | 37 return false; |
38 } | 38 } |
39 }; | 39 }; |
40 | 40 |
41 /** A low side predicate which tests if the source value < Min(D). | 41 /** A low side predicate which tests if the source value < Min(D). |
42 * Assumes that Min(S) <= Min(D). | 42 * Assumes that Min(S) <= Min(D). |
43 */ | 43 */ |
44 template <typename D, typename S> struct SkTOutOfRange_LT_MinD { | 44 template <typename D, typename S> struct SkTOutOfRange_LT_MinD { |
45 typedef skstd::true_type can_be_true; | 45 typedef std::true_type can_be_true; |
46 typedef S source_type; | 46 typedef S source_type; |
47 static bool apply(S s) { | 47 static bool apply(S s) { |
48 typedef SkTHasMoreDigits<S, D> precondition; | 48 typedef SkTHasMoreDigits<S, D> precondition; |
49 static_assert(precondition::value, "SkTOutOfRange_LT_MinD__minS_gt_minD" ); | 49 static_assert(precondition::value, "SkTOutOfRange_LT_MinD__minS_gt_minD" ); |
50 | 50 |
51 return s < static_cast<S>((std::numeric_limits<D>::min)()); | 51 return s < static_cast<S>((std::numeric_limits<D>::min)()); |
52 } | 52 } |
53 }; | 53 }; |
54 | 54 |
55 /** A low side predicate which tests if the source value is less than 0. */ | 55 /** A low side predicate which tests if the source value is less than 0. */ |
56 template <typename D, typename S> struct SkTOutOfRange_LT_Zero { | 56 template <typename D, typename S> struct SkTOutOfRange_LT_Zero { |
57 typedef skstd::true_type can_be_true; | 57 typedef std::true_type can_be_true; |
58 typedef S source_type; | 58 typedef S source_type; |
59 static bool apply(S s) { | 59 static bool apply(S s) { |
60 return s < static_cast<S>(0); | 60 return s < static_cast<S>(0); |
61 } | 61 } |
62 }; | 62 }; |
63 | 63 |
64 /** A high side predicate which tests if the source value > Max(D). | 64 /** A high side predicate which tests if the source value > Max(D). |
65 * Assumes that Max(S) >= Max(D). | 65 * Assumes that Max(S) >= Max(D). |
66 */ | 66 */ |
67 template <typename D, typename S> struct SkTOutOfRange_GT_MaxD { | 67 template <typename D, typename S> struct SkTOutOfRange_GT_MaxD { |
68 typedef skstd::true_type can_be_true; | 68 typedef std::true_type can_be_true; |
69 typedef S source_type; | 69 typedef S source_type; |
70 static bool apply(S s) { | 70 static bool apply(S s) { |
71 typedef SkTHasMoreDigits<S, D> precondition; | 71 typedef SkTHasMoreDigits<S, D> precondition; |
72 static_assert(precondition::value, "SkTOutOfRange_GT_MaxD__maxS_lt_maxD" ); | 72 static_assert(precondition::value, "SkTOutOfRange_GT_MaxD__maxS_lt_maxD" ); |
73 | 73 |
74 return s > static_cast<S>((std::numeric_limits<D>::max)()); | 74 return s > static_cast<S>((std::numeric_limits<D>::max)()); |
75 } | 75 } |
76 }; | 76 }; |
77 | 77 |
78 /** Composes two SkTOutOfRange predicates. | 78 /** Composes two SkTOutOfRange predicates. |
79 * First checks OutOfRange_Low then, if in range, OutOfRange_High. | 79 * First checks OutOfRange_Low then, if in range, OutOfRange_High. |
80 */ | 80 */ |
81 template<class OutOfRange_Low, class OutOfRange_High> struct SkTOutOfRange_Eithe r { | 81 template<class OutOfRange_Low, class OutOfRange_High> struct SkTOutOfRange_Eithe r { |
82 typedef skstd::true_type can_be_true; | 82 typedef std::true_type can_be_true; |
83 typedef typename OutOfRange_Low::source_type source_type; | 83 typedef typename OutOfRange_Low::source_type source_type; |
84 static bool apply(source_type s) { | 84 static bool apply(source_type s) { |
85 bool outOfRange = OutOfRange_Low::apply(s); | 85 bool outOfRange = OutOfRange_Low::apply(s); |
86 if (!outOfRange) { | 86 if (!outOfRange) { |
87 outOfRange = OutOfRange_High::apply(s); | 87 outOfRange = OutOfRange_High::apply(s); |
88 } | 88 } |
89 return outOfRange; | 89 return outOfRange; |
90 } | 90 } |
91 }; | 91 }; |
92 | 92 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 | 208 |
209 /** Returns true if the integer source value 's' will fit in the integer destina tion type 'D'. */ | 209 /** Returns true if the integer source value 's' will fit in the integer destina tion type 'D'. */ |
210 template <typename D, typename S> inline bool SkTFitsIn(S s) { | 210 template <typename D, typename S> inline bool SkTFitsIn(S s) { |
211 static_assert(std::numeric_limits<S>::is_integer, "SkTFitsIn_source_must_be_ integer"); | 211 static_assert(std::numeric_limits<S>::is_integer, "SkTFitsIn_source_must_be_ integer"); |
212 static_assert(std::numeric_limits<D>::is_integer, "SkTFitsIn_destination_mus t_be_integer"); | 212 static_assert(std::numeric_limits<D>::is_integer, "SkTFitsIn_destination_mus t_be_integer"); |
213 | 213 |
214 return !sktfitsin::Private::SkTFitsIn<D, S>::type::OutOfRange(s); | 214 return !sktfitsin::Private::SkTFitsIn<D, S>::type::OutOfRange(s); |
215 } | 215 } |
216 | 216 |
217 #endif | 217 #endif |
OLD | NEW |