If a pointer-to-member is passed to the Signal::connect() function with
arguments that don't match the Signal type, the pointer-to-member
version of connect() will not match during template argument resolution,
but the functor version will. This results in a compilation error in the
BoundMethodFunctor class, due to the pointer-to-member not being a
functor and thus not being callable directly. The error messages are
quite cryptic. With the following error applied,
diff --git a/test/signal.cpp b/test/signal.cpp
index 5c6b304dac0b..6dd11ac45313 100644
--- a/test/signal.cpp
+++ b/test/signal.cpp
@@ -107,6 +107,7 @@ protected:
/* Test signal emission and reception. */
called_ = false;
signalVoid_.connect(this, &SignalTest::slotVoid);
+ signalVoid_.connect(this, &SignalTest::slotInteger1);
signalVoid_.emit();
if (!called_) {
gcc outputs
../../include/libcamera/base/bound_method.h: In instantiation of ‘R libcamera::BoundMethodFunctor<T, R, Func, Args>::activate(Args ..., bool) [with T = SignalTest; R = void; Func = void (SignalTest::*)(int); Args = {}]’:
../../include/libcamera/base/bound_method.h:143:4: required from here
../../include/libcamera/base/bound_method.h:146:37: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘((libcamera::BoundMethodFunctor<SignalTest, void, void (SignalTest::*)(int)>*)this)->libcamera::BoundMethodFunctor<SignalTest, void, void (SignalTest::*)(int)>::func_ (...)’, e.g. ‘(... ->* ((libcamera::BoundMethodFunctor<SignalTest, void, void (SignalTes
t::*)(int)>*)this)->libcamera::BoundMethodFunctor<SignalTest, void, void (SignalTest::*)(int)>::func_) (...)’
146 | return func_(args...);
| ~~~~~^~~~~~~~~
and clang isn't much better:
../../include/libcamera/base/bound_method.h:146:11: error: called object type 'void (SignalTest::*)(int)' is not a function or function pointer
return func_(args...);
^~~~~
../../include/libcamera/base/bound_method.h:137:2: note: in instantiation of member function 'libcamera::BoundMethodFunctor<SignalTest, void, void (SignalTest::*)(int)>::activate' requested here
BoundMethodFunctor(T *obj, Object *object, Func func,
^
../../include/libcamera/base/signal.h:80:27: note: in instantiation of member function 'libcamera::BoundMethodFunctor<SignalTest, void, void (SignalTest::*)(int)>::BoundMethodFunctor' requested here
SignalBase::connect(new BoundMethodFunctor<T, void, Func, Args...>(obj, nullptr, func));
^
../../test/signal.cpp:110:15: note: in instantiation of function template specialization 'libcamera::Signal<>::connect<SignalTest, void (SignalTest::*)(int), nullptr>' requested here
signalVoid_.connect(this, &SignalTest::slotInteger1);
^
Improve error reporting by disabling the functor version of connect()
when the Func argument isn't invocable with the Signal arguments. gcc
will then complain with
../../test/signal.cpp:110:36: error: no matching function for call to ‘libcamera::Signal<>::connect(SignalTest*, void (SignalTest::*)(int))’
110 | signalVoid_.connect(this, &SignalTest::slotInteger1);
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
and clang with
../../test/signal.cpp:110:15: error: no matching member function for call to 'connect'
signalVoid_.connect(this, &SignalTest::slotInteger1);
~~~~~~~~~~~~^~~~~~~
which are more readable.
This change requires usage of std::is_invocable<>, which is only
available starting in C++17. This is fine for usage of the Signal class
within libcamera, as the project is compiled with C++17, but we try to
keep the public API compatible C++14. Condition the additional checks
based on the C++ version.
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>
Acked-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
158 lines
3.7 KiB
C++
158 lines
3.7 KiB
C++
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
|
/*
|
|
* Copyright (C) 2019, Google Inc.
|
|
*
|
|
* signal.h - Signal & slot implementation
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <functional>
|
|
#include <list>
|
|
#include <type_traits>
|
|
#include <vector>
|
|
|
|
#include <libcamera/base/bound_method.h>
|
|
#include <libcamera/base/object.h>
|
|
|
|
namespace libcamera {
|
|
|
|
class SignalBase
|
|
{
|
|
public:
|
|
void disconnect(Object *object);
|
|
|
|
protected:
|
|
using SlotList = std::list<BoundMethodBase *>;
|
|
|
|
void connect(BoundMethodBase *slot);
|
|
void disconnect(std::function<bool(SlotList::iterator &)> match);
|
|
|
|
SlotList slots();
|
|
|
|
private:
|
|
SlotList slots_;
|
|
};
|
|
|
|
template<typename... Args>
|
|
class Signal : public SignalBase
|
|
{
|
|
public:
|
|
~Signal()
|
|
{
|
|
disconnect();
|
|
}
|
|
|
|
#ifndef __DOXYGEN__
|
|
template<typename T, typename R, std::enable_if_t<std::is_base_of<Object, T>::value> * = nullptr>
|
|
void connect(T *obj, R (T::*func)(Args...),
|
|
ConnectionType type = ConnectionTypeAuto)
|
|
{
|
|
Object *object = static_cast<Object *>(obj);
|
|
SignalBase::connect(new BoundMethodMember<T, R, Args...>(obj, object, func, type));
|
|
}
|
|
|
|
template<typename T, typename R, std::enable_if_t<!std::is_base_of<Object, T>::value> * = nullptr>
|
|
#else
|
|
template<typename T, typename R>
|
|
#endif
|
|
void connect(T *obj, R (T::*func)(Args...))
|
|
{
|
|
SignalBase::connect(new BoundMethodMember<T, R, Args...>(obj, nullptr, func));
|
|
}
|
|
|
|
#ifndef __DOXYGEN__
|
|
template<typename T, typename Func,
|
|
std::enable_if_t<std::is_base_of<Object, T>::value
|
|
#if __cplusplus >= 201703L
|
|
&& std::is_invocable_v<Func, Args...>
|
|
#endif
|
|
> * = nullptr>
|
|
void connect(T *obj, Func func, ConnectionType type = ConnectionTypeAuto)
|
|
{
|
|
Object *object = static_cast<Object *>(obj);
|
|
SignalBase::connect(new BoundMethodFunctor<T, void, Func, Args...>(obj, object, func, type));
|
|
}
|
|
|
|
template<typename T, typename Func,
|
|
std::enable_if_t<!std::is_base_of<Object, T>::value
|
|
#if __cplusplus >= 201703L
|
|
&& std::is_invocable_v<Func, Args...>
|
|
#endif
|
|
> * = nullptr>
|
|
#else
|
|
template<typename T, typename Func>
|
|
#endif
|
|
void connect(T *obj, Func func)
|
|
{
|
|
SignalBase::connect(new BoundMethodFunctor<T, void, Func, Args...>(obj, nullptr, func));
|
|
}
|
|
|
|
template<typename R>
|
|
void connect(R (*func)(Args...))
|
|
{
|
|
SignalBase::connect(new BoundMethodStatic<R, Args...>(func));
|
|
}
|
|
|
|
void disconnect()
|
|
{
|
|
SignalBase::disconnect([]([[maybe_unused]] SlotList::iterator &iter) {
|
|
return true;
|
|
});
|
|
}
|
|
|
|
template<typename T>
|
|
void disconnect(T *obj)
|
|
{
|
|
SignalBase::disconnect([obj](SlotList::iterator &iter) {
|
|
return (*iter)->match(obj);
|
|
});
|
|
}
|
|
|
|
template<typename T, typename R>
|
|
void disconnect(T *obj, R (T::*func)(Args...))
|
|
{
|
|
SignalBase::disconnect([obj, func](SlotList::iterator &iter) {
|
|
BoundMethodArgs<R, Args...> *slot =
|
|
static_cast<BoundMethodArgs<R, Args...> *>(*iter);
|
|
|
|
if (!slot->match(obj))
|
|
return false;
|
|
|
|
/*
|
|
* If the object matches the slot, the slot is
|
|
* guaranteed to be a member slot, so we can safely
|
|
* cast it to BoundMethodMember<T, Args...> to match
|
|
* func.
|
|
*/
|
|
return static_cast<BoundMethodMember<T, R, Args...> *>(slot)->match(func);
|
|
});
|
|
}
|
|
|
|
template<typename R>
|
|
void disconnect(R (*func)(Args...))
|
|
{
|
|
SignalBase::disconnect([func](SlotList::iterator &iter) {
|
|
BoundMethodArgs<R, Args...> *slot =
|
|
static_cast<BoundMethodArgs<R, Args...> *>(*iter);
|
|
|
|
if (!slot->match(nullptr))
|
|
return false;
|
|
|
|
return static_cast<BoundMethodStatic<R, Args...> *>(slot)->match(func);
|
|
});
|
|
}
|
|
|
|
void emit(Args... args)
|
|
{
|
|
/*
|
|
* Make a copy of the slots list as the slot could call the
|
|
* disconnect operation, invalidating the iterator.
|
|
*/
|
|
for (BoundMethodBase *slot : slots())
|
|
static_cast<BoundMethodArgs<void, Args...> *>(slot)->activate(args...);
|
|
}
|
|
};
|
|
|
|
} /* namespace libcamera */
|