From 7ad7e3861822131e494d270170eb36cb006c26e1 Mon Sep 17 00:00:00 2001 From: Andrei Avram <6795248+andreiavrammsd@users.noreply.github.com> Date: Sun, 11 Aug 2024 09:21:21 +0300 Subject: [PATCH] Return reference from iterator (#40) Fixes blocking iterator by returning (const) reference type from the dereference operator. --- .vscode/settings.json | 2 +- CMakeLists.txt | 2 +- examples/cmake-project/CMakeLists.txt | 2 +- include/msd/blocking_iterator.hpp | 24 ++++++++++++------------ tests/blocking_iterator_test.cpp | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index f5f323b..29e6e34 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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", diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ee9854..b1be169 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/examples/cmake-project/CMakeLists.txt b/examples/cmake-project/CMakeLists.txt index 52de5b1..44d6b94 100644 --- a/examples/cmake-project/CMakeLists.txt +++ b/examples/cmake-project/CMakeLists.txt @@ -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 diff --git a/include/msd/blocking_iterator.hpp b/include/msd/blocking_iterator.hpp index 9e5c79b..251f189 100644 --- a/include/msd/blocking_iterator.hpp +++ b/include/msd/blocking_iterator.hpp @@ -20,9 +20,9 @@ template 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. @@ -32,12 +32,11 @@ 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_; } /** @@ -45,26 +44,27 @@ class blocking_iterator { */ bool operator!=(blocking_iterator) const { - std::unique_lock lock{ch_.mtx_}; - ch_.waitBeforeRead(lock); + std::unique_lock 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 struct std::iterator_traits> { using value_type = typename msd::blocking_iterator::value_type; using reference = typename msd::blocking_iterator::reference; - using iterator_category = std::output_iterator_tag; + using iterator_category = std::input_iterator_tag; }; #endif // MSD_CHANNEL_BLOCKING_ITERATOR_HPP_ diff --git a/tests/blocking_iterator_test.cpp b/tests/blocking_iterator_test.cpp index 19bb92c..674db82 100644 --- a/tests/blocking_iterator_test.cpp +++ b/tests/blocking_iterator_test.cpp @@ -12,7 +12,7 @@ TEST(ChannelIteratorTest, Traits) using iterator_traits = std::iterator_traits; EXPECT_TRUE((std::is_same::value)); - EXPECT_TRUE((std::is_same::value)); + EXPECT_TRUE((std::is_same::value)); } TEST(ChannelIteratorTest, Dereference)