| 102 | */ |
| 103 | template <typename Channel> |
| 104 | class blocking_writer_iterator { |
| 105 | public: |
| 106 | /** |
| 107 | * @brief The type of the elements stored in the channel. |
| 108 | */ |
| 109 | using value_type = typename Channel::value_type; |
| 110 | |
| 111 | /** |
| 112 | * @brief Constant reference to the type of the elements stored in the channel. |
| 113 | */ |
| 114 | using reference = const value_type&; |
| 115 | |
| 116 | /** |
| 117 | * @brief Supporting writing of elements. |
| 118 | */ |
| 119 | using iterator_category = std::output_iterator_tag; |
| 120 | |
| 121 | /** |
| 122 | * @brief Signed integral type for iterator difference. |
| 123 | */ |
| 124 | using difference_type = std::ptrdiff_t; |
| 125 | |
| 126 | /** |
| 127 | * @brief Pointer type to the value_type. |
| 128 | */ |
| 129 | using pointer = const value_type*; |
| 130 | |
| 131 | /** |
| 132 | * @brief Constructs a blocking iterator from a channel reference. |
| 133 | * |
| 134 | * @param chan Reference to the channel this iterator will write into. |
| 135 | */ |
| 136 | explicit blocking_writer_iterator(Channel& chan) : chan_{&chan} {} |
| 137 | |
| 138 | /** |
| 139 | * @brief Writes an element into the channel, blocking until space is available. |
| 140 | * |
| 141 | * @param value The value to be written into the channel. |
| 142 | * @return The iterator itself. |
| 143 | * @note There is no effect if the channel is closed. |
| 144 | */ |
| 145 | blocking_writer_iterator& operator=(reference value) |
| 146 | { |
| 147 | chan_->write(value); |
| 148 | return *this; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @brief Not applicable (handled by operator=). |
| 153 | * |
| 154 | * @return The iterator itself. |
| 155 | * |
| 156 | * @note It's uncommon to return a reference to an iterator, but I don't want to return a value from the channel. |
| 157 | * This iterator is supposed to be used only to write values. |
| 158 | * I don't know if it's a terrible idea or not, but it looks related to the issue with MSVC |
| 159 | * in the Transform test in tests/channel_test.cpp. |
| 160 | */ |
| 161 | blocking_writer_iterator& operator*() { return *this; } |