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