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_H
22 #define PULSEVIEW_PV_VIEW_ROWITEMITERATOR_H
29 #include <type_traits>
32 #include <boost/thread.hpp>
34 #include <pv/sigsession.h>
39 template<class Owner, class Item> class RowItemIterator
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;
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;
53 RowItemIterator(Owner *owner) :
55 _lock(owner->session().signals_mutex()) {}
57 RowItemIterator(Owner *owner, child_iterator iter) :
59 _lock(owner->session().signals_mutex()) {
61 if (iter != owner->child_items().end())
62 _iter_stack.push(iter);
65 RowItemIterator(const RowItemIterator<Owner, Item> &o) :
67 _lock(*o._lock.mutex()),
68 _iter_stack(o._iter_stack) {}
70 reference operator*() const {
71 return *_iter_stack.top();
74 reference operator->() const {
78 RowItemIterator<Owner, Item>& operator++() {
79 using std::dynamic_pointer_cast;
80 using std::shared_ptr;
83 assert(!_iter_stack.empty());
85 shared_ptr<Owner> owner(dynamic_pointer_cast<Owner>(
87 if (owner && !owner->child_items().empty()) {
89 _iter_stack.push(owner->child_items().begin());
92 while (_owner && _iter_stack.top() ==
93 _owner->child_items().end()) {
95 _owner = _iter_stack.empty() ? nullptr :
96 (*_iter_stack.top()++)->owner();
103 RowItemIterator<Owner, Item> operator++(int) {
104 RowItemIterator<Owner, Item> pre = *this;
109 bool operator==(const RowItemIterator &o) const {
110 return (_iter_stack.empty() && o._iter_stack.empty()) ||
111 (_owner == o._owner &&
112 _iter_stack.size() == o._iter_stack.size() &&
114 _owner->child_items().cbegin(),
115 _owner->child_items().cend(),
116 o._owner->child_items().cbegin()));
119 bool operator!=(const RowItemIterator &o) const {
120 return !((const RowItemIterator&)*this == o);
123 void swap(RowItemIterator<Owner, Item>& other) {
124 swap(_owner, other._owner);
125 swap(_iter_stack, other._iter_stack);
130 boost::shared_lock<boost::shared_mutex> _lock;
131 std::stack<child_iterator> _iter_stack;
134 template<class Owner, class Item>
135 void swap(RowItemIterator<Owner, Item>& a, RowItemIterator<Owner, Item>& b)
143 #endif // PULSEVIEW_PV_VIEW_ROWITEMITERATOR_H