OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 namespace WTF { | 5 namespace WTF { |
6 | 6 |
7 template<typename To, typename From> | 7 template<typename To, typename From> |
8 bool isInBounds(From value) { | 8 bool isInBounds(From value) { |
9 return true; | 9 return true; |
10 } | 10 } |
11 | 11 |
12 template<typename To, typename From> | 12 template<typename To, typename From> |
13 To safeCast(From value) { | 13 To safeCast(From value) { |
14 if (!isInBounds<To>(value)) | 14 if (!isInBounds<To>(value)) |
15 return 0; | 15 return 0; |
16 return static_cast<To>(value); | 16 return static_cast<To>(value); |
17 } | 17 } |
18 | 18 |
19 template<typename T, typename OverflowHandler> | 19 template<typename T, typename OverflowHandler> |
20 class Checked { | 20 class Checked { |
21 public: | 21 public: |
22 template<typename U, typename V> | 22 template<typename U, typename V> |
23 Checked(const Checked<U, V>& rhs){ | 23 Checked(const Checked<U, V>& rhs){ |
24 // This (incorrectly) doesn't get rewritten, since it's not instantiated. In | |
25 // this case, the AST representation contains a bunch of | |
26 // CXXDependentScopeMemberExpr nodes. | |
27 if (rhs.hasOverflowed()) | 24 if (rhs.hasOverflowed()) |
28 this->overflowed(); | 25 this->overflowed(); |
29 if (!isInBounds<T>(rhs.m_value)) | 26 if (!isInBounds<T>(rhs.m_value)) |
30 this->overflowed(); | 27 this->overflowed(); |
31 m_value = static_cast<T>(rhs.m_value); | 28 m_value = static_cast<T>(rhs.m_value); |
32 } | 29 } |
33 | 30 |
34 bool hasOverflowed() const { return false; } | 31 bool hasOverflowed() const { return false; } |
35 void overflowed() { } | 32 void overflowed() { } |
36 | 33 |
37 private: | 34 private: |
38 T m_value; | 35 T m_value; |
39 }; | 36 }; |
40 | 37 |
41 template<typename To, typename From> | 38 template<typename To, typename From> |
42 To bitwise_cast(From from) { | 39 To bitwise_cast(From from) { |
43 static_assert(sizeof(To) == sizeof(From)); | 40 static_assert(sizeof(To) == sizeof(From), "msg"); |
44 return reinterpret_cast<To>(from); | 41 return reinterpret_cast<To>(from); |
45 } | 42 } |
46 | 43 |
47 } // namespace WTF | 44 } // namespace WTF |
48 | 45 |
| 46 namespace mojo { |
| 47 |
| 48 template <typename U> |
| 49 struct ArrayTraits; |
| 50 |
| 51 template <typename U> |
| 52 struct ArrayTraits<WTF::Checked<U, int>> { |
| 53 static bool HasOverflowed(WTF::Checked<U, int>& input) { |
| 54 // |hasOverflowed| below should be rewritten to |HasOverflowed| |
| 55 // (because this is a method of WTF::Checked; it doesn't matter |
| 56 // that we are not in WTF namespace *here*). |
| 57 return input.hasOverflowed(); |
| 58 } |
| 59 }; |
| 60 |
| 61 } // namespace mojo |
| 62 |
49 using WTF::bitwise_cast; | 63 using WTF::bitwise_cast; |
50 using WTF::safeCast; | 64 using WTF::safeCast; |
OLD | NEW |