Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: Source/core/svg/animation/SMILTime.h

Issue 478273002: Use NaN/Inf to represent unresolved/indefinite SMILTimes (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Updated patch. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Source/core/svg/animation/SMILTime.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #ifndef SMILTime_h 26 #ifndef SMILTime_h
27 #define SMILTime_h 27 #define SMILTime_h
28 28
29 #include "wtf/Assertions.h" 29 #include "wtf/Assertions.h"
30 #include "wtf/HashTraits.h"
30 #include "wtf/MathExtras.h" 31 #include "wtf/MathExtras.h"
31 32
32 namespace blink { 33 namespace blink {
33 34
34 class SMILTime { 35 class SMILTime {
35 public: 36 public:
36 SMILTime() : m_time(0) { } 37 SMILTime() : m_time(0) { }
37 SMILTime(double time) : m_time(time) { ASSERT(!std::isnan(time)); } 38 SMILTime(double time) : m_time(time) { }
38 SMILTime(const SMILTime& o) : m_time(o.m_time) { }
39 39
40 static SMILTime unresolved() { return unresolvedValue; } 40 static SMILTime unresolved() { return std::numeric_limits<double>::quiet_NaN (); }
41 static SMILTime indefinite() { return indefiniteValue; } 41 static SMILTime indefinite() { return std::numeric_limits<double>::infinity( ); }
42 42
43 SMILTime& operator=(const SMILTime& o) { m_time = o.m_time; return *this; }
44 double value() const { return m_time; } 43 double value() const { return m_time; }
45 44
46 bool isFinite() const { return m_time < indefiniteValue; } 45 bool isFinite() const { return std::isfinite(m_time); }
47 bool isIndefinite() const { return m_time == indefiniteValue; } 46 bool isIndefinite() const { return std::isinf(m_time); }
48 bool isUnresolved() const { return m_time == unresolvedValue; } 47 bool isUnresolved() const { return std::isnan(m_time); }
49 48
50 private: 49 private:
51 static const double unresolvedValue;
52 static const double indefiniteValue;
53
54 double m_time; 50 double m_time;
55 }; 51 };
56 52
57 class SMILTimeWithOrigin { 53 class SMILTimeWithOrigin {
58 public: 54 public:
59 enum Origin { 55 enum Origin {
60 ParserOrigin, 56 ParserOrigin,
61 ScriptOrigin 57 ScriptOrigin
62 }; 58 };
63 59
(...skipping 17 matching lines...) Expand all
81 }; 77 };
82 78
83 struct SMILInterval { 79 struct SMILInterval {
84 SMILInterval() { } 80 SMILInterval() { }
85 SMILInterval(const SMILTime& begin, const SMILTime& end) : begin(begin), end (end) { } 81 SMILInterval(const SMILTime& begin, const SMILTime& end) : begin(begin), end (end) { }
86 82
87 SMILTime begin; 83 SMILTime begin;
88 SMILTime end; 84 SMILTime end;
89 }; 85 };
90 86
91 inline bool operator==(const SMILTime& a, const SMILTime& b) { return a.isFinite () && a.value() == b.value(); } 87 inline bool operator==(const SMILTime& a, const SMILTime& b) { return (a.isUnres olved() && b.isUnresolved()) || a.value() == b.value(); }
92 inline bool operator!(const SMILTime& a) { return !a.isFinite() || !a.value(); } 88 inline bool operator!(const SMILTime& a) { return !a.isFinite() || !a.value(); }
93 inline bool operator!=(const SMILTime& a, const SMILTime& b) { return !operator= =(a, b); } 89 inline bool operator!=(const SMILTime& a, const SMILTime& b) { return !operator= =(a, b); }
94 inline bool operator>(const SMILTime& a, const SMILTime& b) { return a.value() > b.value(); } 90
95 inline bool operator<(const SMILTime& a, const SMILTime& b) { return a.value() < b.value(); } 91 // Ordering of SMILTimes has to follow: finite < indefinite (Inf) < unresolved ( NaN). The first comparison is handled by IEEE754 but
96 inline bool operator>=(const SMILTime& a, const SMILTime& b) { return a.value() > b.value() || operator==(a, b); } 92 // NaN values must be checked explicitly to guarantee that unresolved is ordered correctly too.
97 inline bool operator<=(const SMILTime& a, const SMILTime& b) { return a.value() < b.value() || operator==(a, b); } 93 inline bool operator>(const SMILTime& a, const SMILTime& b) { return a.isUnresol ved() || (a.value() > b.value()); }
94 inline bool operator<(const SMILTime& a, const SMILTime& b) { return operator>(b , a); }
95 inline bool operator>=(const SMILTime& a, const SMILTime& b) { return operator>( a, b) || operator==(a, b); }
96 inline bool operator<=(const SMILTime& a, const SMILTime& b) { return operator<( a, b) || operator==(a, b); }
98 inline bool operator<(const SMILTimeWithOrigin& a, const SMILTimeWithOrigin& b) { return a.time() < b.time(); } 97 inline bool operator<(const SMILTimeWithOrigin& a, const SMILTimeWithOrigin& b) { return a.time() < b.time(); }
99 98
100 SMILTime operator+(const SMILTime&, const SMILTime&); 99 SMILTime operator+(const SMILTime&, const SMILTime&);
101 SMILTime operator-(const SMILTime&, const SMILTime&); 100 SMILTime operator-(const SMILTime&, const SMILTime&);
102 // So multiplying times does not make too much sense but SMIL defines it for dur ation * repeatCount 101 // So multiplying times does not make too much sense but SMIL defines it for dur ation * repeatCount
103 SMILTime operator*(const SMILTime&, const SMILTime&); 102 SMILTime operator*(const SMILTime&, const SMILTime&);
104 103
105 inline bool operator!=(const SMILInterval& a, const SMILInterval& b) 104 inline bool operator!=(const SMILInterval& a, const SMILInterval& b)
106 { 105 {
107 // Compare the "raw" values since the operator!= for SMILTime always return 106 // Compare the "raw" values since the operator!= for SMILTime always return
108 // true for non-finite times. 107 // true for non-finite times.
109 return a.begin.value() != b.begin.value() || a.end.value() != b.end.value(); 108 return a.begin.value() != b.begin.value() || a.end.value() != b.end.value();
110 } 109 }
111 110
112 } 111 struct SMILTimeHash {
112 static unsigned hash(const SMILTime& key) { return WTF::FloatHash<double>::h ash(key.value()); }
113 static bool equal(const SMILTime& a, const SMILTime& b) { return WTF::FloatH ash<double>::equal(a.value(), b.value()); }
114 static const bool safeToCompareToEmptyOrDeleted = true;
115 };
116
117 } // namespace blink
118
119 namespace WTF {
120
121 template<> struct DefaultHash<blink::SMILTime> {
122 typedef blink::SMILTimeHash Hash;
123 };
124
125 template<> struct HashTraits<blink::SMILTime> : GenericHashTraits<blink::SMILTim e> {
126 static blink::SMILTime emptyValue() { return blink::SMILTime::unresolved(); }
127 static void constructDeletedValue(blink::SMILTime& slot) { slot = -std::nume ric_limits<double>::infinity(); }
128 static bool isDeletedValue(blink::SMILTime value) { return value == -std::nu meric_limits<double>::infinity(); }
129 };
130
131 } // namespace WTF
113 132
114 #endif // SMILTime_h 133 #endif // SMILTime_h
OLDNEW
« no previous file with comments | « no previous file | Source/core/svg/animation/SMILTime.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698