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 3 // found in the LICENSE file. 4 5 namespace WTF { 6 7 template <typename To, typename From> IsInBounds(From value)8bool IsInBounds(From value) { 9 return true; 10 } 11 12 template <typename To, typename From> SafeCast(From value)13To SafeCast(From value) { 14 if (!IsInBounds<To>(value)) 15 return 0; 16 return static_cast<To>(value); 17 } 18 19 template <typename T, typename OverflowHandler> 20 class Checked { 21 public: 22 template <typename U, typename V> 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()) 28 this->overflowed(); 29 if (!IsInBounds<T>(rhs.m_value)) 30 this->overflowed(); 31 value_ = static_cast<T>(rhs.m_value); 32 } 33 HasOverflowed() const34 bool HasOverflowed() const { return false; } Overflowed()35 void Overflowed() {} 36 37 private: 38 T value_; 39 }; 40 41 template <typename To, typename From> Bitwise_cast(From from)42To Bitwise_cast(From from) { 43 static_assert(sizeof(To) == sizeof(From)); 44 return reinterpret_cast<To>(from); 45 } 46 47 } // namespace WTF 48 49 using WTF::Bitwise_cast; 50 using WTF::SafeCast; 51