Vince's CSV Parser
Loading...
Searching...
No Matches
indexed_proxy_iterator.hpp
1#pragma once
2
3#include <cstddef>
4#include <iterator>
5
6namespace csv {
7 namespace internals {
8 template<typename Owner, typename Proxy, typename Accessor>
10 public:
11 using value_type = Proxy;
12 using difference_type = std::ptrdiff_t;
13 using pointer = const Proxy*;
14 using reference = const Proxy&;
15 using iterator_category = std::random_access_iterator_tag;
16
17 indexed_proxy_iterator() = default;
18 indexed_proxy_iterator(Owner* owner, size_t index, Accessor accessor = Accessor())
19 : owner_(owner), index_(index), accessor_(accessor) {}
20
21 reference operator*() const {
22 cached_proxy_ = accessor_(owner_, index_);
23 return cached_proxy_;
24 }
25
26 pointer operator->() const {
27 operator*();
28 return &cached_proxy_;
29 }
30
31 indexed_proxy_iterator& operator++() { ++index_; return *this; }
32 indexed_proxy_iterator operator++(int) { auto tmp = *this; ++index_; return tmp; }
33 indexed_proxy_iterator& operator--() { --index_; return *this; }
34 indexed_proxy_iterator operator--(int) { auto tmp = *this; --index_; return tmp; }
35
36 indexed_proxy_iterator operator+(difference_type n) const {
37 return indexed_proxy_iterator(owner_, static_cast<size_t>(index_ + n), accessor_);
38 }
39
40 indexed_proxy_iterator operator-(difference_type n) const {
41 return indexed_proxy_iterator(owner_, static_cast<size_t>(index_ - n), accessor_);
42 }
43
44 difference_type operator-(const indexed_proxy_iterator& other) const {
45 return static_cast<difference_type>(index_) - static_cast<difference_type>(other.index_);
46 }
47
48 bool operator==(const indexed_proxy_iterator& other) const {
49 return owner_ == other.owner_ && index_ == other.index_;
50 }
51
52 bool operator!=(const indexed_proxy_iterator& other) const {
53 return !(*this == other);
54 }
55
56 private:
57 Owner* owner_ = nullptr;
58 size_t index_ = 0;
59 Accessor accessor_;
60 mutable Proxy cached_proxy_;
61 };
62 }
63}
Borrow a row and error state for synchronous, single-pass field access.
The all encompassing namespace.