OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Defines a simple integer rectangle class. The containment semantics | 5 // Defines a simple integer rectangle class. The containment semantics |
6 // are array-like; that is, the coordinate (x, y) is considered to be | 6 // are array-like; that is, the coordinate (x, y) is considered to be |
7 // contained by the rectangle, but the coordinate (x + width, y) is not. | 7 // contained by the rectangle, but the coordinate (x + width, y) is not. |
8 // The class will happily let you create malformed rectangles (that is, | 8 // The class will happily let you create malformed rectangles (that is, |
9 // rectangles with negative width and/or height), but there will be assertions | 9 // rectangles with negative width and/or height), but there will be assertions |
10 // in the operations (such as Contains()) to complain in this case. | 10 // in the operations (such as Contains()) to complain in this case. |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 int ManhattanInternalDistance(const Rect& rect) const; | 199 int ManhattanInternalDistance(const Rect& rect) const; |
200 | 200 |
201 std::string ToString() const; | 201 std::string ToString() const; |
202 | 202 |
203 bool ApproximatelyEqual(const Rect& rect, int tolerance) const; | 203 bool ApproximatelyEqual(const Rect& rect, int tolerance) const; |
204 | 204 |
205 private: | 205 private: |
206 gfx::Point origin_; | 206 gfx::Point origin_; |
207 gfx::Size size_; | 207 gfx::Size size_; |
208 | 208 |
| 209 // Returns true iff a+b would overflow max int. |
| 210 static constexpr bool AddWouldOverflow(int a, int b) { |
| 211 // In this function, GCC tries to make optimizations that would only work if |
| 212 // max - a wouldn't overflow but it isn't smart enough to notice that a > 0. |
| 213 // So cast everything to unsigned to avoid this. As it is guaranteed that |
| 214 // max - a and b are both already positive, the cast is a noop. |
| 215 // |
| 216 // This is intended to be: a > 0 && max - a < b |
| 217 return a > 0 && b > 0 && |
| 218 static_cast<unsigned>(std::numeric_limits<int>::max() - a) < |
| 219 static_cast<unsigned>(b); |
| 220 } |
| 221 |
209 // Clamp the size to avoid integer overflow in bottom() and right(). | 222 // Clamp the size to avoid integer overflow in bottom() and right(). |
210 // This returns the width given an origin and a width. | 223 // This returns the width given an origin and a width. |
| 224 // TODO(enne): this should probably use base::SaturatedAddition, but that |
| 225 // function is not a constexpr. |
211 static constexpr int GetClampedValue(int origin, int size) { | 226 static constexpr int GetClampedValue(int origin, int size) { |
212 return AddWouldOverflow(origin, size) | 227 return AddWouldOverflow(origin, size) |
213 ? std::numeric_limits<int>::max() - origin | 228 ? std::numeric_limits<int>::max() - origin |
214 : size; | 229 : size; |
215 } | 230 } |
216 | |
217 // Returns a clamped width given a right and a left, assuming right > left. | |
218 static constexpr int GetClampedWidthFromExtents(int left, int right) { | |
219 return SubtractWouldOverflow(right, left) ? std::numeric_limits<int>::max() | |
220 : right - left; | |
221 } | |
222 }; | 231 }; |
223 | 232 |
224 inline bool operator==(const Rect& lhs, const Rect& rhs) { | 233 inline bool operator==(const Rect& lhs, const Rect& rhs) { |
225 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size(); | 234 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size(); |
226 } | 235 } |
227 | 236 |
228 inline bool operator!=(const Rect& lhs, const Rect& rhs) { | 237 inline bool operator!=(const Rect& lhs, const Rect& rhs) { |
229 return !(lhs == rhs); | 238 return !(lhs == rhs); |
230 } | 239 } |
231 | 240 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 } | 335 } |
327 | 336 |
328 // This is declared here for use in gtest-based unit tests but is defined in | 337 // This is declared here for use in gtest-based unit tests but is defined in |
329 // the //ui/gfx:test_support target. Depend on that to use this in your unit | 338 // the //ui/gfx:test_support target. Depend on that to use this in your unit |
330 // test. This should not be used in production code - call ToString() instead. | 339 // test. This should not be used in production code - call ToString() instead. |
331 void PrintTo(const Rect& rect, ::std::ostream* os); | 340 void PrintTo(const Rect& rect, ::std::ostream* os); |
332 | 341 |
333 } // namespace gfx | 342 } // namespace gfx |
334 | 343 |
335 #endif // UI_GFX_GEOMETRY_RECT_H_ | 344 #endif // UI_GFX_GEOMETRY_RECT_H_ |
OLD | NEW |