From bc092d5b7384f21dc63db366d2f14cb9ec41e83f Mon Sep 17 00:00:00 2001 From: Sean Parent Date: Thu, 22 Feb 2024 18:02:01 -0800 Subject: [PATCH] Some minor fixes (#537) * Minor bug fixes around operations that require mutability. * Replacing an incorrect identity function with std::identity{} * Removed unnecessary moves. --- README.md | 15 ++------ stlab/concurrency/await.hpp | 6 ++- stlab/concurrency/default_executor.hpp | 2 +- stlab/concurrency/future.hpp | 52 ++++++++++++-------------- stlab/concurrency/system_timer.hpp | 2 +- 5 files changed, 33 insertions(+), 44 deletions(-) mode change 100755 => 100644 stlab/concurrency/future.hpp diff --git a/README.md b/README.md index 8481322c..4756aefa 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,7 @@ Release changelogs are listed in [CHANGES.md](CHANGES.md). ## Requirements -- A standards-compliant C++14, C++17, or C++20 compiler -- **Use with a compiler in C++14-compliant mode** requires Boost.Optional and Boost.Variant >= 1.74.0 +- A standards-compliant C++17, C++20, or C++23 compiler - **Building** the library requires CMake 3.23 or later - **Testing or developing** the library requires Boost.Test >= 1.74.0 @@ -41,16 +40,10 @@ for an introduction to this tool. 1. Create a build directory outside this library's source tree. In this guide, we'll use a sibling directory called `BUILD`. -2. If you are using the library in C++14-compliant mode or need to run the test suite, be sure you - have the necessary parts of Boost >= 1.74.0 installed. Linux distributions usually make a - suitable version available through standard package managers. On macOS or Linux, you can easilly - install Boost using [Homebrew](https://brew.sh/). To install Boost on Windows, you can use - Boost's [binary installers](https://sourceforge.net/projects/boost/files/boost-binaries/). - -3. Install a version of CMake >= 3.23. If you are on Debian or Ubuntu Linux you may need to use +1. Install a version of CMake >= 3.23. If you are on Debian or Ubuntu Linux you may need to use `snap` to find one that's new enough. -4. If you are using MSVC, you may need to set environment variables appropriately for your target +1. If you are using MSVC, you may need to set environment variables appropriately for your target architecture by invoking `VCVARSALL.BAT` with an appropriate option. ### Configure @@ -65,7 +58,7 @@ cmake -S . -B ../BUILD -DCMAKE_BUILD_TYPE=# SEE BELOW but there are other options you may need to append in order to be successful. Among them: * `-DCMAKE_BUILD_TYPE=`[`Release`|`Debug`] to build the given configuration (required unless you're using visual studio or another multi-config generator). -* `-DCMAKE_CXX_STANDARD=`[`14`|`17`|`20`|`23`] to build with compliance to the given C++ standard. +* `-DCMAKE_CXX_STANDARD=`[`17`|`20`|`23`] to build with compliance to the given C++ standard. * `-DBUILD_TESTING=OFF` if you only intend to build, but not test, this library. * `-DBoost_USE_STATIC_LIBS=TRUE` if you will be testing on Windows. diff --git a/stlab/concurrency/await.hpp b/stlab/concurrency/await.hpp index ceeb9b4b..0e24c470 100644 --- a/stlab/concurrency/await.hpp +++ b/stlab/concurrency/await.hpp @@ -99,8 +99,10 @@ T await(future x) { std::condition_variable condition; bool flag{false}; + future result; + auto hold = std::move(x).recover(immediate_executor, [&](future&& r) { - x = std::move(r); + result = std::move(r); { std::unique_lock lock{m}; flag = true; @@ -136,7 +138,7 @@ T await(future x) { #endif - return detail::_get_ready_future{}(std::move(x)); + return detail::_get_ready_future{}(std::move(result)); } namespace detail { diff --git a/stlab/concurrency/default_executor.hpp b/stlab/concurrency/default_executor.hpp index 506aa471..63c7f432 100644 --- a/stlab/concurrency/default_executor.hpp +++ b/stlab/concurrency/default_executor.hpp @@ -64,7 +64,7 @@ constexpr auto platform_priority(executor_priority p) { case executor_priority::low: return DISPATCH_QUEUE_PRIORITY_LOW; default: - assert(!"Unknown value!"); + assert(false && "Unknown value!"); } return DISPATCH_QUEUE_PRIORITY_DEFAULT; } diff --git a/stlab/concurrency/future.hpp b/stlab/concurrency/future.hpp old mode 100755 new mode 100644 index 17b297d5..b355a778 --- a/stlab/concurrency/future.hpp +++ b/stlab/concurrency/future.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -567,34 +568,28 @@ class packaged_task { packaged_task() = default; ~packaged_task() { - auto p = _p.lock(); - if (p) p->remove_promise(); + if (auto p = _p.lock()) p->remove_promise(); } packaged_task(const packaged_task& x) : _p(x._p) { - auto p = _p.lock(); - if (p) p->add_promise(); + if (auto p = _p.lock()) p->add_promise(); } packaged_task(packaged_task&&) noexcept = default; - packaged_task& operator=(const packaged_task& x) { - auto tmp = x; - *this = std::move(tmp); - return *this; - } + + packaged_task& operator=(const packaged_task& x) { return *this = packaged_task{x}; } + packaged_task& operator=(packaged_task&& x) noexcept = default; template void operator()(A&&... args) const noexcept { - auto p = _p.lock(); - if (p) (*p)(std::forward(args)...); + if (auto p = _p.lock()) (*p)(std::forward(args)...); } bool canceled() const { return _p.expired(); } void set_exception(std::exception_ptr error) const { - auto p = _p.lock(); - if (p) p->set_error(std::move(error)); + if (auto p = _p.lock()) p->set_error(std::move(error)); } }; @@ -636,7 +631,7 @@ class STLAB_NODISCARD() future> { template auto then(F&& f) const& { - return recover([_f = std::forward(f)](future&& p) { + return recover([_f = std::forward(f)](future&& p) mutable { return std::move(_f)(*std::move(p).get_try()); }); } @@ -649,7 +644,7 @@ class STLAB_NODISCARD() future> { template auto then(E&& executor, F&& f) const& { return recover(std::forward(executor), - [_f = std::forward(f)](future&& p) { + [_f = std::forward(f)](future&& p) mutable { return std::move(_f)(*std::move(p).get_try()); }); } @@ -781,7 +776,7 @@ class STLAB_NODISCARD() future { template auto then(F&& f) const& { - return recover([_f = std::forward(f)](future&& p) { + return recover([_f = std::forward(f)](future&& p) mutable { std::move(p).get_try(); return std::move(_f)(); }); @@ -795,7 +790,7 @@ class STLAB_NODISCARD() future { template auto then(E&& executor, F&& f) const& { return recover(std::forward(executor), - [_f = std::forward(f)](future&& p) { + [_f = std::forward(f)](future&& p) mutable { (void)std::move(p).get_try(); return std::move(_f)(); }); @@ -1244,7 +1239,7 @@ struct make_when_any { using result_t = detail::result_t; auto shared = std::make_shared>(); - auto p = package(executor, [_f = std::move(f), _p = shared] { + auto p = package(executor, [_f = std::move(f), _p = shared]() mutable { return detail::apply_when_any_arg(_f, _p); }); shared->_f = std::move(p.first); @@ -1264,7 +1259,7 @@ struct make_when_any { using result_t = detail::result_t; auto shared = std::make_shared>(); - auto p = package(executor, [_f = std::forward(f), _p = shared] { + auto p = package(executor, [_f = std::forward(f), _p = shared]() mutable { return detail::apply_when_any_arg(_f, _p); }); shared->_f = std::move(p.first); @@ -1566,7 +1561,7 @@ struct _reduce_coroutine : std::enable_shared_from_this<_reduce_coroutine> _this->_promise.set_exception(e); return; } - _this->stage_0(std::move(a)); + _this->stage_0(std::forward(a)); }); } void stage_0(future>&& r) { @@ -1672,12 +1667,13 @@ auto async(E executor, F&& f, Args&&... args) using result_type = detail::result_t, std::decay_t...>; auto p = package( - executor, std::bind( - [_f = std::forward(f)]( - unwrap_reference_t>&... args) mutable -> result_type { - return _f(move_if>>(args)...); - }, - std::forward(args)...)); + executor, + std::bind( + [_f = std::forward(f)]( + unwrap_reference_t>&... args) mutable -> result_type { + return std::move(_f)(move_if>>(args)...); + }, + std::forward(args)...)); executor(std::move(p.first)); @@ -1813,9 +1809,7 @@ struct std::coroutine_traits, Args...> { std::pair, stlab::future> _promise; promise_type() { - _promise = stlab::package(stlab::immediate_executor, [](auto&& x) -> decltype(x) { - return std::forward(x); - }); + _promise = stlab::package(stlab::immediate_executor, std::identity{}); } stlab::future get_return_object() { return std::move(_promise.second); } diff --git a/stlab/concurrency/system_timer.hpp b/stlab/concurrency/system_timer.hpp index 1c85da9c..46711469 100755 --- a/stlab/concurrency/system_timer.hpp +++ b/stlab/concurrency/system_timer.hpp @@ -224,7 +224,7 @@ class system_timer { [[deprecated("Use chrono::duration as parameter instead")]] void operator()( std::chrono::steady_clock::time_point when, F&& f) { using namespace std::chrono; - operator()(when - steady_clock::now(), std::move(f)); + operator()(when - steady_clock::now(), std::forward(f)); } template >