From 356966f4ca2eccd9d47c4853f48dd2d702522edf Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Mon, 26 Feb 2018 09:39:53 +0100 Subject: [PATCH] invoker testprogram added --- playground/invoker/Invoker.cpp | 49 +++++++++++++++++++++++ playground/invoker/Invoker.h | 34 ++++++++++++++++ playground/invoker/invoker.pro | 10 +++++ playground/invoker/main.cpp | 73 ++++++++++++++++++++++++++++++++++ playground/playground.pro | 1 + 5 files changed, 167 insertions(+) create mode 100644 playground/invoker/Invoker.cpp create mode 100644 playground/invoker/Invoker.h create mode 100644 playground/invoker/invoker.pro create mode 100644 playground/invoker/main.cpp diff --git a/playground/invoker/Invoker.cpp b/playground/invoker/Invoker.cpp new file mode 100644 index 00000000..59817df3 --- /dev/null +++ b/playground/invoker/Invoker.cpp @@ -0,0 +1,49 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#include "Invoker.h" + +Invoker::Invoker( QObject* parent ): + QObject( parent ) +{ +} + +void Invoker::addCallback( const QObject* object, + const QskMetaFunction& function ) +{ + m_callbacks += QskMetaCallback( object, function, Qt::DirectConnection ); +} + +void Invoker::invoke( qreal realValue, int intValue ) +{ + for ( auto callback : qskAsConst( m_callbacks ) ) + { + void* args[3] = { nullptr }; + + const auto types = callback.parameterTypes(); + + int i = 1; + for ( auto type : types ) + { + switch ( type ) + { + case QMetaType::Int: + { + args[i++] = reinterpret_cast< void* >( &intValue ); + break; + } + case QMetaType::Double: + { + args[i++] = reinterpret_cast< void* >( &realValue ); + break; + } + default: + break; + } + } + + callback.invoke( args ); + } +} diff --git a/playground/invoker/Invoker.h b/playground/invoker/Invoker.h new file mode 100644 index 00000000..aaf6798e --- /dev/null +++ b/playground/invoker/Invoker.h @@ -0,0 +1,34 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#ifndef INVOKER_H +#define INVOKER_H 1 + +#include +#include + +#include +#include + +class Invoker : public QObject +{ +public: + Invoker( QObject* parent = nullptr ); + + void addCallback( const QskMetaFunction& ); + void addCallback( const QObject*, const QskMetaFunction& ); + + void invoke( qreal d, int i ); + +private: + QVector< QskMetaCallback > m_callbacks; +}; + +inline void Invoker::addCallback( const QskMetaFunction& function ) +{ + addCallback( this, function ); +} + +#endif diff --git a/playground/invoker/invoker.pro b/playground/invoker/invoker.pro new file mode 100644 index 00000000..f1cf4476 --- /dev/null +++ b/playground/invoker/invoker.pro @@ -0,0 +1,10 @@ +include( $${PWD}/../playground.pri ) + +TARGET = invoker + +HEADERS += \ + Invoker.h + +SOURCES += \ + Invoker.cpp \ + main.cpp diff --git a/playground/invoker/main.cpp b/playground/invoker/main.cpp new file mode 100644 index 00000000..d5858c56 --- /dev/null +++ b/playground/invoker/main.cpp @@ -0,0 +1,73 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#include "Invoker.h" +#include + +static void debugValueI( int i ) +{ + qDebug() << i; +} + +static void debugValue( qreal d, int i ) +{ + qDebug() << d << i; +} + +class MyObject : public QObject +{ +public: + MyObject( QObject* parent = nullptr ): + QObject( parent ) + { + } + + void print1( double d, int i ) const + { + qDebug() << d << i; + } + + void print2( int i, double d ) const + { + qDebug() << i << d; + } + + void print3( double d ) const + { + qDebug() << d; + } + + void print4( int i ) const + { + qDebug() << i; + } +}; + +int main( int argc, char* argv[] ) +{ + Q_UNUSED( argc ) + Q_UNUSED( argv ) + + MyObject object; + + Invoker invoker; + + invoker.addCallback( debugValue ); + invoker.addCallback( debugValueI ); + invoker.addCallback( &object, &MyObject::print1 ); + invoker.addCallback( &object, &MyObject::print2 ); + invoker.addCallback( &object, &MyObject::print3 ); + invoker.addCallback( &object, &MyObject::print4 ); + invoker.addCallback( &object, []( double d, int i ) { qDebug() << d << i; } ); + invoker.addCallback( &object, []( int i, double d ) { qDebug() << i << d; } ); + invoker.addCallback( &object, []( double d ) { qDebug() << d; } ); + invoker.addCallback( []() { qDebug() << "HERE"; } ); + invoker.addCallback( [ = ]( double d, int i ) { qDebug() << d << i; } ); + invoker.addCallback( []( int i, double d ) { qDebug() << i << d; } ); + invoker.addCallback( []( int i ) { qDebug() << i; } ); + invoker.addCallback( []( double d ) { qDebug() << d; } ); + + invoker.invoke( 3.14, 35 ); +} diff --git a/playground/playground.pro b/playground/playground.pro index 0df664e1..eda45616 100644 --- a/playground/playground.pro +++ b/playground/playground.pro @@ -4,5 +4,6 @@ TEMPLATE = subdirs # qml SUBDIRS += \ + invoker \ inputpanel \ images