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