NuriaProject Framework  0.1
The NuriaProject Framework
Classes | Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
Nuria::Callback Class Reference

A modern style callback mechanism which can be bound to various method types including slots. More...

#include <callback.hpp>

Public Types

enum  Placeholder {
  _1 = 0, _2, _3, _4,
  _5, _6, _7, _8,
  _9, _10
}
 
enum  Type {
  Invalid = 0, StaticMethod = 1, MemberMethod = 2, Slot = 3,
  Lambda = 4
}
 

Public Member Functions

 Callback ()
 
 Callback (QObject *receiver, const char *slot, bool variadic=false, Qt::ConnectionType connectionType=Qt::AutoConnection)
 
 Callback (const Callback &other)
 
template<typename Ret , typename... Args>
 Callback (Ret(*func)(Args...), bool variadic=false)
 
template<typename Class , typename Ret , typename... Args>
 Callback (Class *instance, Ret(Class::*func)(Args...), bool variadic=false)
 
template<typename Class , typename Ret , typename... Args>
 Callback (Class *instance, Ret(Class::*func)(Args...) const, bool variadic=false)
 
template<typename Ret , typename... Args>
 Callback (const std::function< Ret(Args...) > &func, bool variadic=false)
 
 ~Callback ()
 
QList< int > argumentTypes () const
 
template<typename... Args>
Callbackbind (const Args &...args)
 
void bindList (const QVariantList &arguments=QVariantList())
 
QVariant invoke (const QVariantList &arguments) const
 
bool isValid () const
 
bool isVariadic () const
 
template<typename... Args>
QVariant operator() (const Args &...args) const
 
QVariant operator() ()
 
Callbackoperator= (const Callback &other)
 
template<typename Ret , typename... Args>
Callbackoperator= (Ret(*func)(Args...))
 
bool operator== (const Callback &other) const
 
int returnType () const
 
template<typename Ret , typename... Args>
bool setCallback (Ret(*func)(Args...))
 
template<typename Class , typename Ret , typename... Args>
bool setCallback (Class *instance, Ret(Class::*func)(Args...) const)
 
template<typename Class , typename Ret , typename... Args>
bool setCallback (Class *instance, Ret(Class::*func)(Args...))
 
template<typename Ret , typename... Args>
bool setCallback (std::function< Ret(Args...) > func)
 
bool setCallback (QObject *receiver, const char *slot, Qt::ConnectionType connectionType=Qt::AutoConnection)
 
void setVariadic (bool variadic)
 
Type type () const
 

Static Public Member Functions

template<typename Lambda , typename... Args>
static Callback boundLambda (Lambda func, const Args &...args)
 
template<typename Lambda >
static Callback fromLambda (Lambda l, bool variadic=false)
 

Friends

class CallbackPrivate
 

Detailed Description

A modern style callback mechanism which can be bound to various method types including slots.

The Nuria::Callback class can be used when developers don't want to use the default signal/slots mechanism Qt provides for various reasons. Callback is designed to mimic parts of the std::function methods which makes it easy to use.

Note
This class is explicitly shared. This means that changes to the instance will not copy the data, but instead alter the instance directly.
Methods
Callback can be created out of all available method types - Essentially everything that has a operator(). This includes:

For all of the above types Callback provides a convenient constructors with the exception of lambdas (See fromLambda).

Argument and return types
Please note that all types which are passed to or from the callback must be registered to the Qt Meta System using the Q_DECLARE_METATYPE macro.

Another point is that Callback will always try its best to invoke a method. Passed arguments are checked if they match the type of the target method. If a argument doesn't, Callback will try to use QVariant::convert to convert the passed argument to the expected argument type. If this still fails, it will construct a temporary instance of the expected type and passes it instead, silently discarding the original argument.

It is also possible to use variadic callbacks. This means that on invocation, all passed arguments are packed into a single QVariantList. This list is now treated as it would've been passed as only argument, meaning that bound variables are applied after this process. The placeholder _1 points to the QVariantList.

Binding
Just like std::function Callback supports a mechanism to bind arguments to a callback. Placeholders are also supported (Using _1, ..., _10).
See also
bind boundLambda
Behaviour with QObject slots
When a callback uses a slot as target method, it can be used for thread to thread communication. If the slot returns void (nothing), then the slot will be invoked using a Qt::QueuedConnection, that means, the caller won't be blocked until the callee (The slot) has finished its operation. If the callee returns something different than a void, then the caller will be blocked until the callee finished and returned the result to the calling thread.
Multi-threading
Callback is re-entrant. Do not change a instance from multiple threads concurrently. Target methods will be invoked in the calling thread, when not using a QObject slot as target.

Member Enumeration Documentation

Placeholders for bind().

Possible types of callbacks.

Enumerator
StaticMethod 

Invalid instance.

MemberMethod 

Invokes a static method.

Slot 

Invokes a member method.

Lambda 

Invokes a slot.

Constructor & Destructor Documentation

Nuria::Callback::Callback ( )

Constructs an invalid instance.

Nuria::Callback::Callback ( QObject *  receiver,
const char *  slot,
bool  variadic = false,
Qt::ConnectionType  connectionType = Qt::AutoConnection 
)

Constructs a callback out of a slot.

Warning
If you want to set the connectionType manually make sure you pass a value for variadic. Else, the compiler may choose to implicitly cast the Qt::ConnectionType to bool!
Nuria::Callback::Callback ( const Callback other)

Copy constructor.

template<typename Ret , typename... Args>
Nuria::Callback::Callback ( Ret(*)(Args...)  func,
bool  variadic = false 
)
inline

Constructs a callback from a static method.

template<typename Class , typename Ret , typename... Args>
Nuria::Callback::Callback ( Class *  instance,
Ret(Class::*)(Args...)  func,
bool  variadic = false 
)
inline

Constructs a callback from a member method.

template<typename Class , typename Ret , typename... Args>
Nuria::Callback::Callback ( Class *  instance,
Ret(Class::*)(Args...) const  func,
bool  variadic = false 
)
inline

Constructs a callback from a constant member method.

Nuria::Callback::~Callback ( )

Destructor.

Member Function Documentation

QList< int > Nuria::Callback::argumentTypes ( ) const

Returns a list of arguments expected by this callback.

template<typename... Args>
Callback& Nuria::Callback::bind ( const Args &...  args)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. This method offers a more std::bind-like functionality. Use bindList if you have a QVariantList of these arguments.

void Nuria::Callback::bindList ( const QVariantList &  arguments = QVariantList())

Binds arguments to the callback. This essentially works like std::bind(). If no placeholders are used additional arguments are appended to the final argument list when invoked. If placeholders are used only the arguments whose positions are set with a placeholder are passed, the others are silently discarded.

template<typename Lambda , typename... Args>
static Callback Nuria::Callback::boundLambda ( Lambda  func,
const Args &...  args 
)
inlinestatic

Takes a lambda and returns a Callback with args already bound.

template<typename Lambda >
static Callback Nuria::Callback::fromLambda ( Lambda  l,
bool  variadic = false 
)
inlinestatic

Constructs a callback out of a lambda.

QVariant Nuria::Callback::invoke ( const QVariantList &  arguments) const

Invokes the callback using arguments. Use this method if you have the arguments themself as list.

See also
operator()
bool Nuria::Callback::isValid ( ) const

Returns true when this callback is valid i.e. callable.

bool Nuria::Callback::isVariadic ( ) const

Returns if this callback is variadic.

template<typename... Args>
QVariant Nuria::Callback::operator() ( const Args &...  args) const
inline

Invokes the callback method, passing args.

QVariant Nuria::Callback::operator() ( )
inline

Invokes the callback without arguments.

Callback& Nuria::Callback::operator= ( const Callback other)

Assignment operator.

bool Nuria::Callback::operator== ( const Callback other) const

Comparison operator.

Returns true if both callbacks point to the very same method (and instance).

Warning
Bound arguments aren't taken into account for this operation.
int Nuria::Callback::returnType ( ) const

Returns the return-type of the callback.

void Nuria::Callback::setVariadic ( bool  variadic)

Sets if this callback is variadic or not.

Type Nuria::Callback::type ( ) const

Returns the type of this callback.


The documentation for this class was generated from the following file: