diff --git a/examples/02-signal-member/main.cpp b/examples/02-signal-member/main.cpp index 6852614..5674fcb 100644 --- a/examples/02-signal-member/main.cpp +++ b/examples/02-signal-member/main.cpp @@ -36,8 +36,9 @@ int main() Button button; Message message; - button.clicked.connect(&Message::display, &message); + auto connection = button.clicked.connect(&Message::display, &message); button.clicked.emit(); + connection.disconnect(); return 0; } diff --git a/examples/03-member-arguments/main.cpp b/examples/03-member-arguments/main.cpp index 1cf91bb..33c9e32 100644 --- a/examples/03-member-arguments/main.cpp +++ b/examples/03-member-arguments/main.cpp @@ -40,11 +40,14 @@ int main() Person alice("Alice"); Person bob("Bob"); - alice.speak.connect(&Person::listen, &bob); - bob.speak.connect(&Person::listen, &alice); + auto connection1 = alice.speak.connect(&Person::listen, &bob); + auto connection2 = bob.speak.connect(&Person::listen, &alice); alice.speak.emit("Have a nice day!"); bob.speak.emit("Thank you!"); + connection1.disconnect(); + connection2.disconnect(); + return 0; } diff --git a/examples/07-advanced-connections/main.cpp b/examples/07-advanced-connections/main.cpp index 8489f7f..eb08cc7 100644 --- a/examples/07-advanced-connections/main.cpp +++ b/examples/07-advanced-connections/main.cpp @@ -41,17 +41,18 @@ class SignalHandler int main() { Signal signal; + std::vector connections; // Signal::connect allows connecting functions that take too few arguments. - signal.connect(display); + connections.emplace_back(signal.connect(display)); // As well as functions with too many arguments, as long as default values are provided. - signal.connect(displayLabelled, "Emitted value"); + connections.emplace_back(signal.connect(displayLabelled, "Emitted value")); // This is very useful to connect member functions, where the first implicit argument // is a pointer to the "this" object. SignalHandler handler; - signal.connect(&SignalHandler::receive, &handler); + connections.emplace_back(signal.connect(&SignalHandler::receive, &handler)); // This will print "Hello World!" and "Emitted value: 5" in an unspecified order. // It will also set handler.received to true @@ -59,5 +60,9 @@ int main() std::cout << std::boolalpha << handler.received << std::endl; + for (auto &connection : connections) { + connection.disconnect(); + } + return 0; } diff --git a/src/kdbindings/signal.h b/src/kdbindings/signal.h index 4e07bbb..c006f43 100644 --- a/src/kdbindings/signal.h +++ b/src/kdbindings/signal.h @@ -11,7 +11,6 @@ #pragma once - #include #include #include @@ -325,7 +324,7 @@ class Signal // std::function, as it otherwise tries to take precedence // over the normal connect function. template>>, std::integral_constant>>> - ConnectionHandle connect(Func &&slot, FuncArgs &&...args) + KDBINDINGS_WARN_UNUSED ConnectionHandle connect(Func &&slot, FuncArgs &&...args) { std::function bound = Private::bind_first(std::forward(slot), std::forward(args)...); return connect(bound); diff --git a/tests/binding/tst_binding.cpp b/tests/binding/tst_binding.cpp index 38c3801..d502a8a 100644 --- a/tests/binding/tst_binding.cpp +++ b/tests/binding/tst_binding.cpp @@ -342,7 +342,7 @@ TEST_CASE("Binding reassignment") Property source(0); auto bound = makeBoundProperty(source); - bound.valueChanged().connect([&called]() { called = true; }); + (void)bound.valueChanged().connect([&called]() { called = true; }); REQUIRE_FALSE(called); diff --git a/tests/property/tst_property.cpp b/tests/property/tst_property.cpp index 34cbeb1..d436c79 100644 --- a/tests/property/tst_property.cpp +++ b/tests/property/tst_property.cpp @@ -129,8 +129,8 @@ TEST_CASE("Signals") Handler handler; HandlerAboutToChange aboutToChangeHandler; - property.valueChanged().connect(&Handler::doSomething, &handler); - property.valueAboutToChange().connect(&HandlerAboutToChange::doSomething, &aboutToChangeHandler); + (void)property.valueChanged().connect(&Handler::doSomething, &handler); + (void)property.valueAboutToChange().connect(&HandlerAboutToChange::doSomething, &aboutToChangeHandler); property = 3; REQUIRE(property.get() == 3); @@ -144,8 +144,8 @@ TEST_CASE("Signals") Handler handler; HandlerAboutToChange aboutToChangeHandler; - property.valueChanged().connect(&Handler::doSomething, &handler); - property.valueAboutToChange().connect(&HandlerAboutToChange::doSomething, &aboutToChangeHandler); + (void)property.valueChanged().connect(&Handler::doSomething, &handler); + (void)property.valueAboutToChange().connect(&HandlerAboutToChange::doSomething, &aboutToChangeHandler); property = 7; REQUIRE(property.get() == 7); @@ -190,7 +190,7 @@ TEST_CASE("Equality") Property property(EqualityTestStruct{ 0 }); - property.valueChanged().connect([&callCount]() { ++callCount; }); + (void)property.valueChanged().connect([&callCount]() { ++callCount; }); property = EqualityTestStruct{ 1 }; REQUIRE(callCount == 1); @@ -298,7 +298,7 @@ TEST_CASE("Moving") auto handlerValue = [&countValue](const std::unique_ptr &) { ++countValue; }; auto property = Property>{ std::make_unique(42) }; - property.valueChanged().connect(handlerVoid); + (void)property.valueChanged().connect(handlerVoid); (void)property.valueChanged().connect(handlerValue); auto movedProperty{ std::move(property) }; diff --git a/tests/signal/tst_signal.cpp b/tests/signal/tst_signal.cpp index 48cad8e..5a34313 100644 --- a/tests/signal/tst_signal.cpp +++ b/tests/signal/tst_signal.cpp @@ -70,7 +70,7 @@ class CallbackCounter template CallbackCounter(Signal &s) { - s.connect(&CallbackCounter::callback, this); + (void)s.connect(&CallbackCounter::callback, this); } void callback() @@ -310,7 +310,7 @@ TEST_CASE("Signal connections") Signal signal; auto lambdaCalled = false; - signal.connect([&lambdaCalled](bool value) { lambdaCalled = value; }); + (void)signal.connect([&lambdaCalled](bool value) { lambdaCalled = value; }); signal.emit(true, 5); REQUIRE(lambdaCalled); @@ -324,11 +324,11 @@ TEST_CASE("Signal connections") auto signalValue = 0; auto boundValue = 0; - signal.connect([&signalValue, &boundValue](int bound, int signalled) { + (void)signal.connect([&signalValue, &boundValue](int bound, int signalled) { boundValue = bound; signalValue = signalled; }, - 5); + 5); // The bound value should not have changed yet. REQUIRE(boundValue == 0); @@ -347,10 +347,10 @@ TEST_CASE("Signal connections") // disambiguation necessary, as push_back is overloaded. void (std::vector::*push_back)(const int &) = &std::vector::push_back; - signal.connect(push_back, &numbers); + (void)signal.connect(push_back, &numbers); // this slot doesn't require the int argument, so it will be discarded. - signal.connect([&emitted]() { emitted = true; }); + (void)signal.connect([&emitted]() { emitted = true; }); signal.emit(4); // Will add 4 to the vector and set emitted to true