Skip to content

Commit

Permalink
Return reference from iterator (#40)
Browse files Browse the repository at this point in the history
Fixes blocking iterator by returning (const) reference type from the dereference operator.
  • Loading branch information
andreiavrammsd authored Aug 11, 2024
1 parent ad66d30 commit 7ad7e38
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"-Dcpp_channel_build_tests=ON"
],
"editor.codeActionsOnSave": {
"source.fixAll": true
"source.fixAll": "explicit"
},
"editor.formatOnSave": true,
"clang-format.executable": "clang-format",
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.12)
project(cpp_channel)
set(PROJECT_VERSION 0.8.2)
set(PROJECT_VERSION 0.8.3)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
Expand Down
2 changes: 1 addition & 1 deletion examples/cmake-project/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ target_link_libraries(cmake_project)

include(FetchContent)
if (NOT channel_POPULATED)
FetchContent_Declare(channel URL https://github.com/andreiavrammsd/cpp-channel/archive/v0.8.1.zip)
FetchContent_Declare(channel URL https://github.com/andreiavrammsd/cpp-channel/archive/v0.8.3.zip)
FetchContent_Populate(channel)
include_directories(${channel_SOURCE_DIR}/include)
# OR
Expand Down
24 changes: 12 additions & 12 deletions include/msd/blocking_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ template <typename Channel>
class blocking_iterator {
public:
using value_type = typename Channel::value_type;
using reference = typename Channel::value_type&;
using reference = const typename Channel::value_type&;

explicit blocking_iterator(Channel& ch) : ch_{ch} {}
explicit blocking_iterator(Channel& chan) : chan_{chan} {}

/**
* Advances to next element in the channel.
Expand All @@ -32,39 +32,39 @@ class blocking_iterator {
/**
* Returns an element from the channel.
*/
value_type operator*() const
reference operator*()
{
value_type value{};
ch_ >> value;
chan_ >> value_;

return value;
return value_;
}

/**
* Makes iteration continue until the channel is closed and empty.
*/
bool operator!=(blocking_iterator<Channel>) const
{
std::unique_lock<std::mutex> lock{ch_.mtx_};
ch_.waitBeforeRead(lock);
std::unique_lock<std::mutex> lock{chan_.mtx_};
chan_.waitBeforeRead(lock);

return !(ch_.closed() && ch_.empty());
return !(chan_.closed() && chan_.empty());
}

private:
Channel& ch_;
Channel& chan_;
value_type value_{};
};

} // namespace msd

/**
* @brief Output iterator specialization
* @brief Input iterator specialization
*/
template <typename T>
struct std::iterator_traits<msd::blocking_iterator<T>> {
using value_type = typename msd::blocking_iterator<T>::value_type;
using reference = typename msd::blocking_iterator<T>::reference;
using iterator_category = std::output_iterator_tag;
using iterator_category = std::input_iterator_tag;
};

#endif // MSD_CHANNEL_BLOCKING_ITERATOR_HPP_
2 changes: 1 addition & 1 deletion tests/blocking_iterator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ TEST(ChannelIteratorTest, Traits)

using iterator_traits = std::iterator_traits<iterator>;
EXPECT_TRUE((std::is_same<iterator_traits::value_type, type>::value));
EXPECT_TRUE((std::is_same<iterator_traits::iterator_category, std::output_iterator_tag>::value));
EXPECT_TRUE((std::is_same<iterator_traits::iterator_category, std::input_iterator_tag>::value));
}

TEST(ChannelIteratorTest, Dereference)
Expand Down

0 comments on commit 7ad7e38

Please sign in to comment.