FastArduino  v1.8
C++ library to build fast but small Arduino/AVR projects
iterator.h
Go to the documentation of this file.
1 // Copyright 2016-2021 Jean-Francois Poilpret
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
21 #ifndef ITERATOR_HH
22 #define ITERATOR_HH
23 
24 #include <stddef.h>
25 #include <stdint.h>
26 #include "initializer_list.h"
27 
28 namespace utils
29 {
50  template<typename T> class range
51  {
52  public:
54  range(const T* begin, const T* end) : begin_{begin}, end_{end} {}
55  range(const T* begin, uint8_t size) : begin_{begin}, end_{begin + size} {}
56  range(std::initializer_list<T> list) : begin_{list.begin()}, end_{list.end()} {}
57  template<uint8_t SIZE> range(T (&content)[SIZE]) : begin_{content}, end_{content + SIZE} {}
58 
59  const T* begin() const
60  {
61  return begin_;
62  }
63  const T* end() const
64  {
65  return end_;
66  }
67 
68  uint8_t size() const
69  {
70  return end_ - begin_;
71  }
73 
74  private:
75  const T* const begin_;
76  const T* const end_;
77  };
78 }
79 
80 #endif /* ITERATOR_HH */
81 
std::initializer_list::begin
constexpr const T * begin() const
The first element of this initializer_list.
Definition: initializer_list.h:79
utils::range
Iterable class that can embed arrays or initializer lists through implicit conversion.
Definition: iterator.h:51
utils
Contains all generic utility methods.
Definition: iterator.h:29
std::initializer_list::end
constexpr const T * end() const
One past the last element of this initializer_list.
Definition: initializer_list.h:85
std::initializer_list
C++ standard support for "initializer_list".
Definition: initializer_list.h:57