Advertisement
Vultraz

Untitled

Nov 18th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1.     /**
  2.      * Helper class that allows iteration over the grid of each each of row.
  3.      *
  4.      * Instead of making the listbox widget itself iterateable, I opted to go with a
  5.      * helper class with a private ctor so it can't be instantiated from outside the
  6.      * listbox class.
  7.      *
  8.      * This is also completely seperate from the widget "walkers" that use the
  9.      * iteration::iterator class. I suppose it could be implemented in terms of that
  10.      * class (in which case the @ref each_row_grid function should instead be an
  11.      * overload of @ref widget::create_walker), but I'll leave that for later. This
  12.      * is meant to be a minimal interface, and the whole walker code is confusing and
  13.      * uses non-standard paradigms.
  14.      *
  15.      * This class only allows forward iteration over each row and returns that row's
  16.      * grid on each pass. The const and non-const iterators return the result of the
  17.      * const and non-const overloads of @ref get_row_grid at the current index.
  18.      *
  19.      * -- vultraz, 2017-11-19
  20.      */
  21.     template<typename T>
  22.     class row_grid_iterator_helper
  23.     {
  24.     private:
  25.         static_assert(std::is_same<utils::remove_const_t<T>, listbox>::value, "");
  26.  
  27.         friend class listbox;
  28.  
  29.         explicit row_grid_iterator_helper(T& parent)
  30.             : parent_(parent)
  31.         {
  32.         }
  33.  
  34.     public:
  35.         row_grid_iterator_helper() = delete;
  36.  
  37.         struct row_iterator
  38.         {
  39.             using iterator_category = std::output_iterator_tag;
  40.             using value_type        = grid;
  41.             using pointer           = grid*;
  42.             using reference         = grid&;
  43.             using difference_type   = unsigned;
  44.  
  45.             row_iterator(T& parent, unsigned index)
  46.                 : parent_(parent)
  47.                 , index_(index)
  48.             {
  49.             }
  50.  
  51.             row_iterator& operator++()
  52.             {
  53.                 ++index_;
  54.                 return *this;
  55.             }
  56.  
  57.             grid* operator*()
  58.             {
  59.                 return parent_.get_row_grid(index_);
  60.             }
  61.  
  62.             const grid* operator*() const
  63.             {
  64.                 return parent_.get_row_grid(index_);
  65.             }
  66.  
  67.             bool operator==(const row_iterator& other) const
  68.             {
  69.                 return index_ == other.index_;
  70.             }
  71.  
  72.             bool operator!=(const row_iterator& other) const
  73.             {
  74.                 return !operator==(other);
  75.             }
  76.  
  77.         private:
  78.             T& parent_;
  79.             unsigned index_;
  80.         };
  81.  
  82.         row_iterator begin()
  83.         {
  84.             return iterator(parent_, 0);
  85.         }
  86.  
  87.         row_iterator end()
  88.         {
  89.             return iterator(parent_, parent_.get_item_count() - 1);
  90.         }
  91.  
  92.     private:
  93.         T& parent_;
  94.     };
  95.  
  96.     /** Returns a helper object with iteration capabilities over each row grid. */
  97.     row_grid_iterator_helper<listbox> each_row_grid()
  98.     {
  99.         return row_grid_iterator_helper<listbox>(*this);
  100.     }
  101.  
  102.     /** Const overload of @ref each_row_grid. */
  103.     const row_grid_iterator_helper<const listbox> each_row_grid() const
  104.     {
  105.         return row_grid_iterator_helper<const listbox>(*this);
  106.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement