Lumiera  0.pre.03
»edit your freedom«
iter-index.hpp
Go to the documentation of this file.
1 /*
2  ITER-INDEX.hpp - iterator with indexed random-access to referred container
3 
4  Copyright (C) Lumiera.org
5  2024, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
40 #ifndef SRC_LIB_ITER_INDEX_H
41 #define SRC_LIB_ITER_INDEX_H
42 
43 
44 #include "lib/iter-adapter.hpp"
45 
46 
47 namespace lib {
48  namespace iter {
49 
54  template<typename PTR>
56  {
57  PTR data_{};
58  size_t idx_{0};
59 
60  using ResVal = decltype(data_->operator[](0));
61 
62  using value_type = typename meta::RefTraits<ResVal>::Value;
63  using reference = typename meta::RefTraits<ResVal>::Reference;
64 
66 
67  bool
68  checkPoint() const
69  {
70  return isValidIDX(idx_);
71  }
72 
73  reference
74  yield() const
75  {
76  return (*data_)[idx_];
77  }
78 
79  void
80  iterNext()
81  {
82  ++idx_;
83  }
84 
85 
86  bool
87  isValidIDX (size_t idx) const
88  {
89  return bool(data_)
90  and idx < data_->size();
91  }
92 
93 
94  friend bool operator== (IndexAccessCore const& c1, IndexAccessCore const& c2)
95  {
96  return c1.data_ == c2.data_ and (not c1.data_ or c1.idx_ == c2.idx_);
97  }
98  friend bool operator!= (IndexAccessCore const& c1, IndexAccessCore const& c2)
99  {
100  return not (c1 == c2);
101  }
102  };
103  //
104  }//(End)Implementation
105 
106 
107 
120  template<class CON, typename PTR = CON*>
121  class IterIndex
122  : public iter::IndexAccessCore<PTR>::IterWrapper
123  {
124  using _Cor = iter::IndexAccessCore<PTR>;
125  using _Par = typename _Cor::IterWrapper;
126 
127  public:
128  IterIndex() = default;
129  IterIndex (CON& container) : IterIndex{&container}{ };
130  IterIndex (PTR pContainer)
131  : _Par{_Cor{pContainer, 0}}
132  { }
133 
134 
135  size_t
136  getIDX() const
137  {
138  _Par::__throw_if_empty();
139  return _Par::stateCore().idx_;
140  }
141 
142  void
143  setIDX (size_t newIDX)
144  {
145  auto& core = _Par::stateCore();
146  if (not core.isValidIDX (newIDX))
147  throw lumiera::error::Invalid ("Attempt to set index out of bounds",
148  lumiera::error::LUMIERA_ERROR_INDEX_BOUNDS);
149  core.idx_ = newIDX;
150  }
151  };
152 
153 
154 } // namespace lib
155 #endif /*SRC_LIB_ITER_INDEX_H*/
Representation of the result of some operation, EITHER a value or a failure.
Definition: result.hpp:106
Helper template(s) for creating Lumiera Forward Iterators.
Subscript-index based access to a container, packaged as iterator.
Definition: iter-index.hpp:121
Implementation namespace for support and library code.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
Another Lumiera Forward Iterator building block, based on incorporating a state type right into the i...
Implementation of a »IterStateCore« to access the container through an embedded index variable...
Definition: iter-index.hpp:55