NuriaProject Framework
0.1
The NuriaProject Framework
|
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> | |
Callback & | bind (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() () |
Callback & | operator= (const Callback &other) |
template<typename Ret , typename... Args> | |
Callback & | operator= (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 |
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.
For all of the above types Callback provides a convenient constructors with the exception of lambdas (See fromLambda).
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.
Placeholders for bind().
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.
Nuria::Callback::Callback | ( | const Callback & | other | ) |
Copy constructor.
|
inline |
Constructs a callback from a static method.
|
inline |
Constructs a callback from a member method.
|
inline |
Constructs a callback from a constant member method.
Nuria::Callback::~Callback | ( | ) |
Destructor.
QList< int > Nuria::Callback::argumentTypes | ( | ) | const |
Returns a list of arguments expected by this callback.
|
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.
|
inlinestatic |
Takes a lambda and returns a Callback with args already bound.
|
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.
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.
|
inline |
Invokes the callback method, passing args.
|
inline |
Invokes the callback without arguments.
bool Nuria::Callback::operator== | ( | const Callback & | other | ) | const |
Comparison operator.
Returns true
if both callbacks point to the very same method (and instance).
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.