Skip to content

Return reference from iterator #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading