From 85ca6d14f008c15c54aad38793490b8f6f0f2749 Mon Sep 17 00:00:00 2001 From: Sean Harmer Date: Tue, 1 Oct 2024 09:37:02 +0100 Subject: [PATCH 1/4] Add example CMakeUserPresets.json file Useful on Windows if you have the LLVM distro installed for tools such as clang-format. When this is in the path the standard "dev" preset finds clang in preference to cl when configuring. The example "msvc_x64-dev" helps to find cl. Just copy and rename to CMakeUserPresets.json to use and customize. Added to .gitignore to prevent accidental commits of local modifications. --- .gitignore | 1 + CMakeUserPresets-example.json | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 CMakeUserPresets-example.json diff --git a/.gitignore b/.gitignore index 366f060..6a96065 100644 --- a/.gitignore +++ b/.gitignore @@ -73,6 +73,7 @@ Thumbs.db CMakeLists.txt.user CMakeLists.txt.user.* +CMakeUserPresets.json *.spv .vs/* imgui.ini diff --git a/CMakeUserPresets-example.json b/CMakeUserPresets-example.json new file mode 100644 index 0000000..50dbf79 --- /dev/null +++ b/CMakeUserPresets-example.json @@ -0,0 +1,22 @@ +{ + "version": 3, + "configurePresets": [ + { + "name": "msvc_x64-dev", + "displayName": "dev (msvc_x64)", + "inherits": ["dev"], + "architecture": { + "value": "x64", + "strategy": "external" + }, + "toolset": { + "value": "host=x64", + "strategy": "external" + }, + "cacheVariables": { + "CMAKE_C_COMPILER": "cl", + "CMAKE_CXX_COMPILER": "cl" + } + } + ] +} From bf6a0f60de62c902f3c3171ae97a1b4a0b5efc32 Mon Sep 17 00:00:00 2001 From: Sean Harmer Date: Tue, 1 Oct 2024 11:23:57 +0100 Subject: [PATCH 2/4] Add a convenience for single shot connections Although this is a trivial wrapper around connectReflective it is useful to have this to make it easy to find and reduce the burden on the developer. --- src/kdbindings/signal.h | 24 ++++++++++++++++++++++++ tests/signal/tst_signal.cpp | 7 ++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/kdbindings/signal.h b/src/kdbindings/signal.h index d0b1f04..beb0845 100644 --- a/src/kdbindings/signal.h +++ b/src/kdbindings/signal.h @@ -362,6 +362,30 @@ class Signal return ConnectionHandle{ m_impl, m_impl->connectReflective(slot) }; } + /** + * Establishes a single-shot connection between a signal and a slot and when the signal is emitted, the connection will be + * disconnected and the slot will be called. Note that the slot will be disconnected before it is called. If the slot + * triggers another signal emission of the same signal, the slot will not be called again. + * + * @param slot A std::function that takes the signal's parameter types. + * @return An instance of ConnectionHandle, that can be used to disconnect. + * + * @warning Connecting functions to a signal that throw an exception when called is currently undefined behavior. + * All connected functions should handle their own exceptions. + * For backwards-compatibility, the slot function is not required to be noexcept. + */ + KDBINDINGS_WARN_UNUSED ConnectionHandle connectSingleShot(std::function const &slot) + { + ensureImpl(); + + auto singleShotSlot = [slot](ConnectionHandle &handle, Args... args) { + handle.disconnect(); + slot(args...); + }; + + return ConnectionHandle{ m_impl, m_impl->connectReflective(singleShotSlot) }; + } + /** * @brief Establishes a deferred connection between the provided evaluator and slot. * diff --git a/tests/signal/tst_signal.cpp b/tests/signal/tst_signal.cpp index 9a9e7d8..1fb5008 100644 --- a/tests/signal/tst_signal.cpp +++ b/tests/signal/tst_signal.cpp @@ -270,12 +270,9 @@ TEST_CASE("Signal connections") Signal mySignal; int val = 5; - // Connect a reflective slot to the signal - auto handle = mySignal.connectReflective([&val](ConnectionHandle &selfHandle, int value) { + // Connect a single shot slot to the signal + auto handle = mySignal.connectSingleShot([&val](int value) { val += value; - - // Disconnect after handling the signal once - selfHandle.disconnect(); }); mySignal.emit(5); // This should trigger the slot and then disconnect it From fa28d3672c6201f979c8119eb7c2f484e3c202f0 Mon Sep 17 00:00:00 2001 From: Sean Harmer Date: Tue, 1 Oct 2024 15:03:58 +0100 Subject: [PATCH 3/4] Update src/kdbindings/signal.h Just call connectReflective. Co-authored-by: Leon Matthes --- src/kdbindings/signal.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/kdbindings/signal.h b/src/kdbindings/signal.h index beb0845..feec999 100644 --- a/src/kdbindings/signal.h +++ b/src/kdbindings/signal.h @@ -376,14 +376,10 @@ class Signal */ KDBINDINGS_WARN_UNUSED ConnectionHandle connectSingleShot(std::function const &slot) { - ensureImpl(); - - auto singleShotSlot = [slot](ConnectionHandle &handle, Args... args) { + return connectReflective([slot](ConnectionHandle &handle, Args... args) { handle.disconnect(); slot(args...); - }; - - return ConnectionHandle{ m_impl, m_impl->connectReflective(singleShotSlot) }; + } } /** From 76531306021019dfa73c4b2dc01a2ae1b874dc9a Mon Sep 17 00:00:00 2001 From: Sean Harmer Date: Tue, 1 Oct 2024 15:22:17 +0100 Subject: [PATCH 4/4] Fix syntax --- src/kdbindings/signal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kdbindings/signal.h b/src/kdbindings/signal.h index feec999..d157dd6 100644 --- a/src/kdbindings/signal.h +++ b/src/kdbindings/signal.h @@ -379,7 +379,7 @@ class Signal return connectReflective([slot](ConnectionHandle &handle, Args... args) { handle.disconnect(); slot(args...); - } + }); } /**