]> sigrok.org Git - pulseview.git/blob - pv/view/rowitemiterator.hpp
10de80a03060bde9323eb4e549ff1aede26b4a49
[pulseview.git] / pv / view / rowitemiterator.hpp
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/locks.hpp>
38 #include <boost/thread/shared_mutex.hpp>
39
40 #include <pv/session.hpp>
41
42 namespace pv {
43 namespace view {
44
45 template<class Owner, class Item> class RowItemIterator
46 {
47 public:
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
58 public:
59         RowItemIterator(Owner *owner) :
60                 owner_(owner),
61                 lock_(owner->session().signals_mutex()) {}
62
63         RowItemIterator(Owner *owner, child_iterator iter) :
64                 owner_(owner),
65                 lock_(owner->session().signals_mutex()) {
66                 assert(owner);
67                 if (iter != owner->child_items().end())
68                         iter_stack_.push(iter);
69         }
70
71         RowItemIterator(const RowItemIterator<Owner, Item> &o) :
72                 owner_(o.owner_),
73                 lock_(*o.lock_.mutex()),
74                 iter_stack_(o.iter_stack_) {}
75
76         reference operator*() const {
77                 return *iter_stack_.top();
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
88                 assert(owner_);
89                 assert(!iter_stack_.empty());
90
91                 shared_ptr<Owner> owner(dynamic_pointer_cast<Owner>(
92                         *iter_stack_.top()));
93                 if (owner && !owner->child_items().empty()) {
94                         owner_ = owner.get();
95                         iter_stack_.push(owner->child_items().begin());
96                 } else {
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();
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 {
116                 return (iter_stack_.empty() && o.iter_stack_.empty()) ||
117                         (owner_ == o.owner_ &&
118                         iter_stack_.size() == o.iter_stack_.size() &&
119                         std::equal(
120                                 owner_->child_items().cbegin(),
121                                 owner_->child_items().cend(),
122                                 o.owner_->child_items().cbegin()));
123         }
124
125         bool operator!=(const RowItemIterator &o) const {
126                 return !((const RowItemIterator&)*this == o);
127         }
128
129         void swap(RowItemIterator<Owner, Item>& other) {
130                 swap(owner_, other.owner_);
131                 swap(iter_stack_, other.iter_stack_);
132         }
133
134 private:
135         Owner *owner_;
136         boost::shared_lock<boost::shared_mutex> lock_;
137         std::stack<child_iterator> iter_stack_;
138 };
139
140 template<class Owner, class Item>
141 void swap(RowItemIterator<Owner, Item>& a, RowItemIterator<Owner, Item>& b)
142 {
143         a.swap(b);
144 }
145
146 } // namespace view
147 } // namespace pv
148
149 #endif // PULSEVIEW_PV_VIEW_ROWITEMITERATOR_HPP