2 * This file is part of the PulseView project.
4 * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #ifndef PULSEVIEW_PV_VIEW_ROWITEMITERATOR_HPP
22 #define PULSEVIEW_PV_VIEW_ROWITEMITERATOR_HPP
29 #include <type_traits>
33 // Windows: Avoid namespace pollution by thread.hpp (which includes windows.h).
37 #include <boost/thread/locks.hpp>
38 #include <boost/thread/shared_mutex.hpp>
40 #include <pv/session.hpp>
45 template<class Owner, class Item> class RowItemIterator
48 typedef typename std::conditional<std::is_const<Owner>::value,
49 typename Owner::item_list::const_iterator,
50 typename Owner::item_list::iterator>::type child_iterator;
52 typedef std::shared_ptr<Item> value_type;
53 typedef ptrdiff_t difference_type;
54 typedef value_type pointer;
55 typedef value_type& reference;
56 typedef std::forward_iterator_tag iterator_category;
59 RowItemIterator(Owner *owner) :
61 lock_(owner->session().signals_mutex()) {}
63 RowItemIterator(Owner *owner, child_iterator iter) :
65 lock_(owner->session().signals_mutex()) {
67 if (iter != owner->child_items().end())
68 iter_stack_.push(iter);
71 RowItemIterator(const RowItemIterator<Owner, Item> &o) :
73 lock_(*o.lock_.mutex()),
74 iter_stack_(o.iter_stack_) {}
76 reference operator*() const {
77 return *iter_stack_.top();
80 reference operator->() const {
84 RowItemIterator<Owner, Item>& operator++() {
85 using std::dynamic_pointer_cast;
86 using std::shared_ptr;
89 assert(!iter_stack_.empty());
91 shared_ptr<Owner> owner(dynamic_pointer_cast<Owner>(
93 if (owner && !owner->child_items().empty()) {
95 iter_stack_.push(owner->child_items().begin());
98 while (owner_ && iter_stack_.top() ==
99 owner_->child_items().end()) {
101 owner_ = iter_stack_.empty() ? nullptr :
102 (*iter_stack_.top()++)->owner();
109 RowItemIterator<Owner, Item> operator++(int) {
110 RowItemIterator<Owner, Item> pre = *this;
115 bool operator==(const RowItemIterator &o) const {
116 return (iter_stack_.empty() && o.iter_stack_.empty()) ||
117 (owner_ == o.owner_ &&
118 iter_stack_.size() == o.iter_stack_.size() &&
120 owner_->child_items().cbegin(),
121 owner_->child_items().cend(),
122 o.owner_->child_items().cbegin()));
125 bool operator!=(const RowItemIterator &o) const {
126 return !((const RowItemIterator&)*this == o);
129 void swap(RowItemIterator<Owner, Item>& other) {
130 swap(owner_, other.owner_);
131 swap(iter_stack_, other.iter_stack_);
136 boost::shared_lock<boost::shared_mutex> lock_;
137 std::stack<child_iterator> iter_stack_;
140 template<class Owner, class Item>
141 void swap(RowItemIterator<Owner, Item>& a, RowItemIterator<Owner, Item>& b)
149 #endif // PULSEVIEW_PV_VIEW_ROWITEMITERATOR_HPP