C++: Fixed sixtyfps::blocking_invoke_from_main_loop when the callable returns void

Fixes #623
This commit is contained in:
Olivier Goffart 2021-11-01 10:46:40 +01:00
parent a102e9ed8d
commit d26e95fb95
3 changed files with 21 additions and 2 deletions

View file

@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
### Fixed ### Fixed
- The Slider's changed callback not being called with the fluent style (#621) - The Slider's changed callback not being called with the fluent style (#621)
- C++: Fixed compilation error in `sixtyfps::blocking_invoke_from_main_loop` when the callable returns `void` (#623)
## [0.1.4] - 2021-10-22 ## [0.1.4] - 2021-10-22

View file

@ -855,7 +855,7 @@ void invoke_from_event_loop(Functor f)
/// ... /// ...
/// } /// }
/// ``` /// ```
template<typename Functor> template<typename Functor, typename = std::enable_if_t<!std::is_void_v<std::invoke_result_t<Functor>>>>
auto blocking_invoke_from_event_loop(Functor f) -> std::invoke_result_t<Functor> { auto blocking_invoke_from_event_loop(Functor f) -> std::invoke_result_t<Functor> {
std::optional<std::invoke_result_t<Functor>> result; std::optional<std::invoke_result_t<Functor>> result;
std::mutex mtx; std::mutex mtx;
@ -871,6 +871,21 @@ auto blocking_invoke_from_event_loop(Functor f) -> std::invoke_result_t<Functor>
return std::move(*result); return std::move(*result);
} }
template<typename Functor, typename = std::enable_if_t<std::is_void_v<std::invoke_result_t<Functor>>>>
auto blocking_invoke_from_event_loop(Functor f) -> void {
std::mutex mtx;
std::condition_variable cv;
bool ok = false;
invoke_from_event_loop([&] {
f();
std::unique_lock lock(mtx);
ok = true;
cv.notify_one();
});
std::unique_lock lock(mtx);
cv.wait(lock, [&] { return ok; });
}
namespace private_api { namespace private_api {
/// Registers a font by the specified path. The path must refer to an existing /// Registers a font by the specified path. The path must refer to an existing

View file

@ -67,9 +67,12 @@ TEST_CASE("Blocking Event from thread")
return std::make_unique<int>(42); return std::make_unique<int>(42);
}); });
called = *foo; called = *foo;
sixtyfps::invoke_from_event_loop([&] { int xxx = 123;
sixtyfps::blocking_invoke_from_event_loop([&] {
sixtyfps::quit_event_loop(); sixtyfps::quit_event_loop();
xxx = 888999;
}); });
REQUIRE(xxx == 888999);
}); });
sixtyfps::run_event_loop(); sixtyfps::run_event_loop();