Flow
Documentation for the Flow C++ Library
Loading...
Searching...
No Matches
flow_counted_value_view_iterator.h
Go to the documentation of this file.
1#pragma once
2#include <cassert>
3#include <cstddef>
4#include <iterator>
5
6namespace flow {
7
10 template <typename T>
12 const T* value_;
13 std::size_t count_;
14
15 public:
16 using value_type = T;
17 using reference = const T&;
18 using pointer = const T*;
19 using difference_type = std::ptrdiff_t;
20 using iterator_category = std::forward_iterator_tag;
21
23 : value_(nullptr), count_(0) {
24 }
25
26 CountedValueViewIterator(const T& value, std::size_t count = 0) noexcept
27 : value_(&value), count_(count) {
28 }
29
31 assert(value_ && "no value");
32 return *value_;
33 }
34
36 assert(value_ && "no value");
37 return value_;
38 }
39
41 assert(count_ > 0 && "negative count");
42 --count_;
43 return *this;
44 }
45
47 CountedValueViewIterator tmp = *this;
48 --count_;
49 return tmp;
50 }
51
52 friend bool operator==(const CountedValueViewIterator& lhs, const CountedValueViewIterator& rhs) {
53 return lhs.count_ == rhs.count_ && lhs.value_ == rhs.value_;
54 }
55
56 friend bool operator!=(const CountedValueViewIterator& lhs, const CountedValueViewIterator& rhs) {
57 return !(lhs == rhs);
58 }
59 };
60}
CountedValueViewIterator(const T &value, std::size_t count=0) noexcept
friend bool operator!=(const CountedValueViewIterator &lhs, const CountedValueViewIterator &rhs)
friend bool operator==(const CountedValueViewIterator &lhs, const CountedValueViewIterator &rhs)