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 #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 <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
| 11 #include <limits> |
11 #include <ostream> | 12 #include <ostream> |
12 #include <string> | 13 #include <string> |
13 | 14 |
14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
15 #include "ui/gfx/range/gfx_range_export.h" | 16 #include "ui/gfx/range/gfx_range_export.h" |
16 | 17 |
17 #if defined(OS_MACOSX) | 18 #if defined(OS_MACOSX) |
18 #if __OBJC__ | 19 #if __OBJC__ |
19 #import <Foundation/Foundation.h> | 20 #import <Foundation/Foundation.h> |
20 #else | 21 #else |
21 typedef struct _NSRange NSRange; | 22 typedef struct _NSRange NSRange; |
22 #endif | 23 #endif |
23 #endif // defined(OS_MACOSX) | 24 #endif // defined(OS_MACOSX) |
24 | 25 |
25 #if defined(OS_WIN) | 26 #if defined(OS_WIN) |
26 #include <windows.h> | 27 #include <windows.h> |
27 #include <richedit.h> | 28 #include <richedit.h> |
28 #endif | 29 #endif |
29 | 30 |
30 namespace gfx { | 31 namespace gfx { |
31 | 32 |
32 // A Range contains two integer values that represent a numeric range, like the | 33 // A Range contains two integer values that represent a numeric range, like the |
33 // range of characters in a text selection. A range is made of a start and end | 34 // range of characters in a text selection. A range is made of a start and end |
34 // position; when they are the same, the Range is akin to a caret. Note that | 35 // position; when they are the same, the Range is akin to a caret. Note that |
35 // |start_| can be greater than |end_| to respect the directionality of the | 36 // |start_| can be greater than |end_| to respect the directionality of the |
36 // range. | 37 // range. |
37 class GFX_RANGE_EXPORT Range { | 38 class GFX_RANGE_EXPORT Range { |
38 public: | 39 public: |
39 // Creates an empty range {0,0}. | 40 // Creates an empty range {0,0}. |
40 Range(); | 41 constexpr Range() : Range(0) {} |
41 | 42 |
42 // Initializes the range with a start and end. | 43 // Initializes the range with a start and end. |
43 Range(uint32_t start, uint32_t end); | 44 constexpr Range(uint32_t start, uint32_t end) : start_(start), end_(end) {} |
44 | 45 |
45 // Initializes the range with the same start and end positions. | 46 // Initializes the range with the same start and end positions. |
46 explicit Range(uint32_t position); | 47 constexpr explicit Range(uint32_t position) : Range(position, position) {} |
47 | 48 |
48 // Platform constructors. | 49 // Platform constructors. |
49 #if defined(OS_MACOSX) | 50 #if defined(OS_MACOSX) |
50 explicit Range(const NSRange& range); | 51 explicit Range(const NSRange& range); |
51 #elif defined(OS_WIN) | 52 #elif defined(OS_WIN) |
52 // The |total_length| paramater should be used if the CHARRANGE is set to | 53 // The |total_length| paramater should be used if the CHARRANGE is set to |
53 // {0,-1} to indicate the whole range. | 54 // {0,-1} to indicate the whole range. |
54 Range(const CHARRANGE& range, LONG total_length = -1); | 55 Range(const CHARRANGE& range, LONG total_length = -1); |
55 #endif | 56 #endif |
56 | 57 |
57 // Returns a range that is invalid, which is {UINT32_MAX,UINT32_MAX}. | 58 // Returns a range that is invalid, which is {UINT32_MAX,UINT32_MAX}. |
58 static const Range InvalidRange(); | 59 static constexpr Range InvalidRange() { |
| 60 return Range(std::numeric_limits<uint32_t>::max()); |
| 61 } |
59 | 62 |
60 // Checks if the range is valid through comparison to InvalidRange(). | 63 // Checks if the range is valid through comparison to InvalidRange(). |
61 bool IsValid() const; | 64 constexpr bool IsValid() const { return *this != InvalidRange(); } |
62 | 65 |
63 // Getters and setters. | 66 // Getters and setters. |
64 uint32_t start() const { return start_; } | 67 constexpr uint32_t start() const { return start_; } |
65 void set_start(uint32_t start) { start_ = start; } | 68 void set_start(uint32_t start) { start_ = start; } |
66 | 69 |
67 uint32_t end() const { return end_; } | 70 constexpr uint32_t end() const { return end_; } |
68 void set_end(uint32_t end) { end_ = end; } | 71 void set_end(uint32_t end) { end_ = end; } |
69 | 72 |
70 // Returns the absolute value of the length. | 73 // Returns the absolute value of the length. |
71 uint32_t length() const { | 74 constexpr uint32_t length() const { return GetMax() - GetMin(); } |
72 return GetMax() - GetMin(); | 75 |
| 76 constexpr bool is_reversed() const { return start() > end(); } |
| 77 constexpr bool is_empty() const { return start() == end(); } |
| 78 |
| 79 // Returns the minimum and maximum values. |
| 80 constexpr uint32_t GetMin() const { |
| 81 return start() < end() ? start() : end(); |
| 82 } |
| 83 constexpr uint32_t GetMax() const { |
| 84 return start() > end() ? start() : end(); |
73 } | 85 } |
74 | 86 |
75 bool is_reversed() const { return start() > end(); } | 87 constexpr bool operator==(const Range& other) const { |
76 bool is_empty() const { return start() == end(); } | 88 return start() == other.start() && end() == other.end(); |
77 | 89 } |
78 // Returns the minimum and maximum values. | 90 constexpr bool operator!=(const Range& other) const { |
79 uint32_t GetMin() const; | 91 return !(*this == other); |
80 uint32_t GetMax() const; | 92 } |
81 | 93 constexpr bool EqualsIgnoringDirection(const Range& other) const { |
82 bool operator==(const Range& other) const; | 94 return GetMin() == other.GetMin() && GetMax() == other.GetMax(); |
83 bool operator!=(const Range& other) const; | 95 } |
84 bool EqualsIgnoringDirection(const Range& other) const; | |
85 | 96 |
86 // Returns true if this range intersects the specified |range|. | 97 // Returns true if this range intersects the specified |range|. |
87 bool Intersects(const Range& range) const; | 98 constexpr bool Intersects(const Range& range) const { |
| 99 return IsValid() && range.IsValid() && |
| 100 !(range.GetMax() < GetMin() || range.GetMin() >= GetMax()); |
| 101 } |
88 | 102 |
89 // Returns true if this range contains the specified |range|. | 103 // Returns true if this range contains the specified |range|. |
90 bool Contains(const Range& range) const; | 104 constexpr bool Contains(const Range& range) const { |
| 105 return IsValid() && range.IsValid() && GetMin() <= range.GetMin() && |
| 106 range.GetMax() <= GetMax(); |
| 107 } |
91 | 108 |
92 // Computes the intersection of this range with the given |range|. | 109 // Computes the intersection of this range with the given |range|. |
93 // If they don't intersect, it returns an InvalidRange(). | 110 // If they don't intersect, it returns an InvalidRange(). |
94 // The returned range is always empty or forward (never reversed). | 111 // The returned range is always empty or forward (never reversed). |
95 Range Intersect(const Range& range) const; | 112 Range Intersect(const Range& range) const; |
96 | 113 |
97 #if defined(OS_MACOSX) | 114 #if defined(OS_MACOSX) |
98 Range& operator=(const NSRange& range); | 115 Range& operator=(const NSRange& range); |
99 | 116 |
100 // NSRange does not store the directionality of a range, so if this | 117 // NSRange does not store the directionality of a range, so if this |
(...skipping 12 matching lines...) Expand all Loading... |
113 // shouldn't exceed UINT32_MAX even on 64 bit builds. | 130 // shouldn't exceed UINT32_MAX even on 64 bit builds. |
114 uint32_t start_; | 131 uint32_t start_; |
115 uint32_t end_; | 132 uint32_t end_; |
116 }; | 133 }; |
117 | 134 |
118 GFX_RANGE_EXPORT std::ostream& operator<<(std::ostream& os, const Range& range); | 135 GFX_RANGE_EXPORT std::ostream& operator<<(std::ostream& os, const Range& range); |
119 | 136 |
120 } // namespace gfx | 137 } // namespace gfx |
121 | 138 |
122 #endif // UI_GFX_RANGE_RANGE_H_ | 139 #endif // UI_GFX_RANGE_RANGE_H_ |
OLD | NEW |