1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
/*
* This file is part of the PulseView project.
*
* Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PULSEVIEW_PV_VIEWS_TRACEVIEW_VIEWITEMITERATOR_HPP
#define PULSEVIEW_PV_VIEWS_TRACEVIEW_VIEWITEMITERATOR_HPP
#include <algorithm>
#include <cassert>
#include <iterator>
#include <memory>
#include <stack>
#include <type_traits>
#include <vector>
#include <pv/session.hpp>
using std::dynamic_pointer_cast;
using std::forward_iterator_tag;
using std::shared_ptr;
using std::stack;
namespace pv {
namespace views {
namespace trace {
template<class Owner, class Item> class ViewItemIterator
{
public:
typedef typename Owner::item_list::const_iterator child_iterator;
typedef shared_ptr<Item> value_type;
typedef ptrdiff_t difference_type;
typedef value_type pointer;
typedef const value_type& reference;
typedef forward_iterator_tag iterator_category;
public:
ViewItemIterator(Owner *owner) :
owner_stack_({owner}) {}
ViewItemIterator(Owner *owner, child_iterator iter) :
owner_stack_({owner}) {
assert(owner);
if (iter != owner->child_items().end())
iter_stack_.push(iter);
}
ViewItemIterator(const ViewItemIterator<Owner, Item> &o) :
owner_stack_(o.owner_stack_),
iter_stack_(o.iter_stack_) {}
reference operator*() const {
return *iter_stack_.top();
}
reference operator->() const {
return *this;
}
ViewItemIterator<Owner, Item>& operator++() {
assert(!owner_stack_.empty());
assert(!iter_stack_.empty());
shared_ptr<Owner> owner(dynamic_pointer_cast<Owner>(
*iter_stack_.top()));
if (owner && !owner->child_items().empty()) {
owner_stack_.push(owner.get());
iter_stack_.push(owner->child_items().begin());
} else {
while (!iter_stack_.empty() && (++iter_stack_.top()) ==
owner_stack_.top()->child_items().end()) {
owner_stack_.pop();
iter_stack_.pop();
}
}
return *this;
}
ViewItemIterator<Owner, Item> operator++(int) {
ViewItemIterator<Owner, Item> pre = *this;
++*this;
return pre;
}
bool operator==(const ViewItemIterator &o) const {
return (iter_stack_.empty() && o.iter_stack_.empty()) || (
iter_stack_.size() == o.iter_stack_.size() &&
owner_stack_.top() == o.owner_stack_.top() &&
iter_stack_.top() == o.iter_stack_.top());
}
bool operator!=(const ViewItemIterator &o) const {
return !((const ViewItemIterator&)*this == o);
}
void swap(ViewItemIterator<Owner, Item>& other) {
swap(owner_stack_, other.owner_stack_);
swap(iter_stack_, other.iter_stack_);
}
private:
stack<Owner*> owner_stack_;
stack<child_iterator> iter_stack_;
};
template<class Owner, class Item>
void swap(ViewItemIterator<Owner, Item>& a, ViewItemIterator<Owner, Item>& b)
{
a.swap(b);
}
} // namespace trace
} // namespace views
} // namespace pv
#endif // PULSEVIEW_PV_VIEWS_TRACEVIEW_VIEWITEMITERATOR_HPP
|