OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2011 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 CheckedArithmetic_h | 26 #ifndef CheckedArithmetic_h |
27 #define CheckedArithmetic_h | 27 #define CheckedArithmetic_h |
28 | 28 |
29 #include "wtf/Assertions.h" | 29 #include "wtf/Assertions.h" |
30 #include "wtf/EnumClass.h" | 30 #include "wtf/EnumClass.h" |
31 #include "wtf/TypeTraits.h" | 31 #include "wtf/TypeTraits.h" |
32 | 32 |
33 #include <limits> | 33 #include <limits> |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 * | 65 * |
66 */ | 66 */ |
67 | 67 |
68 namespace WTF { | 68 namespace WTF { |
69 | 69 |
70 ENUM_CLASS(CheckedState) | 70 ENUM_CLASS(CheckedState) |
71 { | 71 { |
72 DidOverflow, | 72 DidOverflow, |
73 DidNotOverflow | 73 DidNotOverflow |
74 } ENUM_CLASS_END(CheckedState); | 74 } ENUM_CLASS_END(CheckedState); |
75 | 75 |
76 class CrashOnOverflow { | 76 class CrashOnOverflow { |
77 protected: | 77 protected: |
78 NO_RETURN_DUE_TO_CRASH void overflowed() | 78 NO_RETURN_DUE_TO_CRASH void overflowed() |
79 { | 79 { |
80 CRASH(); | 80 CRASH(); |
81 } | 81 } |
82 | 82 |
83 void clearOverflow() { } | 83 void clearOverflow() { } |
84 | 84 |
85 public: | 85 public: |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 }; | 134 }; |
135 | 135 |
136 template <typename Target, typename Source> struct BoundsChecker<Target, Source,
false, true> { | 136 template <typename Target, typename Source> struct BoundsChecker<Target, Source,
false, true> { |
137 static bool inBounds(Source value) | 137 static bool inBounds(Source value) |
138 { | 138 { |
139 // Target is unsigned so any value less than zero is clearly unsafe | 139 // Target is unsigned so any value less than zero is clearly unsafe |
140 if (value < 0) | 140 if (value < 0) |
141 return false; | 141 return false; |
142 // If our (unsigned) Target is the same or greater width we can | 142 // If our (unsigned) Target is the same or greater width we can |
143 // convert value to type Target without losing precision | 143 // convert value to type Target without losing precision |
144 if (sizeof(Target) >= sizeof(Source)) | 144 if (sizeof(Target) >= sizeof(Source)) |
145 return static_cast<Target>(value) <= std::numeric_limits<Target>::ma
x(); | 145 return static_cast<Target>(value) <= std::numeric_limits<Target>::ma
x(); |
146 // The signed Source type has greater precision than the target so | 146 // The signed Source type has greater precision than the target so |
147 // max(Target) -> Source will widen. | 147 // max(Target) -> Source will widen. |
148 return value <= static_cast<Source>(std::numeric_limits<Target>::max()); | 148 return value <= static_cast<Source>(std::numeric_limits<Target>::max()); |
149 } | 149 } |
150 }; | 150 }; |
151 | 151 |
152 template <typename Target, typename Source> struct BoundsChecker<Target, Source,
true, false> { | 152 template <typename Target, typename Source> struct BoundsChecker<Target, Source,
true, false> { |
153 static bool inBounds(Source value) | 153 static bool inBounds(Source value) |
154 { | 154 { |
155 // Signed target with an unsigned source | 155 // Signed target with an unsigned source |
156 if (sizeof(Target) <= sizeof(Source)) | 156 if (sizeof(Target) <= sizeof(Source)) |
157 return value <= static_cast<Source>(std::numeric_limits<Target>::max
()); | 157 return value <= static_cast<Source>(std::numeric_limits<Target>::max
()); |
158 // Target is Wider than Source so we're guaranteed to fit any value in | 158 // Target is Wider than Source so we're guaranteed to fit any value in |
159 // unsigned Source | 159 // unsigned Source |
160 return true; | 160 return true; |
161 } | 161 } |
162 }; | 162 }; |
163 | 163 |
164 template <typename Target, typename Source, bool CanElide = IsSameType<Target, S
ource>::value || (sizeof(Target) > sizeof(Source)) > struct BoundsCheckElider; | 164 template <typename Target, typename Source, bool CanElide = IsSameType<Target, S
ource>::value || (sizeof(Target) > sizeof(Source)) > struct BoundsCheckElider; |
165 template <typename Target, typename Source> struct BoundsCheckElider<Target, Sou
rce, true> { | 165 template <typename Target, typename Source> struct BoundsCheckElider<Target, Sou
rce, true> { |
166 static bool inBounds(Source) { return true; } | 166 static bool inBounds(Source) { return true; } |
167 }; | 167 }; |
168 template <typename Target, typename Source> struct BoundsCheckElider<Target, Sou
rce, false> : public BoundsChecker<Target, Source> { | 168 template <typename Target, typename Source> struct BoundsCheckElider<Target, Sou
rce, false> : public BoundsChecker<Target, Source> { |
169 }; | 169 }; |
170 | 170 |
171 template <typename Target, typename Source> static inline bool isInBounds(Source
value) | 171 template <typename Target, typename Source> static inline bool isInBounds(Source
value) |
172 { | 172 { |
173 return BoundsCheckElider<Target, Source>::inBounds(value); | 173 return BoundsCheckElider<Target, Source>::inBounds(value); |
174 } | 174 } |
175 | 175 |
176 template <typename T> struct RemoveChecked { | 176 template <typename T> struct RemoveChecked { |
177 typedef T CleanType; | 177 typedef T CleanType; |
178 static const CleanType DefaultValue = 0; | 178 static const CleanType DefaultValue = 0; |
179 }; | 179 }; |
180 | 180 |
181 template <typename T> struct RemoveChecked<Checked<T, CrashOnOverflow> > { | 181 template <typename T> struct RemoveChecked<Checked<T, CrashOnOverflow> > { |
182 typedef typename RemoveChecked<T>::CleanType CleanType; | 182 typedef typename RemoveChecked<T>::CleanType CleanType; |
183 static const CleanType DefaultValue = 0; | 183 static const CleanType DefaultValue = 0; |
184 }; | 184 }; |
185 | 185 |
186 template <typename T> struct RemoveChecked<Checked<T, RecordOverflow> > { | 186 template <typename T> struct RemoveChecked<Checked<T, RecordOverflow> > { |
187 typedef typename RemoveChecked<T>::CleanType CleanType; | 187 typedef typename RemoveChecked<T>::CleanType CleanType; |
188 static const CleanType DefaultValue = 0; | 188 static const CleanType DefaultValue = 0; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 typedef U ResultType; | 220 typedef U ResultType; |
221 }; | 221 }; |
222 | 222 |
223 template <typename U, typename V> struct ResultBase<U, V, false, true> { | 223 template <typename U, typename V> struct ResultBase<U, V, false, true> { |
224 typedef typename SignednessSelector<U, V>::ResultType ResultType; | 224 typedef typename SignednessSelector<U, V>::ResultType ResultType; |
225 }; | 225 }; |
226 | 226 |
227 template <typename U, typename V> struct Result : ResultBase<typename RemoveChec
ked<U>::CleanType, typename RemoveChecked<V>::CleanType> { | 227 template <typename U, typename V> struct Result : ResultBase<typename RemoveChec
ked<U>::CleanType, typename RemoveChecked<V>::CleanType> { |
228 }; | 228 }; |
229 | 229 |
230 template <typename LHS, typename RHS, typename ResultType = typename Result<LHS,
RHS>::ResultType, | 230 template <typename LHS, typename RHS, typename ResultType = typename Result<LHS,
RHS>::ResultType, |
231 bool lhsSigned = std::numeric_limits<LHS>::is_signed, bool rhsSigned = std::
numeric_limits<RHS>::is_signed> struct ArithmeticOperations; | 231 bool lhsSigned = std::numeric_limits<LHS>::is_signed, bool rhsSigned = std::
numeric_limits<RHS>::is_signed> struct ArithmeticOperations; |
232 | 232 |
233 template <typename LHS, typename RHS, typename ResultType> struct ArithmeticOper
ations<LHS, RHS, ResultType, true, true> { | 233 template <typename LHS, typename RHS, typename ResultType> struct ArithmeticOper
ations<LHS, RHS, ResultType, true, true> { |
234 // LHS and RHS are signed types | 234 // LHS and RHS are signed types |
235 | 235 |
236 // Helper function | 236 // Helper function |
237 static inline bool signsMatch(LHS lhs, RHS rhs) | 237 static inline bool signsMatch(LHS lhs, RHS rhs) |
238 { | 238 { |
239 return (lhs ^ rhs) >= 0; | 239 return (lhs ^ rhs) >= 0; |
240 } | 240 } |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 static inline bool add(int64_t lhs, int64_t rhs, ResultType& result) | 339 static inline bool add(int64_t lhs, int64_t rhs, ResultType& result) |
340 { | 340 { |
341 int64_t temp = lhs + rhs; | 341 int64_t temp = lhs + rhs; |
342 if (temp < std::numeric_limits<ResultType>::min()) | 342 if (temp < std::numeric_limits<ResultType>::min()) |
343 return false; | 343 return false; |
344 if (temp > std::numeric_limits<ResultType>::max()) | 344 if (temp > std::numeric_limits<ResultType>::max()) |
345 return false; | 345 return false; |
346 result = static_cast<ResultType>(temp); | 346 result = static_cast<ResultType>(temp); |
347 return true; | 347 return true; |
348 } | 348 } |
349 | 349 |
350 static inline bool sub(int64_t lhs, int64_t rhs, ResultType& result) | 350 static inline bool sub(int64_t lhs, int64_t rhs, ResultType& result) |
351 { | 351 { |
352 int64_t temp = lhs - rhs; | 352 int64_t temp = lhs - rhs; |
353 if (temp < std::numeric_limits<ResultType>::min()) | 353 if (temp < std::numeric_limits<ResultType>::min()) |
354 return false; | 354 return false; |
355 if (temp > std::numeric_limits<ResultType>::max()) | 355 if (temp > std::numeric_limits<ResultType>::max()) |
356 return false; | 356 return false; |
357 result = static_cast<ResultType>(temp); | 357 result = static_cast<ResultType>(temp); |
358 return true; | 358 return true; |
359 } | 359 } |
(...skipping 13 matching lines...) Expand all Loading... |
373 { | 373 { |
374 return static_cast<int64_t>(lhs) == static_cast<int64_t>(rhs); | 374 return static_cast<int64_t>(lhs) == static_cast<int64_t>(rhs); |
375 } | 375 } |
376 }; | 376 }; |
377 | 377 |
378 template <typename ResultType> struct ArithmeticOperations<unsigned, int, Result
Type, false, true> { | 378 template <typename ResultType> struct ArithmeticOperations<unsigned, int, Result
Type, false, true> { |
379 static inline bool add(int64_t lhs, int64_t rhs, ResultType& result) | 379 static inline bool add(int64_t lhs, int64_t rhs, ResultType& result) |
380 { | 380 { |
381 return ArithmeticOperations<int, unsigned, ResultType>::add(rhs, lhs, re
sult); | 381 return ArithmeticOperations<int, unsigned, ResultType>::add(rhs, lhs, re
sult); |
382 } | 382 } |
383 | 383 |
384 static inline bool sub(int64_t lhs, int64_t rhs, ResultType& result) | 384 static inline bool sub(int64_t lhs, int64_t rhs, ResultType& result) |
385 { | 385 { |
386 return ArithmeticOperations<int, unsigned, ResultType>::sub(lhs, rhs, re
sult); | 386 return ArithmeticOperations<int, unsigned, ResultType>::sub(lhs, rhs, re
sult); |
387 } | 387 } |
388 | 388 |
389 static inline bool multiply(int64_t lhs, int64_t rhs, ResultType& result) | 389 static inline bool multiply(int64_t lhs, int64_t rhs, ResultType& result) |
390 { | 390 { |
391 return ArithmeticOperations<int, unsigned, ResultType>::multiply(rhs, lh
s, result); | 391 return ArithmeticOperations<int, unsigned, ResultType>::multiply(rhs, lh
s, result); |
392 } | 392 } |
393 | 393 |
(...skipping 17 matching lines...) Expand all Loading... |
411 { | 411 { |
412 return ArithmeticOperations<U, V, R>::multiply(lhs, rhs, result); | 412 return ArithmeticOperations<U, V, R>::multiply(lhs, rhs, result); |
413 } | 413 } |
414 | 414 |
415 template <typename U, typename V> static inline bool safeEquals(U lhs, V rhs) | 415 template <typename U, typename V> static inline bool safeEquals(U lhs, V rhs) |
416 { | 416 { |
417 return ArithmeticOperations<U, V>::equals(lhs, rhs); | 417 return ArithmeticOperations<U, V>::equals(lhs, rhs); |
418 } | 418 } |
419 | 419 |
420 enum ResultOverflowedTag { ResultOverflowed }; | 420 enum ResultOverflowedTag { ResultOverflowed }; |
421 | 421 |
422 // FIXME: Needed to workaround http://llvm.org/bugs/show_bug.cgi?id=10801 | 422 // FIXME: Needed to workaround http://llvm.org/bugs/show_bug.cgi?id=10801 |
423 static inline bool workAroundClangBug() { return true; } | 423 static inline bool workAroundClangBug() { return true; } |
424 | 424 |
425 template <typename T, class OverflowHandler> class Checked : public OverflowHand
ler { | 425 template <typename T, class OverflowHandler> class Checked : public OverflowHand
ler { |
426 public: | 426 public: |
427 template <typename _T, class _OverflowHandler> friend class Checked; | 427 template <typename _T, class _OverflowHandler> friend class Checked; |
428 Checked() | 428 Checked() |
429 : m_value(0) | 429 : m_value(0) |
430 { | 430 { |
431 } | 431 } |
432 | 432 |
433 Checked(ResultOverflowedTag) | 433 Checked(ResultOverflowedTag) |
434 : m_value(0) | 434 : m_value(0) |
435 { | 435 { |
436 // FIXME: Remove this when clang fixes http://llvm.org/bugs/show_bug.cgi
?id=10801 | 436 // FIXME: Remove this when clang fixes http://llvm.org/bugs/show_bug.cgi
?id=10801 |
437 if (workAroundClangBug()) | 437 if (workAroundClangBug()) |
438 this->overflowed(); | 438 this->overflowed(); |
439 } | 439 } |
440 | 440 |
441 template <typename U> Checked(U value) | 441 template <typename U> Checked(U value) |
442 { | 442 { |
443 if (!isInBounds<T>(value)) | 443 if (!isInBounds<T>(value)) |
444 this->overflowed(); | 444 this->overflowed(); |
445 m_value = static_cast<T>(value); | 445 m_value = static_cast<T>(value); |
446 } | 446 } |
447 | 447 |
448 template <typename V> Checked(const Checked<T, V>& rhs) | 448 template <typename V> Checked(const Checked<T, V>& rhs) |
449 : m_value(rhs.m_value) | 449 : m_value(rhs.m_value) |
450 { | 450 { |
451 if (rhs.hasOverflowed()) | 451 if (rhs.hasOverflowed()) |
452 this->overflowed(); | 452 this->overflowed(); |
453 } | 453 } |
454 | 454 |
455 template <typename U> Checked(const Checked<U, OverflowHandler>& rhs) | 455 template <typename U> Checked(const Checked<U, OverflowHandler>& rhs) |
456 : OverflowHandler(rhs) | 456 : OverflowHandler(rhs) |
457 { | 457 { |
458 if (!isInBounds<T>(rhs.m_value)) | 458 if (!isInBounds<T>(rhs.m_value)) |
459 this->overflowed(); | 459 this->overflowed(); |
460 m_value = static_cast<T>(rhs.m_value); | 460 m_value = static_cast<T>(rhs.m_value); |
461 } | 461 } |
462 | 462 |
463 template <typename U, typename V> Checked(const Checked<U, V>& rhs) | 463 template <typename U, typename V> Checked(const Checked<U, V>& rhs) |
464 { | 464 { |
465 if (rhs.hasOverflowed()) | 465 if (rhs.hasOverflowed()) |
466 this->overflowed(); | 466 this->overflowed(); |
467 if (!isInBounds<T>(rhs.m_value)) | 467 if (!isInBounds<T>(rhs.m_value)) |
468 this->overflowed(); | 468 this->overflowed(); |
469 m_value = static_cast<T>(rhs.m_value); | 469 m_value = static_cast<T>(rhs.m_value); |
470 } | 470 } |
471 | 471 |
472 const Checked& operator=(Checked rhs) | 472 const Checked& operator=(Checked rhs) |
473 { | 473 { |
474 this->clearOverflow(); | 474 this->clearOverflow(); |
475 if (rhs.hasOverflowed()) | 475 if (rhs.hasOverflowed()) |
476 this->overflowed(); | 476 this->overflowed(); |
477 m_value = static_cast<T>(rhs.m_value); | 477 m_value = static_cast<T>(rhs.m_value); |
478 return *this; | 478 return *this; |
479 } | 479 } |
480 | 480 |
481 template <typename U> const Checked& operator=(U value) | 481 template <typename U> const Checked& operator=(U value) |
482 { | 482 { |
483 return *this = Checked(value); | 483 return *this = Checked(value); |
484 } | 484 } |
485 | 485 |
486 template <typename U, typename V> const Checked& operator=(const Checked<U,
V>& rhs) | 486 template <typename U, typename V> const Checked& operator=(const Checked<U,
V>& rhs) |
487 { | 487 { |
488 return *this = Checked(rhs); | 488 return *this = Checked(rhs); |
489 } | 489 } |
490 | 490 |
491 // prefix | 491 // prefix |
492 const Checked& operator++() | 492 const Checked& operator++() |
493 { | 493 { |
494 if (m_value == std::numeric_limits<T>::max()) | 494 if (m_value == std::numeric_limits<T>::max()) |
495 this->overflowed(); | 495 this->overflowed(); |
496 m_value++; | 496 m_value++; |
497 return *this; | 497 return *this; |
498 } | 498 } |
499 | 499 |
500 const Checked& operator--() | 500 const Checked& operator--() |
501 { | 501 { |
502 if (m_value == std::numeric_limits<T>::min()) | 502 if (m_value == std::numeric_limits<T>::min()) |
503 this->overflowed(); | 503 this->overflowed(); |
504 m_value--; | 504 m_value--; |
505 return *this; | 505 return *this; |
506 } | 506 } |
507 | 507 |
508 // postfix operators | 508 // postfix operators |
509 const Checked operator++(int) | 509 const Checked operator++(int) |
510 { | 510 { |
511 if (m_value == std::numeric_limits<T>::max()) | 511 if (m_value == std::numeric_limits<T>::max()) |
512 this->overflowed(); | 512 this->overflowed(); |
513 return Checked(m_value++); | 513 return Checked(m_value++); |
514 } | 514 } |
515 | 515 |
516 const Checked operator--(int) | 516 const Checked operator--(int) |
517 { | 517 { |
518 if (m_value == std::numeric_limits<T>::min()) | 518 if (m_value == std::numeric_limits<T>::min()) |
519 this->overflowed(); | 519 this->overflowed(); |
520 return Checked(m_value--); | 520 return Checked(m_value--); |
521 } | 521 } |
522 | 522 |
523 // Boolean operators | 523 // Boolean operators |
524 bool operator!() const | 524 bool operator!() const |
525 { | 525 { |
526 if (this->hasOverflowed()) | 526 if (this->hasOverflowed()) |
527 CRASH(); | 527 CRASH(); |
528 return !m_value; | 528 return !m_value; |
529 } | 529 } |
530 | 530 |
531 typedef void* (Checked::*UnspecifiedBoolType); | 531 typedef void* (Checked::*UnspecifiedBoolType); |
532 operator UnspecifiedBoolType*() const | 532 operator UnspecifiedBoolType*() const |
533 { | 533 { |
534 if (this->hasOverflowed()) | 534 if (this->hasOverflowed()) |
535 CRASH(); | 535 CRASH(); |
536 return (m_value) ? reinterpret_cast<UnspecifiedBoolType*>(1) : 0; | 536 return (m_value) ? reinterpret_cast<UnspecifiedBoolType*>(1) : 0; |
537 } | 537 } |
538 | 538 |
539 // Value accessors. unsafeGet() will crash if there's been an overflow. | 539 // Value accessors. unsafeGet() will crash if there's been an overflow. |
540 T unsafeGet() const | 540 T unsafeGet() const |
541 { | 541 { |
542 if (this->hasOverflowed()) | 542 if (this->hasOverflowed()) |
543 CRASH(); | 543 CRASH(); |
544 return m_value; | 544 return m_value; |
545 } | 545 } |
546 | 546 |
547 inline CheckedState safeGet(T& value) const WARN_UNUSED_RETURN | 547 inline CheckedState safeGet(T& value) const WARN_UNUSED_RETURN |
548 { | 548 { |
549 value = m_value; | 549 value = m_value; |
550 if (this->hasOverflowed()) | 550 if (this->hasOverflowed()) |
551 return CheckedState::DidOverflow; | 551 return CheckedState::DidOverflow; |
552 return CheckedState::DidNotOverflow; | 552 return CheckedState::DidNotOverflow; |
553 } | 553 } |
554 | 554 |
555 // Mutating assignment | 555 // Mutating assignment |
556 template <typename U> const Checked operator+=(U rhs) | 556 template <typename U> const Checked operator+=(U rhs) |
(...skipping 24 matching lines...) Expand all Loading... |
581 if (!(std::numeric_limits<T>::min() <= result && std::numeric_limits<T>:
:max() >= result)) | 581 if (!(std::numeric_limits<T>::min() <= result && std::numeric_limits<T>:
:max() >= result)) |
582 this->overflowed(); | 582 this->overflowed(); |
583 m_value = (T)result; | 583 m_value = (T)result; |
584 return *this; | 584 return *this; |
585 } | 585 } |
586 | 586 |
587 const Checked operator*=(float rhs) | 587 const Checked operator*=(float rhs) |
588 { | 588 { |
589 return *this *= (double)rhs; | 589 return *this *= (double)rhs; |
590 } | 590 } |
591 | 591 |
592 template <typename U, typename V> const Checked operator+=(Checked<U, V> rhs
) | 592 template <typename U, typename V> const Checked operator+=(Checked<U, V> rhs
) |
593 { | 593 { |
594 if (rhs.hasOverflowed()) | 594 if (rhs.hasOverflowed()) |
595 this->overflowed(); | 595 this->overflowed(); |
596 return *this += rhs.m_value; | 596 return *this += rhs.m_value; |
597 } | 597 } |
598 | 598 |
599 template <typename U, typename V> const Checked operator-=(Checked<U, V> rhs
) | 599 template <typename U, typename V> const Checked operator-=(Checked<U, V> rhs
) |
600 { | 600 { |
601 if (rhs.hasOverflowed()) | 601 if (rhs.hasOverflowed()) |
(...skipping 13 matching lines...) Expand all Loading... |
615 { | 615 { |
616 return unsafeGet() == rhs.unsafeGet(); | 616 return unsafeGet() == rhs.unsafeGet(); |
617 } | 617 } |
618 | 618 |
619 template <typename U> bool operator==(U rhs) | 619 template <typename U> bool operator==(U rhs) |
620 { | 620 { |
621 if (this->hasOverflowed()) | 621 if (this->hasOverflowed()) |
622 this->overflowed(); | 622 this->overflowed(); |
623 return safeEquals(m_value, rhs); | 623 return safeEquals(m_value, rhs); |
624 } | 624 } |
625 | 625 |
626 template <typename U, typename V> const Checked operator==(Checked<U, V> rhs
) | 626 template <typename U, typename V> const Checked operator==(Checked<U, V> rhs
) |
627 { | 627 { |
628 return unsafeGet() == Checked(rhs.unsafeGet()); | 628 return unsafeGet() == Checked(rhs.unsafeGet()); |
629 } | 629 } |
630 | 630 |
631 template <typename U> bool operator!=(U rhs) | 631 template <typename U> bool operator!=(U rhs) |
632 { | 632 { |
633 return !(*this == rhs); | 633 return !(*this == rhs); |
634 } | 634 } |
635 | 635 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 return Checked<U, OverflowHandler>(lhs) * rhs; | 712 return Checked<U, OverflowHandler>(lhs) * rhs; |
713 } | 713 } |
714 | 714 |
715 } | 715 } |
716 | 716 |
717 using WTF::Checked; | 717 using WTF::Checked; |
718 using WTF::CheckedState; | 718 using WTF::CheckedState; |
719 using WTF::RecordOverflow; | 719 using WTF::RecordOverflow; |
720 | 720 |
721 #endif | 721 #endif |
OLD | NEW |