Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Helper class that allows iteration over the grid of each each of row.
- *
- * Instead of making the listbox widget itself iterateable, I opted to go with a
- * helper class with a private ctor so it can't be instantiated from outside the
- * listbox class.
- *
- * This is also completely seperate from the widget "walkers" that use the
- * iteration::iterator class. I suppose it could be implemented in terms of that
- * class (in which case the @ref each_row_grid function should instead be an
- * overload of @ref widget::create_walker), but I'll leave that for later. This
- * is meant to be a minimal interface, and the whole walker code is confusing and
- * uses non-standard paradigms.
- *
- * This class only allows forward iteration over each row and returns that row's
- * grid on each pass. The const and non-const iterators return the result of the
- * const and non-const overloads of @ref get_row_grid at the current index.
- *
- * -- vultraz, 2017-11-19
- */
- template<typename T>
- class row_grid_iterator_helper
- {
- private:
- static_assert(std::is_same<utils::remove_const_t<T>, listbox>::value, "");
- friend class listbox;
- explicit row_grid_iterator_helper(T& parent)
- : parent_(parent)
- {
- }
- public:
- row_grid_iterator_helper() = delete;
- struct row_iterator
- {
- using iterator_category = std::output_iterator_tag;
- using value_type = grid;
- using pointer = grid*;
- using reference = grid&;
- using difference_type = unsigned;
- row_iterator(T& parent, unsigned index)
- : parent_(parent)
- , index_(index)
- {
- }
- row_iterator& operator++()
- {
- ++index_;
- return *this;
- }
- grid* operator*()
- {
- return parent_.get_row_grid(index_);
- }
- const grid* operator*() const
- {
- return parent_.get_row_grid(index_);
- }
- bool operator==(const row_iterator& other) const
- {
- return index_ == other.index_;
- }
- bool operator!=(const row_iterator& other) const
- {
- return !operator==(other);
- }
- private:
- T& parent_;
- unsigned index_;
- };
- row_iterator begin()
- {
- return iterator(parent_, 0);
- }
- row_iterator end()
- {
- return iterator(parent_, parent_.get_item_count() - 1);
- }
- private:
- T& parent_;
- };
- /** Returns a helper object with iteration capabilities over each row grid. */
- row_grid_iterator_helper<listbox> each_row_grid()
- {
- return row_grid_iterator_helper<listbox>(*this);
- }
- /** Const overload of @ref each_row_grid. */
- const row_grid_iterator_helper<const listbox> each_row_grid() const
- {
- return row_grid_iterator_helper<const listbox>(*this);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement