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

Side by Side Diff: third_party/WebKit/Source/platform/LayoutUnit.h

Issue 2261663002: Disallow cast/implicit conversion from LayoutUnit to int/unsigned (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: - Created 4 years, 3 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
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012, Google Inc. All rights reserved. 2 * Copyright (c) 2012, Google 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 are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 { 100 {
101 float floatValue = toFloat(); 101 float floatValue = toFloat();
102 if (static_cast<int>(floatValue * kFixedPointDenominator) == m_value) 102 if (static_cast<int>(floatValue * kFixedPointDenominator) == m_value)
103 return floatValue; 103 return floatValue;
104 if (floatValue > 0) 104 if (floatValue > 0)
105 return nextafterf(floatValue, std::numeric_limits<float>::max()); 105 return nextafterf(floatValue, std::numeric_limits<float>::max());
106 return nextafterf(floatValue, std::numeric_limits<float>::min()); 106 return nextafterf(floatValue, std::numeric_limits<float>::min());
107 } 107 }
108 unsigned toUnsigned() const { REPORT_OVERFLOW(m_value >= 0); return toInt(); } 108 unsigned toUnsigned() const { REPORT_OVERFLOW(m_value >= 0); return toInt(); }
109 109
110 operator int() const { return toInt(); } 110 // Conversion to int or unsigned is lossy. 'explicit' on these operators won 't work because
111 operator unsigned() const { return toUnsigned(); } 111 // there are also other implicit conversion paths (e.g. operator bool then t o int which would
112 // generate wrong result). Use toInt() and toUnsigned() instead.
113 operator int() const = delete;
114 operator unsigned() const = delete;
115
112 operator double() const { return toDouble(); } 116 operator double() const { return toDouble(); }
113 operator float() const { return toFloat(); } 117 operator float() const { return toFloat(); }
114 operator bool() const { return m_value; } 118 operator bool() const { return m_value; }
115 119
116 LayoutUnit operator++(int) 120 LayoutUnit operator++(int)
117 { 121 {
118 m_value += kFixedPointDenominator; 122 m_value += kFixedPointDenominator;
119 return *this; 123 return *this;
120 } 124 }
121 125
(...skipping 689 matching lines...) Expand 10 before | Expand all | Expand 10 after
811 } 815 }
812 816
813 inline std::ostream& operator<<(std::ostream& stream, const LayoutUnit& value) 817 inline std::ostream& operator<<(std::ostream& stream, const LayoutUnit& value)
814 { 818 {
815 return stream << value.toString(); 819 return stream << value.toString();
816 } 820 }
817 821
818 } // namespace blink 822 } // namespace blink
819 823
820 #endif // LayoutUnit_h 824 #endif // LayoutUnit_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/testing/Internals.cpp ('k') | third_party/WebKit/Source/platform/LengthFunctions.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698