Skip to content

Commit

Permalink
fix: KDBINDINGS_WARN_UNUSED on templated connect
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonMatthesKDAB committed Jun 14, 2024
1 parent 90a96ce commit 1122255
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 21 deletions.
3 changes: 2 additions & 1 deletion examples/02-signal-member/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 5 additions & 2 deletions examples/03-member-arguments/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
11 changes: 8 additions & 3 deletions examples/07-advanced-connections/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,28 @@ class SignalHandler
int main()
{
Signal<int> signal;
std::vector<ConnectionHandle> 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
signal.emit(5);

std::cout << std::boolalpha << handler.received << std::endl;

for (auto &connection : connections) {
connection.disconnect();
}

return 0;
}
3 changes: 1 addition & 2 deletions src/kdbindings/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#pragma once


#include <assert.h>
#include <memory>
#include <stdexcept>
Expand Down Expand Up @@ -325,7 +324,7 @@ class Signal
// std::function<void(Args...)>, as it otherwise tries to take precedence
// over the normal connect function.
template<typename Func, typename... FuncArgs, typename = std::enable_if_t<std::disjunction_v<std::negation<std::is_convertible<Func, std::function<void(Args...)>>>, std::integral_constant<bool, sizeof...(FuncArgs) /*Also enable this function if we want to bind at least one argument*/>>>>
ConnectionHandle connect(Func &&slot, FuncArgs &&...args)
KDBINDINGS_WARN_UNUSED ConnectionHandle connect(Func &&slot, FuncArgs &&...args)
{
std::function<void(Args...)> bound = Private::bind_first(std::forward<Func>(slot), std::forward<FuncArgs>(args)...);
return connect(bound);
Expand Down
2 changes: 1 addition & 1 deletion tests/binding/tst_binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ TEST_CASE("Binding reassignment")
Property<int> source(0);
auto bound = makeBoundProperty(source);

bound.valueChanged().connect([&called]() { called = true; });
(void)bound.valueChanged().connect([&called]() { called = true; });

REQUIRE_FALSE(called);

Expand Down
12 changes: 6 additions & 6 deletions tests/property/tst_property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -190,7 +190,7 @@ TEST_CASE("Equality")

Property<EqualityTestStruct> property(EqualityTestStruct{ 0 });

property.valueChanged().connect([&callCount]() { ++callCount; });
(void)property.valueChanged().connect([&callCount]() { ++callCount; });

property = EqualityTestStruct{ 1 };
REQUIRE(callCount == 1);
Expand Down Expand Up @@ -298,7 +298,7 @@ TEST_CASE("Moving")
auto handlerValue = [&countValue](const std::unique_ptr<int> &) { ++countValue; };

auto property = Property<std::unique_ptr<int>>{ std::make_unique<int>(42) };
property.valueChanged().connect(handlerVoid);
(void)property.valueChanged().connect(handlerVoid);
(void)property.valueChanged().connect(handlerValue);

auto movedProperty{ std::move(property) };
Expand Down
12 changes: 6 additions & 6 deletions tests/signal/tst_signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class CallbackCounter
template<typename Signal>
CallbackCounter(Signal &s)
{
s.connect(&CallbackCounter::callback, this);
(void)s.connect(&CallbackCounter::callback, this);
}

void callback()
Expand Down Expand Up @@ -310,7 +310,7 @@ TEST_CASE("Signal connections")
Signal<bool, int> 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);

Expand All @@ -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);
Expand All @@ -347,10 +347,10 @@ TEST_CASE("Signal connections")

// disambiguation necessary, as push_back is overloaded.
void (std::vector<int>::*push_back)(const int &) = &std::vector<int>::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

Expand Down

0 comments on commit 1122255

Please sign in to comment.