GDAL
cpl_enumerate.h
1/******************************************************************************
2 *
3 * Project: CPL - Common Portability Library
4 * Purpose: Collection enumerator
5 * Author: Even Rouault, even.rouault at spatialys.com
6 *
7 ******************************************************************************
8 * Copyright (c) 2025, Even Rouault <even.rouault at spatialys.com>
9 *
10 * SPDX-License-Identifier: MIT
11 ****************************************************************************/
12
13#ifndef CPL_ENUMERATE_H
14#define CPL_ENUMERATE_H
15
16#include <cstddef> // size_t
17#include <iterator> // std::begin(), std::end()
18#include <utility> // std::declval(), std::pair
19
20namespace cpl
21{
22
24
25template <class T> class Enumerator
26{
27 public:
28 using TIter = decltype(std::begin(std::declval<T &>()));
29
30 class iterator
31 {
32 public:
33 explicit inline iterator(const TIter &it) : m_iter(it)
34 {
35 }
36
37 inline bool operator==(const iterator &other) const
38 {
39 return m_iter == other.m_iter;
40 }
41
42 inline bool operator!=(const iterator &other) const
43 {
44 return m_iter != other.m_iter;
45 }
46
47 inline iterator &operator++()
48 {
49 ++m_index;
50 ++m_iter;
51 return *this;
52 }
53
54 inline iterator operator++(int)
55 {
56 iterator before = *this;
57 ++(*this);
58 return before;
59 }
60
61 inline auto operator*() const
62 {
63 return std::pair<size_t, decltype(*m_iter) &>(m_index, *m_iter);
64 }
65
66 private:
67 TIter m_iter;
68 size_t m_index = 0;
69 };
70
71 explicit inline Enumerator(T &iterable) : m_iterable(iterable)
72 {
73 }
74
75 inline iterator begin() const
76 {
77 return iterator(std::begin(m_iterable));
78 }
79
80 inline iterator end() const
81 {
82 // We could initialize the m_index to SIZE_T_MAX, but this does not
83 // really matter as iterator comparison is done on the underlying
84 // iterator and not the index.
85 return iterator(std::end(m_iterable));
86 }
87
88 private:
89 T &m_iterable;
90};
91
93
102template <class T> inline auto enumerate(T &iterable)
103{
104 return Enumerator<T>(iterable);
105}
106
107} // namespace cpl
108
109#endif
OGRLayer::FeatureIterator begin(OGRLayer *poLayer)
Return begin of feature iterator.
Definition ogrsf_frmts.h:478
OGRLayer::FeatureIterator end(OGRLayer *poLayer)
Return end of feature iterator.
Definition ogrsf_frmts.h:486