C++ Channel
Loading...
Searching...
No Matches
blocking_iterator.hpp
Go to the documentation of this file.
1// Copyright (C) 2020-2026 Andrei Avram
2
3#ifndef MSD_CHANNEL_BLOCKING_ITERATOR_HPP_
4#define MSD_CHANNEL_BLOCKING_ITERATOR_HPP_
5
6#include <cstddef>
7#include <iterator>
8#include <type_traits>
9
11
12namespace msd {
13
21template <typename Channel>
23 public:
27 using value_type = typename Channel::value_type;
28
32 using reference = const typename Channel::value_type&;
33
37 using iterator_category = std::input_iterator_tag;
38
42 using difference_type = std::ptrdiff_t;
43
47 using pointer = const value_type*;
48
55#ifdef _MSC_VER
56// False positive in MSVC (Release).
57// https://github.com/stephenberry/glaze/issues/2545
58// https://github.com/stephenberry/glaze/pull/2556
59// https://developercommunity.visualstudio.com/t/Wrong-C4702-unreachable-code-in-template/10978308
60#pragma warning(push)
61#pragma warning(disable : 4702)
62#endif
63 explicit blocking_iterator(Channel& chan,
64 bool is_end = false) noexcept(std::is_nothrow_default_constructible<value_type>::value &&
65 std::is_nothrow_move_assignable<value_type>::value)
66 : chan_{&chan}, is_end_{is_end}
67 {
68 if (!is_end_ && !chan_->read(value_)) {
69 is_end_ = true;
70 }
71 }
72#ifdef _MSC_VER
73#pragma warning(pop)
74#endif
75
81#ifdef _MSC_VER
82#pragma warning(push)
83#pragma warning(disable : 4702) // See the comment on the constructor above.
84#endif
85 blocking_iterator<Channel>& operator++() noexcept(std::is_nothrow_move_assignable<value_type>::value)
86 {
87 if (!chan_->read(value_)) {
88 is_end_ = true;
89 }
90 return *this;
91 }
92#ifdef _MSC_VER
93#pragma warning(pop)
94#endif
95
101 reference operator*() { return value_; }
102
110 bool operator!=(const blocking_iterator& other) { return is_end_ != other.is_end_; }
111
112 private:
113 Channel* chan_;
114 value_type value_{};
115 bool is_end_{false};
116};
117
125template <typename Channel>
127 public:
131 using value_type = typename Channel::value_type;
132
136 using reference = const value_type&;
137
141 using iterator_category = std::output_iterator_tag;
142
146 using difference_type = std::ptrdiff_t;
147
151 using pointer = const value_type*;
152
158 explicit blocking_writer_iterator(Channel& chan) : chan_{&chan} {}
159
168 {
169 chan_->write(value);
170 return *this;
171 }
172
184
191
198
199 private:
200 Channel* chan_;
201};
202
210template <typename Channel>
215
216} // namespace msd
217
218#endif // MSD_CHANNEL_BLOCKING_ITERATOR_HPP_
blocking_writer_iterator< Channel > back_inserter(Channel &chan)
Creates a blocking iterator for the given channel.
Definition blocking_iterator.hpp:211
std::ptrdiff_t difference_type
Signed integral type for iterator difference.
Definition blocking_iterator.hpp:42
reference operator*()
Returns the latest element retrieved from the channel.
Definition blocking_iterator.hpp:101
const value_type * pointer
Pointer type to the value_type.
Definition blocking_iterator.hpp:47
const typename Channel::value_type & reference
Constant reference to the type of the elements stored in the channel.
Definition blocking_iterator.hpp:32
bool operator!=(const blocking_iterator &other)
Makes iteration continue until the channel is closed and empty.
Definition blocking_iterator.hpp:110
std::input_iterator_tag iterator_category
Supporting single-pass reading of elements.
Definition blocking_iterator.hpp:37
blocking_iterator(Channel &chan, bool is_end=false) noexcept(std::is_nothrow_default_constructible< value_type >::value &&std::is_nothrow_move_assignable< value_type >::value)
Constructs a blocking iterator from a channel reference.
Definition blocking_iterator.hpp:63
blocking_iterator< Channel > & operator++() noexcept(std::is_nothrow_move_assignable< value_type >::value)
Retrieves the next element from the channel.
Definition blocking_iterator.hpp:85
typename Channel::value_type value_type
The type of the elements stored in the channel.
Definition blocking_iterator.hpp:27
An output iterator pushes elements into a channel. Blocking until the channel is not full.
Definition blocking_iterator.hpp:126
const value_type & reference
Constant reference to the type of the elements stored in the channel.
Definition blocking_iterator.hpp:136
blocking_writer_iterator operator++(int)
Not applicable (handled by operator=).
Definition blocking_iterator.hpp:197
blocking_writer_iterator & operator++()
Not applicable (handled by operator=).
Definition blocking_iterator.hpp:190
std::output_iterator_tag iterator_category
Supporting writing of elements.
Definition blocking_iterator.hpp:141
blocking_writer_iterator & operator=(reference value)
Writes an element into the channel, blocking until space is available.
Definition blocking_iterator.hpp:167
blocking_writer_iterator & operator*()
Not applicable (handled by operator=).
Definition blocking_iterator.hpp:183
typename Channel::value_type value_type
The type of the elements stored in the channel.
Definition blocking_iterator.hpp:131
std::ptrdiff_t difference_type
Signed integral type for iterator difference.
Definition blocking_iterator.hpp:146
blocking_writer_iterator(Channel &chan)
Constructs a blocking iterator from a channel reference.
Definition blocking_iterator.hpp:158
const value_type * pointer
Pointer type to the value_type.
Definition blocking_iterator.hpp:151