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

Side by Side Diff: ui/accessibility/ax_range.h

Issue 2934953004: De-templatize ui::AXPosition
Patch Set: Fix Android? Created 3 years, 6 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 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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_ACCESSIBILITY_AX_RANGE_H_ 5 #ifndef UI_ACCESSIBILITY_AX_RANGE_H_
6 #define UI_ACCESSIBILITY_AX_RANGE_H_ 6 #define UI_ACCESSIBILITY_AX_RANGE_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/strings/string16.h" 11 #include "base/strings/string16.h"
12 #include "ui/accessibility/ax_export.h"
13 #include "ui/accessibility/ax_position.h"
12 14
13 namespace ui { 15 namespace ui {
14 16
15 // A range of ax positions. 17 // A range of ax positions.
16 // 18 //
17 // In order to avoid any confusion regarding whether a deep or a shallow copy is 19 // In order to avoid any confusion regarding whether a deep or a shallow copy is
18 // being performed, this class can be moved but not copied. 20 // being performed, this class can be moved but not copied.
19 template <class AXPositionType> 21 class AX_EXPORT AXAbstractRange {
20 class AXRange {
21 public: 22 public:
22 AXRange() 23 AXAbstractRange(std::unique_ptr<AXPositionBase> anchor,
23 : anchor_(AXPositionType::CreateNullPosition()), 24 std::unique_ptr<AXPositionBase> focus);
24 focus_(AXPositionType::CreateNullPosition()) {} 25 AXAbstractRange(AXAbstractRange&& other);
25 26
26 AXRange(std::unique_ptr<AXPositionType> anchor, 27 AXAbstractRange(const AXAbstractRange& other) = delete;
27 std::unique_ptr<AXPositionType> focus) { 28 AXAbstractRange& operator=(const AXAbstractRange& other) = delete;
28 if (anchor) { 29 AXAbstractRange& operator=(AXAbstractRange&& other) = delete;
29 anchor_ = std::move(anchor);
30 } else {
31 anchor_ = AXPositionType::CreateNullPosition();
32 }
33 if (focus) {
34 focus_ = std::move(focus);
35 } else {
36 focus = AXPositionType::CreateNullPosition();
37 }
38 }
39 30
40 AXRange(const AXRange& other) = delete; 31 ~AXAbstractRange();
41
42 AXRange(AXRange&& other) : AXRange() {
43 anchor_.swap(other.anchor_);
44 focus_.swap(other.focus_);
45 }
46
47 AXRange& operator=(const AXRange& other) = delete;
48
49 AXRange& operator=(const AXRange&& other) {
50 if (this != other) {
tapted 2017/06/15 11:57:44 I removed this method since it's not currently ins
51 anchor_ = AXPositionType::CreateNullPosition();
52 focus_ = AXPositionType::CreateNullPosition();
53 anchor_.swap(other.anchor_);
54 focus_.swap(other.focus_);
55 }
56 return *this;
57 }
58
59 virtual ~AXRange() {}
60 32
61 bool IsNull() const { 33 bool IsNull() const {
62 return !anchor_ || !focus_ || anchor_->IsNullPosition() || 34 return !anchor_ || !focus_ || anchor_->IsNullPosition() ||
63 focus_->IsNullPosition(); 35 focus_->IsNullPosition();
64 } 36 }
65 37
66 AXPositionType* anchor() const {
67 DCHECK(anchor_);
68 return anchor_.get();
69 }
70
71 AXPositionType* focus() const {
72 DCHECK(focus_);
73 return focus_.get();
74 }
75
76 base::string16 GetText() const { 38 base::string16 GetText() const {
77 base::string16 text; 39 base::string16 text;
78 if (IsNull()) 40 if (IsNull())
79 return text; 41 return text;
80 42
81 std::unique_ptr<AXPositionType> start, end; 43 std::unique_ptr<AXPositionBase> start, end;
82 if (*anchor_ < *focus_) { 44 if (*anchor_ < *focus_) {
83 start = anchor_->AsLeafTextPosition(); 45 start = anchor_->AsLeafTextPosition();
84 end = focus_->AsLeafTextPosition(); 46 end = focus_->AsLeafTextPosition();
85 } else { 47 } else {
86 start = focus_->AsLeafTextPosition(); 48 start = focus_->AsLeafTextPosition();
87 end = anchor_->AsLeafTextPosition(); 49 end = anchor_->AsLeafTextPosition();
88 } 50 }
89 51
90 int start_offset = start->text_offset(); 52 int start_offset = start->text_offset();
91 DCHECK_GE(start_offset, 0); 53 DCHECK_GE(start_offset, 0);
92 int end_offset = end->text_offset(); 54 int end_offset = end->text_offset();
93 DCHECK_GE(end_offset, 0); 55 DCHECK_GE(end_offset, 0);
94 56
95 do { 57 do {
96 text += start->GetInnerText(); 58 text += start->GetInnerText();
97 start = start->CreateNextTextAnchorPosition(); 59 start = start->CreateNextTextAnchorPosition();
98 } while (!start->IsNullPosition() && *start <= *end); 60 } while (!start->IsNullPosition() && *start <= *end);
99 61
100 if (static_cast<size_t>(start_offset) > text.length()) 62 if (static_cast<size_t>(start_offset) > text.length())
101 return base::string16(); 63 return base::string16();
102 64
103 text = text.substr(start_offset, base::string16::npos); 65 text = text.substr(start_offset, base::string16::npos);
104 size_t text_length = text.length() - end->GetInnerText().length() + 66 size_t text_length = text.length() - end->GetInnerText().length() +
105 static_cast<size_t>(end_offset); 67 static_cast<size_t>(end_offset);
106 return text.substr(0, text_length); 68 return text.substr(0, text_length);
107 } 69 }
108 70
71 AXPositionBase* anchor() const { return anchor_.get(); }
72 AXPositionBase* focus() const { return focus_.get(); }
73
74 protected:
75 std::unique_ptr<AXPositionBase> anchor_;
76 std::unique_ptr<AXPositionBase> focus_;
77 };
78
79 template <class AXPositionType>
80 class AXRange : public AXAbstractRange {
81 public:
82 AXRange() : AXAbstractRange(CreateNull(), CreateNull()) {}
83 AXRange(std::unique_ptr<AXPositionType> anchor,
84 std::unique_ptr<AXPositionType> focus)
85 : AXAbstractRange(anchor ? AsBase(std::move(anchor)) : CreateNull(),
86 focus ? AsBase(std::move(focus)) : CreateNull()) {}
87 AXRange(AXRange&& other) = default;
88
89 AXRange(const AXRange& other) = delete;
90 AXRange& operator=(const AXRange& other) = delete;
91 AXRange& operator=(AXRange&& other) = delete;
92
93 typename AXPositionType::ConcreteNodeType* GetAnchorNode() const {
94 return static_cast<AXPositionType*>(anchor_.get())->GetAnchor();
95 }
96 typename AXPositionType::ConcreteNodeType* GetFocusNode() const {
97 return static_cast<AXPositionType*>(focus_.get())->GetAnchor();
98 }
99
109 private: 100 private:
110 std::unique_ptr<AXPositionType> anchor_; 101 static std::unique_ptr<AXPositionBase> AsBase(
111 std::unique_ptr<AXPositionType> focus_; 102 std::unique_ptr<AXPositionType> concrete) {
103 return base::WrapUnique<AXPositionBase>(concrete.release());
104 }
105
106 static std::unique_ptr<AXPositionBase> CreateNull() {
107 return AsBase(AXPositionType::CreateConcreteNullPosition());
108 }
112 }; 109 };
113 110
114 } // namespace ui 111 } // namespace ui
115 112
116 #endif // UI_ACCESSIBILITY_AX_RANGE_H_ 113 #endif // UI_ACCESSIBILITY_AX_RANGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698