C++ Channel
Loading...
Searching...
No Matches
blocking_iterator.hpp
Go to the documentation of this file.
1// Copyright (C) 2020-2025 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
11namespace msd {
12
20template <typename Channel>
22 public:
26 using value_type = typename Channel::value_type;
27
31 using reference = const typename Channel::value_type&;
32
36 using iterator_category = std::input_iterator_tag;
37
41 using difference_type = std::ptrdiff_t;
42
46 using pointer = const value_type*;
47
54 explicit blocking_iterator(Channel& chan, bool is_end = false) : chan_{&chan}, is_end_{is_end}
55 {
56 if (!is_end_ && !chan_->read(value_)) {
57 is_end_ = true;
58 }
59 }
60
67 {
68 if (!chan_->read(value_)) {
69 is_end_ = true;
70 }
71 return *this;
72 }
73
79 reference operator*() { return value_; }
80
88 bool operator!=(const blocking_iterator& other) { return is_end_ != other.is_end_; }
89
90 private:
91 Channel* chan_;
92 value_type value_{};
93 bool is_end_{false};
94};
95
103template <typename Channel>
105 public:
109 using value_type = typename Channel::value_type;
110
114 using reference = const value_type&;
115
119 using iterator_category = std::output_iterator_tag;
120
124 using difference_type = std::ptrdiff_t;
125
129 using pointer = const value_type*;
130
136 explicit blocking_writer_iterator(Channel& chan) : chan_{&chan} {}
137
146 {
147 chan_->write(value);
148 return *this;
149 }
150
162
169
176
177 private:
178 Channel* chan_;
179};
180
188template <typename Channel>
193
194} // namespace msd
195
196#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:189
An iterator that blocks the current thread, waiting to fetch elements from the channel.
Definition blocking_iterator.hpp:21
std::ptrdiff_t difference_type
Signed integral type for iterator difference.
Definition blocking_iterator.hpp:41
reference operator*()
Returns the latest element retrieved from the channel.
Definition blocking_iterator.hpp:79
blocking_iterator< Channel > & operator++() noexcept
Retrieves the next element from the channel.
Definition blocking_iterator.hpp:66
const value_type * pointer
Pointer type to the value_type.
Definition blocking_iterator.hpp:46
blocking_iterator(Channel &chan, bool is_end=false)
Constructs a blocking iterator from a channel reference.
Definition blocking_iterator.hpp:54
const typename Channel::value_type & reference
Constant reference to the type of the elements stored in the channel.
Definition blocking_iterator.hpp:31
bool operator!=(const blocking_iterator &other)
Makes iteration continue until the channel is closed and empty.
Definition blocking_iterator.hpp:88
std::input_iterator_tag iterator_category
Supporting single-pass reading of elements.
Definition blocking_iterator.hpp:36
typename Channel::value_type value_type
The type of the elements stored in the channel.
Definition blocking_iterator.hpp:26
An output iterator pushes elements into a channel. Blocking until the channel is not full.
Definition blocking_iterator.hpp:104
const value_type & reference
Constant reference to the type of the elements stored in the channel.
Definition blocking_iterator.hpp:114
blocking_writer_iterator operator++(int)
Not applicable (handled by operator=).
Definition blocking_iterator.hpp:175
blocking_writer_iterator & operator++()
Not applicable (handled by operator=).
Definition blocking_iterator.hpp:168
std::output_iterator_tag iterator_category
Supporting writing of elements.
Definition blocking_iterator.hpp:119
blocking_writer_iterator & operator=(reference value)
Writes an element into the channel, blocking until space is available.
Definition blocking_iterator.hpp:145
blocking_writer_iterator & operator*()
Not applicable (handled by operator=).
Definition blocking_iterator.hpp:161
typename Channel::value_type value_type
The type of the elements stored in the channel.
Definition blocking_iterator.hpp:109
std::ptrdiff_t difference_type
Signed integral type for iterator difference.
Definition blocking_iterator.hpp:124
blocking_writer_iterator(Channel &chan)
Constructs a blocking iterator from a channel reference.
Definition blocking_iterator.hpp:136
const value_type * pointer
Pointer type to the value_type.
Definition blocking_iterator.hpp:129