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

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

Issue 876873003: Add float version of gfx::Range (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: newline at EOF Created 5 years, 10 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 | « no previous file | ui/gfx/range/range.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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
Robert Sesek 2015/02/02 21:42:11 The copyright year should actually remain the same
ckocagil 2015/02/03 00:51:05 Done.
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_H_ 5 #ifndef UI_GFX_RANGE_RANGE_H_
6 #define UI_GFX_RANGE_RANGE_H_ 6 #define UI_GFX_RANGE_RANGE_H_
7 7
8 #include <algorithm>
8 #include <ostream> 9 #include <ostream>
10 #include <sstream>
9 #include <string> 11 #include <string>
10 12
11 #include "base/basictypes.h" 13 #include "base/basictypes.h"
12 #include "ui/gfx/gfx_export.h" 14 #include "ui/gfx/gfx_export.h"
13 15
14 #if defined(OS_MACOSX) 16 #if defined(OS_MACOSX)
15 #if __OBJC__ 17 #if __OBJC__
16 #import <Foundation/Foundation.h> 18 #import <Foundation/Foundation.h>
17 #else 19 #else
18 typedef struct _NSRange NSRange; 20 typedef struct _NSRange NSRange;
19 #endif 21 #endif
20 #endif // defined(OS_MACOSX) 22 #endif // defined(OS_MACOSX)
21 23
22 #if defined(OS_WIN) 24 #if defined(OS_WIN)
23 #include <windows.h> 25 #include <windows.h>
24 #include <richedit.h> 26 #include <richedit.h>
25 #endif 27 #endif
26 28
27 namespace gfx { 29 namespace gfx {
28 30
31 namespace internal {
32
33 template <typename T>
34 class TRangeBase;
35
36 template <typename T>
37 TRangeBase<T> InvalidRange();
38
39 template <>
40 TRangeBase<size_t> InvalidRange<size_t>();
41
42 template <>
43 TRangeBase<float> InvalidRange<float>();
44
45 bool IsValidRange(const TRangeBase<size_t>& range);
46 bool IsValidRange(const TRangeBase<float>& range);
47
48 size_t RangeLength(const TRangeBase<size_t>& range);
49 float RangeLength(const TRangeBase<float>& range);
50
51 bool RangeIsEmpty(const TRangeBase<size_t>& range);
52 bool RangeIsEmpty(const TRangeBase<float>& range);
53
54 template <typename T>
55 class TRangeBase {
56 public:
57 // Initializes the range with a start and end.
58 TRangeBase(T start, T end) : start_(start), end_(end) {}
59
60 TRangeBase(const TRangeBase<T>& other)
61 : start_(other.start()), end_(other.end()) {}
62
63 // Returns a range that is invalid.
64 static const TRangeBase<T> InvalidRange() {
65 return internal::InvalidRange<T>();
Robert Sesek 2015/02/02 21:42:11 Don't need internal:: since this is in the namespa
ckocagil 2015/02/03 00:51:05 In this case we actually need it since TRangeBase:
66 }
67
68 // Checks if the range is valid.
69 bool IsValid() const {
70 return internal::IsValidRange(*this);
71 }
72
73 // Getters and setters.
74 T start() const { return start_; }
75 void set_start(T start) { start_ = start; }
76
77 T end() const { return end_; }
78 void set_end(T end) { end_ = end; }
79
80 // Returns the absolute value of the length.
81 T length() const {
82 return internal::RangeLength(*this);
83 }
84
85 bool is_reversed() const { return start_ > end_; }
86 bool is_empty() const {
87 return internal::RangeIsEmpty(*this);
88 }
89
90 // Returns the minimum and maximum values.
91 T GetMin() const { return std::min(start_, end_); }
92 T GetMax() const { return std::max(start_, end_); }
93
94 bool operator==(const TRangeBase& other) const {
95 return start_ == other.start() && end_ == other.end();
Robert Sesek 2015/02/02 21:42:11 Use other.start_ and other.end_ for consistency.
ckocagil 2015/02/03 00:51:05 Done.
96 }
97 bool operator!=(const TRangeBase& other) const {
98 return !(*this == other);
99 }
100 bool EqualsIgnoringDirection(const TRangeBase& other) const {
101 return GetMin() == other.GetMin() && GetMax() == other.GetMax();
102 }
103
104 // Returns true if this range intersects the specified |range|.
105 bool Intersects(const TRangeBase& range) const {
106 return IsValid() && range.IsValid() &&
107 !(range.GetMax() < GetMin() || range.GetMin() >= GetMax());
108 }
109
110 // Returns true if this range contains the specified |range|.
111 bool Contains(const TRangeBase& range) const {
112 return IsValid() && range.IsValid() &&
113 GetMin() <= range.GetMin() && range.GetMax() <= GetMax();
114 }
115
116 // Computes the intersection of this range with the given |range|.
117 // If they don't intersect, it returns an InvalidRange().
118 // The returned range is always empty or forward (never reversed).
119 TRangeBase Intersect(const TRangeBase& range) const {
120 T min = std::max(GetMin(), range.GetMin());
121 T max = std::min(GetMax(), range.GetMax());
122
123 if (min >= max) // No intersection.
124 return InvalidRange();
125
126 return TRangeBase(min, max);
127 }
128
129 std::string ToString() const {
130 std::ostringstream stream;
131 stream << "{" << start_ << "," << end_ << "}";
132 return stream.str();
133 }
134
135 private:
136 T start_;
137 T end_;
138 };
139
140 } // namespace internal
141
29 // A Range contains two integer values that represent a numeric range, like the 142 // A Range contains two integer values that represent a numeric range, like the
30 // range of characters in a text selection. A range is made of a start and end 143 // range of characters in a text selection. A range is made of a start and end
31 // position; when they are the same, the Range is akin to a caret. Note that 144 // position; when they are the same, the Range is akin to a caret. Note that
32 // |start_| can be greater than |end_| to respect the directionality of the 145 // |start_| can be greater than |end_| to respect the directionality of the
33 // range. 146 // range.
34 class GFX_EXPORT Range { 147 // TODO(ckocagil): Move these constructors to TRangeBase and use delegated
148 // constructors (C++11) after the feature is allowed.
149 class GFX_EXPORT Range : public internal::TRangeBase<size_t> {
35 public: 150 public:
36 // Creates an empty range {0,0}. 151 // Creates an empty range {0,0}.
37 Range(); 152 Range();
38 153
39 // Initializes the range with a start and end. 154 // Initializes the range with a start and end.
40 Range(size_t start, size_t end); 155 Range(size_t start, size_t end);
41 156
157 Range(const internal::TRangeBase<size_t>& other);
Robert Sesek 2015/02/02 21:42:11 Do you have to use the internal name here, or can
ckocagil 2015/02/03 00:51:05 We need to construct a Range from a TRangeBase her
158
42 // Initializes the range with the same start and end positions. 159 // Initializes the range with the same start and end positions.
43 explicit Range(size_t position); 160 explicit Range(size_t position);
44 161
45 // Platform constructors. 162 // Platform constructors.
46 #if defined(OS_MACOSX) 163 #if defined(OS_MACOSX)
47 explicit Range(const NSRange& range); 164 explicit Range(const NSRange& range);
48 #elif defined(OS_WIN) 165 #elif defined(OS_WIN)
49 // The |total_length| paramater should be used if the CHARRANGE is set to 166 // The |total_length| paramater should be used if the CHARRANGE is set to
50 // {0,-1} to indicate the whole range. 167 // {0,-1} to indicate the whole range.
51 Range(const CHARRANGE& range, LONG total_length = -1); 168 Range(const CHARRANGE& range, LONG total_length = -1);
52 #endif 169 #endif
53 170
54 // Returns a range that is invalid, which is {size_t_max,size_t_max}.
55 static const Range InvalidRange();
56
57 // Checks if the range is valid through comparision to InvalidRange().
58 bool IsValid() const;
59
60 // Getters and setters.
61 size_t start() const { return start_; }
62 void set_start(size_t start) { start_ = start; }
63
64 size_t end() const { return end_; }
65 void set_end(size_t end) { end_ = end; }
66
67 // Returns the absolute value of the length.
68 size_t length() const {
69 ptrdiff_t length = end() - start();
70 return length >= 0 ? length : -length;
71 }
72
73 bool is_reversed() const { return start() > end(); }
74 bool is_empty() const { return start() == end(); }
75
76 // Returns the minimum and maximum values.
77 size_t GetMin() const;
78 size_t GetMax() const;
79
80 bool operator==(const Range& other) const;
81 bool operator!=(const Range& other) const;
82 bool EqualsIgnoringDirection(const Range& other) const;
83
84 // Returns true if this range intersects the specified |range|.
85 bool Intersects(const Range& range) const;
86
87 // Returns true if this range contains the specified |range|.
88 bool Contains(const Range& range) const;
89
90 // Computes the intersection of this range with the given |range|.
91 // If they don't intersect, it returns an InvalidRange().
92 // The returned range is always empty or forward (never reversed).
93 Range Intersect(const Range& range) const;
94
95 #if defined(OS_MACOSX) 171 #if defined(OS_MACOSX)
96 Range& operator=(const NSRange& range); 172 Range& operator=(const NSRange& range);
97 173
98 // NSRange does not store the directionality of a range, so if this 174 // NSRange does not store the directionality of a range, so if this
99 // is_reversed(), the range will get flipped when converted to an NSRange. 175 // is_reversed(), the range will get flipped when converted to an NSRange.
100 NSRange ToNSRange() const; 176 NSRange ToNSRange() const;
101 #elif defined(OS_WIN) 177 #elif defined(OS_WIN)
102 CHARRANGE ToCHARRANGE() const; 178 CHARRANGE ToCHARRANGE() const;
103 #endif 179 #endif
104 // GTK+ has no concept of a range. 180 // GTK+ has no concept of a range.
181 };
105 182
106 std::string ToString() const; 183 // A RangeF contains two float values that represent a numeric range, like a
184 // range of positions on a line. A RangeF is made of a start and end position;
185 // when they are the same, the RangeF is considered empty. Note that |start_|
186 // can be greater than |end_| to respect the directionality of the range.
187 class GFX_EXPORT RangeF : public internal::TRangeBase<float> {
188 public:
189 // Creates an empty range {0,0}.
190 RangeF();
107 191
108 private: 192 // Initializes the range with a start and end.
109 size_t start_; 193 RangeF(float start, float end);
110 size_t end_; 194
195 RangeF(const internal::TRangeBase<float>& other);
Robert Sesek 2015/02/02 21:42:11 Same.
196
197 // Initializes the range with the same start and end positions.
198 explicit RangeF(float position);
111 }; 199 };
112 200
113 GFX_EXPORT std::ostream& operator<<(std::ostream& os, const Range& range); 201 GFX_EXPORT std::ostream& operator<<(std::ostream& os, const Range& range);
202 GFX_EXPORT std::ostream& operator<<(std::ostream& os, const RangeF& range);
114 203
115 } // namespace gfx 204 } // namespace gfx
116 205
117 #endif // UI_GFX_RANGE_RANGE_H_ 206 #endif // UI_GFX_RANGE_RANGE_H_
OLDNEW
« no previous file with comments | « no previous file | ui/gfx/range/range.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698