]>
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 | ||
21 | #ifndef PULSEVIEW_PV_VIEW_ROWITEMITERATOR_H | |
22 | #define PULSEVIEW_PV_VIEW_ROWITEMITERATOR_H | |
23 | ||
24 | #include <algorithm> | |
25 | #include <cassert> | |
26 | #include <iterator> | |
27 | #include <memory> | |
28 | #include <stack> | |
29 | #include <type_traits> | |
30 | #include <vector> | |
31 | ||
32 | #include <boost/thread.hpp> | |
33 | ||
f65cd27b | 34 | #include <pv/session.hpp> |
6b715302 JH |
35 | |
36 | namespace pv { | |
37 | namespace view { | |
38 | ||
39 | template<class Owner, class Item> class RowItemIterator | |
40 | { | |
41 | public: | |
42 | typedef typename std::conditional<std::is_const<Owner>::value, | |
43 | typename Owner::item_list::const_iterator, | |
44 | typename Owner::item_list::iterator>::type child_iterator; | |
45 | ||
46 | typedef std::shared_ptr<Item> value_type; | |
47 | typedef ptrdiff_t difference_type; | |
48 | typedef value_type pointer; | |
49 | typedef value_type& reference; | |
50 | typedef std::forward_iterator_tag iterator_category; | |
51 | ||
52 | public: | |
53 | RowItemIterator(Owner *owner) : | |
8dbbc7f0 JH |
54 | owner_(owner), |
55 | lock_(owner->session().signals_mutex()) {} | |
6b715302 JH |
56 | |
57 | RowItemIterator(Owner *owner, child_iterator iter) : | |
8dbbc7f0 JH |
58 | owner_(owner), |
59 | lock_(owner->session().signals_mutex()) { | |
6b715302 JH |
60 | assert(owner); |
61 | if (iter != owner->child_items().end()) | |
8dbbc7f0 | 62 | iter_stack_.push(iter); |
6b715302 JH |
63 | } |
64 | ||
65 | RowItemIterator(const RowItemIterator<Owner, Item> &o) : | |
8dbbc7f0 JH |
66 | owner_(o.owner_), |
67 | lock_(*o.lock_.mutex()), | |
68 | iter_stack_(o.iter_stack_) {} | |
6b715302 JH |
69 | |
70 | reference operator*() const { | |
8dbbc7f0 | 71 | return *iter_stack_.top(); |
6b715302 JH |
72 | } |
73 | ||
74 | reference operator->() const { | |
75 | return *this; | |
76 | } | |
77 | ||
78 | RowItemIterator<Owner, Item>& operator++() { | |
79 | using std::dynamic_pointer_cast; | |
80 | using std::shared_ptr; | |
81 | ||
8dbbc7f0 JH |
82 | assert(owner_); |
83 | assert(!iter_stack_.empty()); | |
6b715302 JH |
84 | |
85 | shared_ptr<Owner> owner(dynamic_pointer_cast<Owner>( | |
8dbbc7f0 | 86 | *iter_stack_.top())); |
6b715302 | 87 | if (owner && !owner->child_items().empty()) { |
8dbbc7f0 JH |
88 | owner_ = owner.get(); |
89 | iter_stack_.push(owner->child_items().begin()); | |
6b715302 | 90 | } else { |
8dbbc7f0 JH |
91 | ++iter_stack_.top(); |
92 | while (owner_ && iter_stack_.top() == | |
93 | owner_->child_items().end()) { | |
94 | iter_stack_.pop(); | |
95 | owner_ = iter_stack_.empty() ? nullptr : | |
96 | (*iter_stack_.top()++)->owner(); | |
6b715302 JH |
97 | } |
98 | } | |
99 | ||
100 | return *this; | |
101 | } | |
102 | ||
103 | RowItemIterator<Owner, Item> operator++(int) { | |
104 | RowItemIterator<Owner, Item> pre = *this; | |
105 | ++*this; | |
106 | return pre; | |
107 | } | |
108 | ||
109 | bool operator==(const RowItemIterator &o) const { | |
8dbbc7f0 JH |
110 | return (iter_stack_.empty() && o.iter_stack_.empty()) || |
111 | (owner_ == o.owner_ && | |
112 | iter_stack_.size() == o.iter_stack_.size() && | |
6b715302 | 113 | std::equal( |
8dbbc7f0 JH |
114 | owner_->child_items().cbegin(), |
115 | owner_->child_items().cend(), | |
116 | o.owner_->child_items().cbegin())); | |
6b715302 JH |
117 | } |
118 | ||
119 | bool operator!=(const RowItemIterator &o) const { | |
120 | return !((const RowItemIterator&)*this == o); | |
121 | } | |
122 | ||
123 | void swap(RowItemIterator<Owner, Item>& other) { | |
8dbbc7f0 JH |
124 | swap(owner_, other.owner_); |
125 | swap(iter_stack_, other.iter_stack_); | |
6b715302 JH |
126 | } |
127 | ||
128 | private: | |
8dbbc7f0 JH |
129 | Owner *owner_; |
130 | boost::shared_lock<boost::shared_mutex> lock_; | |
131 | std::stack<child_iterator> iter_stack_; | |
6b715302 JH |
132 | }; |
133 | ||
134 | template<class Owner, class Item> | |
135 | void swap(RowItemIterator<Owner, Item>& a, RowItemIterator<Owner, Item>& b) | |
136 | { | |
137 | a.swap(b); | |
138 | } | |
139 | ||
140 | } // namespace view | |
141 | } // namespace pv | |
142 | ||
143 | #endif // PULSEVIEW_PV_VIEW_ROWITEMITERATOR_H |