This is a follow-up to this question where I was encouraged to use perfect forwarding in a variadic template operator()(...) implementation. Here's my observer pattern which I want to use to call free functions and member functions with variadic arguments:
#ifndef _SIGNALS_H_
#define _SIGNALS_H_
#include <utility>
/** Interface for delegates with a specific set of arguments **/
template<typename... args>
class AbstractDelegate
{
public:
virtual void operator()(args&&...) const = 0;
virtual ~AbstractDelegate() {}
};
/** Concrete function delegate that discards the function's return value **/
template<typename ReturnType, typename... args>
class FnDelegate : public AbstractDelegate<args...>
{
public:
/** member function typedef **/
using Fn = ReturnType(*)(args...);
/** constructor **/
FnDelegate(Fn fn)
: fn_{fn}
{
}
/** call operator that calls the stored function **/
void operator()(args&&... a) const override
{
(*fn_)(std::forward<args>(a)...);
}
private:
/** function pointer **/
const Fn fn_;
};
/** forward declaration **/
template<typename... args>
class Connection;
/** Signal class that can be connected to**/
template<typename... args>
class Signal
{
public:
/** connection pointer typedef **/
typedef Connection<args...>* connection_p;
/** constructor **/
Signal()
: connections_(NULL),
blocked_(false)
{
}
/** call operator that notifes all connections associated with this Signal.
The most recently associated connection will be notified first **/
void operator()(args&&... a) const
{
// only notify connections if this signal is not blocked
if (!blocked())
{
auto c = connections_;
while(c != NULL)
{
(*c)(std::forward<args>(a)...);
c = c->next();
}
}
}
/** connect to this signal **/
void connect(connection_p p)
{
p->next_ = connections_;
connections_ = p;
p->signal_ = this;
}
/** disconnect from this signal.
Invalidates the connection's signal pointer
and removes the connection from the list **/
void disconnect(connection_p conn)
{
// find connection and remove it from the list
connection_p c = connections_;
if (c == conn)
{
connections_ = connections_->next();
conn->next_ = NULL;
conn->signal_ = NULL;
return;
}
while(c != NULL)
{
if (c->next() == conn)
{
c->next_ = conn->next();
conn->next_ = NULL;
conn->signal_ = NULL;
return;
}
c = c->next();
}
}
/** block events from this signal **/
void block()
{
blocked_ = true;
}
/** unblock events from this signal **/
void unblock()
{
blocked_ = false;
}
/** is this signal blocked? **/
bool blocked() const
{
return blocked_;
}
/** destructor. disconnects all connections **/
~Signal()
{
connection_p p = connections_;
while(p != NULL)
{
connection_p n = p->next();
disconnect(p);
p = n;
}
}
connection_p connections() const {return connections_;}
private:
connection_p connections_;
bool blocked_;
};
/** connection class that can be connected to a signal **/
template<typename... args>
class Connection
{
public:
/** template constructor for static member functions and free functions.
allocates a new delegate on the heap **/
template<typename ReturnType>
Connection(Signal<args...>& signal, ReturnType (*Fn)(args...))
: delegate_(new FnDelegate<ReturnType, args...>(Fn)),
signal_(NULL),
next_(NULL),
blocked_(false)
{
signal.connect(this);
}
/** get reference to this connection's delegate **/
AbstractDelegate<args...>& delegate() const
{
return *delegate_;
}
/** call this connection's delegate if not blocked **/
void operator()(args&&... a) const
{
if (!blocked())
{
delegate()(std::forward<args>(a)...);
}
}
/** get pointer to next connection in the signal's list **/
Connection* next() const
{
return next_;
}
/** is this connection connected to a valid signal? **/
bool connected() const
{
return (signal_ != NULL);
}
/** block events for this connection **/
void block()
{
blocked_ = true;
}
/** unblock events for this connection **/
void unblock()
{
blocked_ = false;
}
/** is this connection blocked? **/
bool blocked() const
{
return blocked_;
}
/** desctructor. If the signal is still alive, disconnects from it **/
~Connection()
{
if (signal_ != NULL)
{
signal_->disconnect(this);
}
delete delegate_;
}
const Signal<args...>* signal() const {return signal_;}
friend class Signal<args...>;
private:
AbstractDelegate<args...>* delegate_;
Signal<args...>* signal_;
Connection* next_;
bool blocked_;
};
/** free connect function: creates a connection (static member or free function) on the heap
that can be used anonymously **/
template<typename ReturnType, typename... args>
Connection<args...>* connect(Signal<args...>& signal, ReturnType (*fn)(args...))
{
return new Connection<args...>(signal, fn);
}
#endif // _SIGNALS_H_
I'm trying to use it in these ways:
Signal<int> sig;
void print(int i)
{
std::cout << "print(" << i << ")" << std::endl;
}
int get(int i)
{
return i;
}
int main()
{
connect(sig, print);
sig(3);
int i = 4;
sig(i); // <-- here I get an error
sig(get(5));
}
The error I get is
main.cpp: In function ‘int main()’:
main.cpp:21:10: error: cannot bind ‘int’ lvalue to ‘int&&’
sig(i);
^
In file included from main.cpp:2:0:
main.h:89:10: error: initializing argument 1 of ‘void Signal<args>::operator()(args&& ...) const [with args = {int}]’
void operator()(args&&... a) const
^
The errors vanishes when I use const int&
everywhere, i.e. Signal<const int&> sig
and void print(const int&)
, but I don't understand why. Also, it would feel awkward to pass around a const bool&
in case of "flag" signals.
Can you suggest a fix that would allow for some more flexibility here?