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

Side by Side Diff: ui/gfx/range/range_f.h

Issue 2734663002: Make much of gfx::Range[F] constexpr. (Closed)
Patch Set: size_t Created 3 years, 9 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
« no previous file with comments | « ui/gfx/range/range.cc ('k') | ui/gfx/range/range_f.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 #ifndef UI_GFX_RANGE_RANGE_F_H_ 5 #ifndef UI_GFX_RANGE_RANGE_F_H_
6 #define UI_GFX_RANGE_RANGE_F_H_ 6 #define UI_GFX_RANGE_RANGE_F_H_
7 7
8 #include <limits>
8 #include <ostream> 9 #include <ostream>
9 #include <string> 10 #include <string>
10 11
11 #include "ui/gfx/range/gfx_range_export.h" 12 #include "ui/gfx/range/gfx_range_export.h"
12 #include "ui/gfx/range/range.h" 13 #include "ui/gfx/range/range.h"
13 14
14 namespace gfx { 15 namespace gfx {
15 16
16 // A float version of Range. RangeF is made of a start and end position; when 17 // A float version of Range. RangeF is made of a start and end position; when
17 // they are the same, the range is empty. Note that |start_| can be greater 18 // they are the same, the range is empty. Note that |start_| can be greater
18 // than |end_| to respect the directionality of the range. 19 // than |end_| to respect the directionality of the range.
19 class GFX_RANGE_EXPORT RangeF { 20 class GFX_RANGE_EXPORT RangeF {
20 public: 21 public:
21 // Creates an empty range {0,0}. 22 // Creates an empty range {0,0}.
22 RangeF(); 23 constexpr RangeF() : RangeF(0.f) {}
23 24
24 // Initializes the range with a start and end. 25 // Initializes the range with a start and end.
25 RangeF(float start, float end); 26 constexpr RangeF(float start, float end) : start_(start), end_(end) {}
26 27
27 // Initializes the range with the same start and end positions. 28 // Initializes the range with the same start and end positions.
28 explicit RangeF(float position); 29 constexpr explicit RangeF(float position) : RangeF(position, position) {}
29 30
30 // Returns a range that is invalid, which is {float_max,float_max}. 31 // Returns a range that is invalid, which is {float_max,float_max}.
31 static const RangeF InvalidRange(); 32 static constexpr RangeF InvalidRange() {
33 return RangeF(std::numeric_limits<float>::max());
34 }
32 35
33 // Checks if the range is valid through comparison to InvalidRange(). 36 // Checks if the range is valid through comparison to InvalidRange().
34 bool IsValid() const; 37 constexpr bool IsValid() const { return *this != InvalidRange(); }
35 38
36 // Getters and setters. 39 // Getters and setters.
37 float start() const { return start_; } 40 constexpr float start() const { return start_; }
38 void set_start(float start) { start_ = start; } 41 void set_start(float start) { start_ = start; }
39 42
40 float end() const { return end_; } 43 constexpr float end() const { return end_; }
41 void set_end(float end) { end_ = end; } 44 void set_end(float end) { end_ = end; }
42 45
43 // Returns the absolute value of the length. 46 // Returns the absolute value of the length.
44 float length() const { 47 constexpr float length() const { return GetMax() - GetMin(); }
45 const float length = end() - start(); 48
46 return length >= 0 ? length : -length; 49 constexpr bool is_reversed() const { return start() > end(); }
50 constexpr bool is_empty() const { return start() == end(); }
51
52 // Returns the minimum and maximum values.
53 constexpr float GetMin() const { return start() < end() ? start() : end(); }
54 constexpr float GetMax() const { return start() > end() ? start() : end(); }
55
56 constexpr bool operator==(const RangeF& other) const {
57 return start() == other.start() && end() == other.end();
58 }
59 constexpr bool operator!=(const RangeF& other) const {
60 return !(*this == other);
61 }
62 constexpr bool EqualsIgnoringDirection(const RangeF& other) const {
63 return GetMin() == other.GetMin() && GetMax() == other.GetMax();
47 } 64 }
48 65
49 bool is_reversed() const { return start() > end(); }
50 bool is_empty() const { return start() == end(); }
51
52 // Returns the minimum and maximum values.
53 float GetMin() const;
54 float GetMax() const;
55
56 bool operator==(const RangeF& other) const;
57 bool operator!=(const RangeF& other) const;
58 bool EqualsIgnoringDirection(const RangeF& other) const;
59
60 // Returns true if this range intersects the specified |range|. 66 // Returns true if this range intersects the specified |range|.
61 bool Intersects(const RangeF& range) const; 67 constexpr bool Intersects(const RangeF& range) const {
68 return IsValid() && range.IsValid() &&
69 !(range.GetMax() < GetMin() || range.GetMin() >= GetMax());
70 }
62 71
63 // Returns true if this range contains the specified |range|. 72 // Returns true if this range contains the specified |range|.
64 bool Contains(const RangeF& range) const; 73 constexpr bool Contains(const RangeF& range) const {
74 return IsValid() && range.IsValid() && GetMin() <= range.GetMin() &&
75 range.GetMax() <= GetMax();
76 }
65 77
66 // Computes the intersection of this range with the given |range|. 78 // Computes the intersection of this range with the given |range|.
67 // If they don't intersect, it returns an InvalidRange(). 79 // If they don't intersect, it returns an InvalidRange().
68 // The returned range is always empty or forward (never reversed). 80 // The returned range is always empty or forward (never reversed).
69 RangeF Intersect(const RangeF& range) const; 81 RangeF Intersect(const RangeF& range) const;
70 RangeF Intersect(const Range& range) const; 82 RangeF Intersect(const Range& range) const;
71 83
72 // Floor/Ceil/Round the start and end values of the given RangeF. 84 // Floor/Ceil/Round the start and end values of the given RangeF.
73 Range Floor() const; 85 Range Floor() const;
74 Range Ceil() const; 86 Range Ceil() const;
75 Range Round() const; 87 Range Round() const;
76 88
77 std::string ToString() const; 89 std::string ToString() const;
78 90
79 private: 91 private:
80 float start_; 92 float start_;
81 float end_; 93 float end_;
82 }; 94 };
83 95
84 GFX_RANGE_EXPORT std::ostream& operator<<(std::ostream& os, 96 GFX_RANGE_EXPORT std::ostream& operator<<(std::ostream& os,
85 const RangeF& range); 97 const RangeF& range);
86 98
87 } // namespace gfx 99 } // namespace gfx
88 100
89 #endif // UI_GFX_RANGE_RANGE_F_H_ 101 #endif // UI_GFX_RANGE_RANGE_F_H_
OLDNEW
« no previous file with comments | « ui/gfx/range/range.cc ('k') | ui/gfx/range/range_f.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698