]> sigrok.org Git - pulseview.git/blame - pv/views/trace/viewitemiterator.hpp
Improve hover point signaling
[pulseview.git] / pv / views / trace / viewitemiterator.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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
6b715302
JH
18 */
19
f4e57597
SA
20#ifndef PULSEVIEW_PV_VIEWS_TRACEVIEW_VIEWITEMITERATOR_HPP
21#define PULSEVIEW_PV_VIEWS_TRACEVIEW_VIEWITEMITERATOR_HPP
6b715302
JH
22
23#include <algorithm>
24#include <cassert>
25#include <iterator>
26#include <memory>
27#include <stack>
28#include <type_traits>
29#include <vector>
30
f65cd27b 31#include <pv/session.hpp>
6b715302 32
6f925ba9
UH
33using std::dynamic_pointer_cast;
34using std::forward_iterator_tag;
35using std::shared_ptr;
36using std::stack;
37
6b715302 38namespace pv {
f4e57597 39namespace views {
1573bf16 40namespace trace {
6b715302 41
c373f828 42template<class Owner, class Item> class ViewItemIterator
6b715302
JH
43{
44public:
a8743cd9 45 typedef typename Owner::item_list::const_iterator child_iterator;
6f925ba9 46 typedef shared_ptr<Item> value_type;
6b715302
JH
47 typedef ptrdiff_t difference_type;
48 typedef value_type pointer;
a8743cd9 49 typedef const value_type& reference;
6f925ba9 50 typedef forward_iterator_tag iterator_category;
6b715302
JH
51
52public:
c373f828 53 ViewItemIterator(Owner *owner) :
21d5f19c 54 owner_stack_({owner}) {}
6b715302 55
c373f828 56 ViewItemIterator(Owner *owner, child_iterator iter) :
21d5f19c 57 owner_stack_({owner}) {
6b715302
JH
58 assert(owner);
59 if (iter != owner->child_items().end())
8dbbc7f0 60 iter_stack_.push(iter);
6b715302
JH
61 }
62
c373f828 63 ViewItemIterator(const ViewItemIterator<Owner, Item> &o) :
21d5f19c 64 owner_stack_(o.owner_stack_),
8dbbc7f0 65 iter_stack_(o.iter_stack_) {}
6b715302
JH
66
67 reference operator*() const {
8dbbc7f0 68 return *iter_stack_.top();
6b715302
JH
69 }
70
71 reference operator->() const {
72 return *this;
73 }
74
c373f828 75 ViewItemIterator<Owner, Item>& operator++() {
21d5f19c 76 assert(!owner_stack_.empty());
8dbbc7f0 77 assert(!iter_stack_.empty());
6b715302
JH
78
79 shared_ptr<Owner> owner(dynamic_pointer_cast<Owner>(
8dbbc7f0 80 *iter_stack_.top()));
6b715302 81 if (owner && !owner->child_items().empty()) {
21d5f19c 82 owner_stack_.push(owner.get());
8dbbc7f0 83 iter_stack_.push(owner->child_items().begin());
6b715302 84 } else {
21d5f19c
JH
85 while (!iter_stack_.empty() && (++iter_stack_.top()) ==
86 owner_stack_.top()->child_items().end()) {
87 owner_stack_.pop();
8dbbc7f0 88 iter_stack_.pop();
6b715302
JH
89 }
90 }
91
92 return *this;
93 }
94
c373f828
JH
95 ViewItemIterator<Owner, Item> operator++(int) {
96 ViewItemIterator<Owner, Item> pre = *this;
6b715302
JH
97 ++*this;
98 return pre;
99 }
100
c373f828 101 bool operator==(const ViewItemIterator &o) const {
21d5f19c 102 return (iter_stack_.empty() && o.iter_stack_.empty()) || (
8dbbc7f0 103 iter_stack_.size() == o.iter_stack_.size() &&
21d5f19c
JH
104 owner_stack_.top() == o.owner_stack_.top() &&
105 iter_stack_.top() == o.iter_stack_.top());
6b715302
JH
106 }
107
c373f828
JH
108 bool operator!=(const ViewItemIterator &o) const {
109 return !((const ViewItemIterator&)*this == o);
6b715302
JH
110 }
111
c373f828 112 void swap(ViewItemIterator<Owner, Item>& other) {
21d5f19c 113 swap(owner_stack_, other.owner_stack_);
8dbbc7f0 114 swap(iter_stack_, other.iter_stack_);
6b715302
JH
115 }
116
117private:
6f925ba9
UH
118 stack<Owner*> owner_stack_;
119 stack<child_iterator> iter_stack_;
6b715302
JH
120};
121
122template<class Owner, class Item>
c373f828 123void swap(ViewItemIterator<Owner, Item>& a, ViewItemIterator<Owner, Item>& b)
6b715302
JH
124{
125 a.swap(b);
126}
127
1573bf16 128} // namespace trace
f4e57597 129} // namespace views
6b715302
JH
130} // namespace pv
131
f4e57597 132#endif // PULSEVIEW_PV_VIEWS_TRACEVIEW_VIEWITEMITERATOR_HPP