| 19 | */ |
| 20 | template <typename Channel> |
| 21 | class blocking_iterator { |
| 22 | public: |
| 23 | /** |
| 24 | * @brief The type of the elements stored in the channel. |
| 25 | */ |
| 26 | using value_type = typename Channel::value_type; |
| 27 | |
| 28 | /** |
| 29 | * @brief Constant reference to the type of the elements stored in the channel. |
| 30 | */ |
| 31 | using reference = const typename Channel::value_type&; |
| 32 | |
| 33 | /** |
| 34 | * @brief Supporting single-pass reading of elements. |
| 35 | */ |
| 36 | using iterator_category = std::input_iterator_tag; |
| 37 | |
| 38 | /** |
| 39 | * @brief Signed integral type for iterator difference. |
| 40 | */ |
| 41 | using difference_type = std::ptrdiff_t; |
| 42 | |
| 43 | /** |
| 44 | * @brief Pointer type to the value_type. |
| 45 | */ |
| 46 | using pointer = const value_type*; |
| 47 | |
| 48 | /** |
| 49 | * @brief Constructs a blocking iterator from a channel reference. |
| 50 | * |
| 51 | * @param chan Reference to the channel this iterator will iterate over. |
| 52 | * @param is_end If true, the iterator is in an end state (no elements to read). |
| 53 | */ |
| 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 | |
| 61 | /** |
| 62 | * @brief Retrieves the next element from the channel. |
| 63 | * |
| 64 | * @return The iterator itself. |
| 65 | */ |
| 66 | blocking_iterator<Channel>& operator++() noexcept |
| 67 | { |
| 68 | if (!chan_->read(value_)) { |
| 69 | is_end_ = true; |
| 70 | } |
| 71 | return *this; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @brief Returns the latest element retrieved from the channel. |
| 76 | * |
| 77 | * @return A const reference to the element. |
| 78 | */ |