Skip to content

Commit 7ad7e38

Browse files
Return reference from iterator (#40)
Fixes blocking iterator by returning (const) reference type from the dereference operator.
1 parent ad66d30 commit 7ad7e38

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"-Dcpp_channel_build_tests=ON"
55
],
66
"editor.codeActionsOnSave": {
7-
"source.fixAll": true
7+
"source.fixAll": "explicit"
88
},
99
"editor.formatOnSave": true,
1010
"clang-format.executable": "clang-format",

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.12)
22
project(cpp_channel)
3-
set(PROJECT_VERSION 0.8.2)
3+
set(PROJECT_VERSION 0.8.3)
44

55
set(CMAKE_CXX_STANDARD 11)
66
set(CMAKE_CXX_STANDARD_REQUIRED YES)

examples/cmake-project/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ target_link_libraries(cmake_project)
1414

1515
include(FetchContent)
1616
if (NOT channel_POPULATED)
17-
FetchContent_Declare(channel URL https://github.com/andreiavrammsd/cpp-channel/archive/v0.8.1.zip)
17+
FetchContent_Declare(channel URL https://github.com/andreiavrammsd/cpp-channel/archive/v0.8.3.zip)
1818
FetchContent_Populate(channel)
1919
include_directories(${channel_SOURCE_DIR}/include)
2020
# OR

include/msd/blocking_iterator.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ template <typename Channel>
2020
class blocking_iterator {
2121
public:
2222
using value_type = typename Channel::value_type;
23-
using reference = typename Channel::value_type&;
23+
using reference = const typename Channel::value_type&;
2424

25-
explicit blocking_iterator(Channel& ch) : ch_{ch} {}
25+
explicit blocking_iterator(Channel& chan) : chan_{chan} {}
2626

2727
/**
2828
* Advances to next element in the channel.
@@ -32,39 +32,39 @@ class blocking_iterator {
3232
/**
3333
* Returns an element from the channel.
3434
*/
35-
value_type operator*() const
35+
reference operator*()
3636
{
37-
value_type value{};
38-
ch_ >> value;
37+
chan_ >> value_;
3938

40-
return value;
39+
return value_;
4140
}
4241

4342
/**
4443
* Makes iteration continue until the channel is closed and empty.
4544
*/
4645
bool operator!=(blocking_iterator<Channel>) const
4746
{
48-
std::unique_lock<std::mutex> lock{ch_.mtx_};
49-
ch_.waitBeforeRead(lock);
47+
std::unique_lock<std::mutex> lock{chan_.mtx_};
48+
chan_.waitBeforeRead(lock);
5049

51-
return !(ch_.closed() && ch_.empty());
50+
return !(chan_.closed() && chan_.empty());
5251
}
5352

5453
private:
55-
Channel& ch_;
54+
Channel& chan_;
55+
value_type value_{};
5656
};
5757

5858
} // namespace msd
5959

6060
/**
61-
* @brief Output iterator specialization
61+
* @brief Input iterator specialization
6262
*/
6363
template <typename T>
6464
struct std::iterator_traits<msd::blocking_iterator<T>> {
6565
using value_type = typename msd::blocking_iterator<T>::value_type;
6666
using reference = typename msd::blocking_iterator<T>::reference;
67-
using iterator_category = std::output_iterator_tag;
67+
using iterator_category = std::input_iterator_tag;
6868
};
6969

7070
#endif // MSD_CHANNEL_BLOCKING_ITERATOR_HPP_

tests/blocking_iterator_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ TEST(ChannelIteratorTest, Traits)
1212

1313
using iterator_traits = std::iterator_traits<iterator>;
1414
EXPECT_TRUE((std::is_same<iterator_traits::value_type, type>::value));
15-
EXPECT_TRUE((std::is_same<iterator_traits::iterator_category, std::output_iterator_tag>::value));
15+
EXPECT_TRUE((std::is_same<iterator_traits::iterator_category, std::input_iterator_tag>::value));
1616
}
1717

1818
TEST(ChannelIteratorTest, Dereference)

0 commit comments

Comments
 (0)