]> sigrok.org Git - pulseview.git/blame - pv/view/rowitemiterator.hpp
Reduce include bloat by including boost/thread/{locks,shared_mutex}.hpp directly
[pulseview.git] / pv / view / rowitemiterator.hpp
CommitLineData
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
627ffe3f
VI
32#ifdef _WIN32
33// Windows: Avoid namespace pollution by thread.hpp (which includes windows.h).
34#define NOGDI
35#define NORESOURCE
36#endif
e71eb81c
JH
37#include <boost/thread/locks.hpp>
38#include <boost/thread/shared_mutex.hpp>
6b715302 39
f65cd27b 40#include <pv/session.hpp>
6b715302
JH
41
42namespace pv {
43namespace view {
44
45template<class Owner, class Item> class RowItemIterator
46{
47public:
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;
51
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;
57
58public:
59 RowItemIterator(Owner *owner) :
8dbbc7f0
JH
60 owner_(owner),
61 lock_(owner->session().signals_mutex()) {}
6b715302
JH
62
63 RowItemIterator(Owner *owner, child_iterator iter) :
8dbbc7f0
JH
64 owner_(owner),
65 lock_(owner->session().signals_mutex()) {
6b715302
JH
66 assert(owner);
67 if (iter != owner->child_items().end())
8dbbc7f0 68 iter_stack_.push(iter);
6b715302
JH
69 }
70
71 RowItemIterator(const RowItemIterator<Owner, Item> &o) :
8dbbc7f0
JH
72 owner_(o.owner_),
73 lock_(*o.lock_.mutex()),
74 iter_stack_(o.iter_stack_) {}
6b715302
JH
75
76 reference operator*() const {
8dbbc7f0 77 return *iter_stack_.top();
6b715302
JH
78 }
79
80 reference operator->() const {
81 return *this;
82 }
83
84 RowItemIterator<Owner, Item>& operator++() {
85 using std::dynamic_pointer_cast;
86 using std::shared_ptr;
87
8dbbc7f0
JH
88 assert(owner_);
89 assert(!iter_stack_.empty());
6b715302
JH
90
91 shared_ptr<Owner> owner(dynamic_pointer_cast<Owner>(
8dbbc7f0 92 *iter_stack_.top()));
6b715302 93 if (owner && !owner->child_items().empty()) {
8dbbc7f0
JH
94 owner_ = owner.get();
95 iter_stack_.push(owner->child_items().begin());
6b715302 96 } else {
8dbbc7f0
JH
97 ++iter_stack_.top();
98 while (owner_ && iter_stack_.top() ==
99 owner_->child_items().end()) {
100 iter_stack_.pop();
101 owner_ = iter_stack_.empty() ? nullptr :
102 (*iter_stack_.top()++)->owner();
6b715302
JH
103 }
104 }
105
106 return *this;
107 }
108
109 RowItemIterator<Owner, Item> operator++(int) {
110 RowItemIterator<Owner, Item> pre = *this;
111 ++*this;
112 return pre;
113 }
114
115 bool operator==(const RowItemIterator &o) const {
8dbbc7f0
JH
116 return (iter_stack_.empty() && o.iter_stack_.empty()) ||
117 (owner_ == o.owner_ &&
118 iter_stack_.size() == o.iter_stack_.size() &&
6b715302 119 std::equal(
8dbbc7f0
JH
120 owner_->child_items().cbegin(),
121 owner_->child_items().cend(),
122 o.owner_->child_items().cbegin()));
6b715302
JH
123 }
124
125 bool operator!=(const RowItemIterator &o) const {
126 return !((const RowItemIterator&)*this == o);
127 }
128
129 void swap(RowItemIterator<Owner, Item>& other) {
8dbbc7f0
JH
130 swap(owner_, other.owner_);
131 swap(iter_stack_, other.iter_stack_);
6b715302
JH
132 }
133
134private:
8dbbc7f0
JH
135 Owner *owner_;
136 boost::shared_lock<boost::shared_mutex> lock_;
137 std::stack<child_iterator> iter_stack_;
6b715302
JH
138};
139
140template<class Owner, class Item>
141void swap(RowItemIterator<Owner, Item>& a, RowItemIterator<Owner, Item>& b)
142{
143 a.swap(b);
144}
145
146} // namespace view
147} // namespace pv
148
7a01bd36 149#endif // PULSEVIEW_PV_VIEW_ROWITEMITERATOR_HPP