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