]>
Commit | Line | Data |
---|---|---|
6b715302 JH |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
7a01bd36 JH |
21 | #ifndef PULSEVIEW_PV_VIEW_ROWITEMITERATOR_HPP |
22 | #define PULSEVIEW_PV_VIEW_ROWITEMITERATOR_HPP | |
6b715302 JH |
23 | |
24 | #include <algorithm> | |
25 | #include <cassert> | |
26 | #include <iterator> | |
27 | #include <memory> | |
28 | #include <stack> | |
29 | #include <type_traits> | |
30 | #include <vector> | |
31 | ||
f65cd27b | 32 | #include <pv/session.hpp> |
6b715302 JH |
33 | |
34 | namespace pv { | |
35 | namespace view { | |
36 | ||
37 | template<class Owner, class Item> class RowItemIterator | |
38 | { | |
39 | public: | |
40 | typedef typename std::conditional<std::is_const<Owner>::value, | |
41 | typename Owner::item_list::const_iterator, | |
42 | typename Owner::item_list::iterator>::type child_iterator; | |
43 | ||
44 | typedef std::shared_ptr<Item> value_type; | |
45 | typedef ptrdiff_t difference_type; | |
46 | typedef value_type pointer; | |
47 | typedef value_type& reference; | |
48 | typedef std::forward_iterator_tag iterator_category; | |
49 | ||
50 | public: | |
51 | RowItemIterator(Owner *owner) : | |
bf914698 | 52 | owner_(owner) {} |
6b715302 JH |
53 | |
54 | RowItemIterator(Owner *owner, child_iterator iter) : | |
bf914698 | 55 | owner_(owner) { |
6b715302 JH |
56 | assert(owner); |
57 | if (iter != owner->child_items().end()) | |
8dbbc7f0 | 58 | iter_stack_.push(iter); |
6b715302 JH |
59 | } |
60 | ||
61 | RowItemIterator(const RowItemIterator<Owner, Item> &o) : | |
8dbbc7f0 | 62 | owner_(o.owner_), |
8dbbc7f0 | 63 | iter_stack_(o.iter_stack_) {} |
6b715302 JH |
64 | |
65 | reference operator*() const { | |
8dbbc7f0 | 66 | return *iter_stack_.top(); |
6b715302 JH |
67 | } |
68 | ||
69 | reference operator->() const { | |
70 | return *this; | |
71 | } | |
72 | ||
73 | RowItemIterator<Owner, Item>& operator++() { | |
74 | using std::dynamic_pointer_cast; | |
75 | using std::shared_ptr; | |
76 | ||
8dbbc7f0 JH |
77 | assert(owner_); |
78 | assert(!iter_stack_.empty()); | |
6b715302 JH |
79 | |
80 | shared_ptr<Owner> owner(dynamic_pointer_cast<Owner>( | |
8dbbc7f0 | 81 | *iter_stack_.top())); |
6b715302 | 82 | if (owner && !owner->child_items().empty()) { |
8dbbc7f0 JH |
83 | owner_ = owner.get(); |
84 | iter_stack_.push(owner->child_items().begin()); | |
6b715302 | 85 | } else { |
8dbbc7f0 JH |
86 | ++iter_stack_.top(); |
87 | while (owner_ && iter_stack_.top() == | |
88 | owner_->child_items().end()) { | |
89 | iter_stack_.pop(); | |
90 | owner_ = iter_stack_.empty() ? nullptr : | |
91 | (*iter_stack_.top()++)->owner(); | |
6b715302 JH |
92 | } |
93 | } | |
94 | ||
95 | return *this; | |
96 | } | |
97 | ||
98 | RowItemIterator<Owner, Item> operator++(int) { | |
99 | RowItemIterator<Owner, Item> pre = *this; | |
100 | ++*this; | |
101 | return pre; | |
102 | } | |
103 | ||
104 | bool operator==(const RowItemIterator &o) const { | |
8dbbc7f0 JH |
105 | return (iter_stack_.empty() && o.iter_stack_.empty()) || |
106 | (owner_ == o.owner_ && | |
107 | iter_stack_.size() == o.iter_stack_.size() && | |
6b715302 | 108 | std::equal( |
8dbbc7f0 JH |
109 | owner_->child_items().cbegin(), |
110 | owner_->child_items().cend(), | |
111 | o.owner_->child_items().cbegin())); | |
6b715302 JH |
112 | } |
113 | ||
114 | bool operator!=(const RowItemIterator &o) const { | |
115 | return !((const RowItemIterator&)*this == o); | |
116 | } | |
117 | ||
118 | void swap(RowItemIterator<Owner, Item>& other) { | |
8dbbc7f0 JH |
119 | swap(owner_, other.owner_); |
120 | swap(iter_stack_, other.iter_stack_); | |
6b715302 JH |
121 | } |
122 | ||
123 | private: | |
8dbbc7f0 | 124 | Owner *owner_; |
8dbbc7f0 | 125 | std::stack<child_iterator> iter_stack_; |
6b715302 JH |
126 | }; |
127 | ||
128 | template<class Owner, class Item> | |
129 | void swap(RowItemIterator<Owner, Item>& a, RowItemIterator<Owner, Item>& b) | |
130 | { | |
131 | a.swap(b); | |
132 | } | |
133 | ||
134 | } // namespace view | |
135 | } // namespace pv | |
136 | ||
7a01bd36 | 137 | #endif // PULSEVIEW_PV_VIEW_ROWITEMITERATOR_HPP |