Global Classes
class qi::SignalSubscriber
Functions (class qi::SignalSubscriber)
qi::SignalSubscriber::operator SignalLink
qi::SignalSubscriber::signature
qi::SignalSubscriber::operator=
qi::SignalSubscriber::addActive
qi::SignalSubscriber::waitForInactive
qi::SignalSubscriber::~SignalSubscriber
Signals (class qi::SignalSubscriber)
class qi::Signal
Functions (class qi::Signal)
qi::SignalBase::disconnectAllAsync
qi::SignalBase::hasSubscribers
qi::SignalBase::disconnectAsync
Members (class qi::Signal)
This is an implementation of the signal/event paradigm, with some specificities:
weak_ptr
or qi::Trackable
is
used.qi::Signal
is non-copyable.
Signal
is templated by the argument types that must be passed when
triggering, and that will be transmitted to subscribers. For instance,
Signal<int, int>
is the type of a Signal
with two ints as payload type:
Signal::operator()
will expect two ints, and subscribers will be expected to
have signature void(int, int)
.
Note
The types used as template arguments to signal must be registered to the type system. If you fail to do that, you will get errors like:
Cannot do 'clone' on YourType
Use Signal::connect
to register a subscriber that will be called each time
the signal is triggered.
Arguments to connect
can take multiple forms:
boost::function
).Signal
.The variadic form of connect
works in a similar manner to
boost::bind()
: values passed to connect
will be passed to the function,
in order, and placeholders _1
, _2
... will be replaced by the signal
payloads.
This form will also recognize if the first argument is a boost::weak_ptr
,
or if it as pointer to a class inheriting from qi::Trackable
. In both cases,
the subscriber will be automatically disconnected if the pointer cannot be
locked. See this example for a demonstration of that
very same mechanism in qi::Future
.
Signal::connect
returns a SignalSubscriber&
, that you can use to:
SignalSubscriber::setCallType
(see callback
type).qi::SignalLink
by casting the
SignalSubscriber
:qi::SignalLink l = someSignal.connect(callback1);
You can bind arguments directly at the connection:
class MyClass
{
public:
void myCallback(const std::string& str, int value);
// ...
};
qi::Signal<int> someSignal;
qi::SignalLink l = someSignal.connect(&MyClass::myCallback, this,
"this connection", _1);
Unregistering a subscriber is done by invoking Signal::disconnect
with a
SignalLink
as its sole argument. The call will block until all currently
running invocations of the subscriber have finished. This gives you the strong
guarantee than once disconnect
has returned, your callback function is not
being called, and will never be called again.
Warning
disconnect
is a blocking method which will wait for the callback to finish
(except if it’s called from withing the callback). The signal destruction is
also blocking. These two cases may cause deadlocks in your code, be careful.
Triggering the signal is achieved by using the Signal::operator()
, with
arguments matching the Signal
type:
qi::Signal<int, int> sig;
// QI_EMIT is here for readability
QI_EMIT sig(51, 42);
This will invoke all subscribers with given arguments.
It is possible to control how subscribers are invoked:
MetaCallType_Auto
is the default and means asynchronous.MetaCallType_Direct
forces a synchronous call.MetaCallType_Queued
forces an asynchronous call.Note that if any subscriber is invoked asynchronously, the arguments passed to
Signal::operator()
will be copied.
You can set the call type of a signal globally with setCallType
, but you can
also set it per-callback. You can do that by calling setCallType
on the
SignalSubscriber
returned by connect
.
qi::SignalLink l = someSignal
.connect(callback2)
.setCallType(qi::MetaCallType_Direct);
Warning
It is very dangerous to set the call type to Direct as your function may block the code that triggers the signal. This type of call is only useful for optimization purposes, only for very small and fast fuctions that do not lock.
Sometimes, mainly for performance reasons, it is useful to only enable some
code if a Signal
has at least one subscriber. For example, if you have a
signal humanDetected
, you may want to enable the image processing code only
if there is at least one subscriber to the signal to save CPU cycles.
This can be achieved by passing a callback to the Signal
constructor, of
signature void(bool)
. This function will be called synchronously each
time the number of subscribers switches between 0 and 1.
void onConnect(bool c)
{
if (c)
std::cout << "First connection";
else
std::cout << "No more connections";
}
qi::Signal<int> sig(onConnect);
qi::SignalLink l1 = sig.connect(mycallback); // calls onConnect(true)
qi::SignalLink l2 = sig.connect(mycallback); // nothing happens
sig.disconnect(l1); // nothing happens
sig.disconnect(l2); // calls onConnect(false);
Sometimes, mainly when bridging Signal
with another signal implementation,
one needs to override the action performed when the signal is triggered (which
is by default to invoke all subscribers).
This can be achieved by inheriting from Signal
, and then either overriding
the trigger
virtual function, or by calling setTriggerOverride
with a
functor that will replace the original trigger. You can then call
callSubscribers
to invoke the subscribers, which trigger
would do
by default.
invalidSignalLink
Signal
(OnSubscribers onSubscribers)SignalBase
(const Signature& signature, OnSubscribers onSubscribers)SignalBase
(OnSubscribers onSubscribers)
()
()~SignalBase
()signature
() constconnect
(boost::function<F> func)connect
(AnyObject object, unsigned int slot)connect
(AnyObject object, const std::string& slot)connect
(const SignalSubscriber& s)connectAsync
(const SignalSubscriber&)disconnectAll
()disconnectAllAsync
()asyncDisconnectAll
()disconnect
(const SignalLink& link)disconnectAsync
(const SignalLink& link)asyncDisconnect
(const SignalLink& link)trigger
(const GenericFunctionParameters& params, MetaCallType callType)setCallType
(MetaCallType callType)operator()
(qi::AutoAnyReference p1)subscribers
()hasSubscribers
()_setSignature
(const Signature& s)Class that represent an event to which function can subscribe.
qi::Signal<P>::
Signal
(OnSubscribers onSubscribers = OnSubscribers()¶qi::SignalF<T>::
SignalF
(OnSubscribers onSubscribers = OnSubscribers()¶Brief:
Parameters: |
|
---|
Signal constructor
qi::SignalF<T>::
signature
() const
¶qi::SignalF<T>::
connect
(...)¶Brief:
Returns: | a SignalSubscriber object. This object can be implicitly converted to a SignalLink. |
---|
Connect a subscriber to this signal.
Multiple forms can be used: connect(function, argOrPlaceholder1, argOrPlaceholder2, ...) Where function is a function or callable object (such as a boost::function). If the first argument is a weak ptr or inherits qi::Trackable, the slot will automatically disconnect if object is no longuer available.connect(AnyObject target, unsigned int slot)connect(AnyObject target, const std::string& slotName)connect(AnyFunction func)connect(const SignalSubscriber&)connect(qi::Signal<U>& otherSignal)
qi::SignalBase::
SignalBase
(const Signature& signature, OnSubscribers onSubscribers = OnSubscribers()¶qi::SignalBase::
SignalBase
(OnSubscribers onSubscribers = OnSubscribers()¶()
()
qi::SignalBase::
~SignalBase
()¶qi::SignalBase::
signature
() const
¶qi::SignalBase::
connect
(boost::function<F> func)¶qi::SignalBase::
connect
(AnyObject object, unsigned int slot)¶qi::SignalBase::
connect
(AnyObject object, const std::string& slot)¶qi::SignalBase::
connect
(const SignalSubscriber& s)¶The following overloads are the lowest-level.
qi::SignalBase::
connectAsync
(const SignalSubscriber&)¶Connect asynchronously. This is recommended since derived classes may provide asynchronous customizations for dealing with subscribers. The callbacks are guaranteed to be called only after the returned future is set.
qi::SignalBase::
disconnectAll
()¶Brief:
Returns: | Returns true on success. |
---|
Disconnect all callbacks from signal.
This function will block until all callbacks are finished.
qi::SignalBase::
disconnectAllAsync
()¶Brief:
Returns: | A future set to true on success. |
---|
Disconnect all callbacks from signal without waiting for them.
This function does not block.
qi::SignalBase::
asyncDisconnectAll
()¶qi::SignalBase::
disconnect
(const SignalLink& link)¶Brief:
Returns: | Returns true on success. |
---|
Disconnect a SignalHandler.
The associated callback will not be called anymore as soon as this function returns.
This method blocks until all the already running callbacks are finished.
qi::SignalBase::
disconnectAsync
(const SignalLink& link)¶Brief:
Returns: | A future set to true on success. |
---|
Disconnect a SignalHandler without waiting for it.
Same as disconnect, but this method does not block. Though this is async, you are guaranteed that once the function returns your callback will not be called.
qi::SignalBase::
asyncDisconnect
(const SignalLink& link)¶qi::SignalBase::
trigger
(const GenericFunctionParameters& params, MetaCallType callType = MetaCallType_Auto)¶Brief:
Parameters: |
|
---|
Trigger the signal with given type-erased parameters.
qi::SignalBase::
setCallType
(MetaCallType callType)¶Set the MetaCallType used by operator()().
qi::SignalBase::
operator
(qi::AutoAnyReference p1 = qi::AutoAnyReference()¶Trigger the signal with given arguments, and call type set by setCallType()
qi::SignalBase::
subscribers
()¶qi::SignalBase::
hasSubscribers
()¶qi::SignalBase::
_setSignature
(const Signature& s)¶SignalSubscriber
()SignalSubscriber
(AnyFunction func, MetaCallType callType)SignalSubscriber
(AnyFunction func, ExecutionContext* ec)SignalSubscriber
(const AnyObject& target, unsigned int method)SignalSubscriber
(const SignalSubscriber& other)operator=
(const SignalSubscriber& other)~SignalSubscriber
()call
(const GenericFunctionParameters& args, MetaCallType callType)setCallType
(MetaCallType ct)waitForInactive
()addActive
(bool acquireLock, boost::thread::id tid)removeActive
(bool acquireLock, boost::thread::id tid)link
() constoperator SignalLink
() constsignature
() constEvent subscriber info.
Only one of handler or target must be set. This class is copyable but has entity semantics.
qi::SignalSubscriber::
SignalSubscriber
()¶qi::SignalSubscriber::
SignalSubscriber
(AnyFunction func, MetaCallType callType = MetaCallType_Auto)¶qi::SignalSubscriber::
SignalSubscriber
(AnyFunction func, ExecutionContext* ec)¶qi::SignalSubscriber::
SignalSubscriber
(const AnyObject& target, unsigned int method)¶qi::SignalSubscriber::
SignalSubscriber
(const SignalSubscriber& other)¶qi::SignalSubscriber::
operator=
(const SignalSubscriber& other)¶qi::SignalSubscriber::
~SignalSubscriber
()¶qi::SignalSubscriber::
call
(const GenericFunctionParameters& args, MetaCallType callType)¶Perform the call.
Threading rules in order: Honor threadingModel if set (not auto)Honor callTypoe if set (not auto)Be asynchronous
qi::SignalSubscriber::
setCallType
(MetaCallType ct)¶qi::SignalSubscriber::
waitForInactive
()¶Wait until all threads are inactive except the current thread.
qi::SignalSubscriber::
addActive
(bool acquireLock, boost::thread::id tid = boost::this_thread::get_id()¶qi::SignalSubscriber::
removeActive
(bool acquireLock, boost::thread::id tid = boost::this_thread::get_id()¶qi::SignalSubscriber::
link
() const
¶Brief:
Returns: | the identifier of the subscription (aka link) |
---|
qi::SignalSubscriber::
operator
SignalLink() const
¶qi::SignalSubscriber::
signature
() const
¶Brief:
Returns: | the signature, or an invalid signature if extraction is impossible |
---|
Try to extract exact signature of this subscriber.