qskinny/playground/invoker/Invoker.cpp

98 lines
2.7 KiB
C++
Raw Normal View History

2018-02-26 09:39:53 +01:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#include "Invoker.h"
2018-03-02 11:26:25 +01:00
#include <QThread>
#include <QMetaProperty>
2018-02-26 09:39:53 +01:00
Invoker::Invoker( QObject* parent ):
QObject( parent )
{
}
void Invoker::addFunctionCall( const QObject* object,
2018-02-26 09:39:53 +01:00
const QskMetaFunction& function )
{
m_callbacks.append( Callback( object, function ) );
2018-02-26 09:39:53 +01:00
}
void Invoker::addMethodCall( const QObject* object,
const char* methodName )
{
m_callbacks.append( Callback( object, methodName ) );
}
void Invoker::addPropertyCall( const QObject* object,
const char* property )
{
const auto metaProperty = object->metaObject()->property(
object->metaObject()->indexOfProperty( property ) );
m_callbacks.append( Callback( object, metaProperty ) );
}
void Invoker::invoke( qreal realValue, int intValue,
Qt::ConnectionType connectionType )
2018-02-26 09:39:53 +01:00
{
QString s = QString( "S: %1 + %2" ).arg( realValue ).arg( intValue );
for ( auto& callback : m_callbacks )
2018-02-26 09:39:53 +01:00
{
void* args[4] = { nullptr };
2018-02-26 09:39:53 +01:00
2018-03-12 09:27:54 +01:00
void **a = args + 1;
const auto& invokable = callback.invokable();
2018-02-26 09:39:53 +01:00
2018-03-12 09:27:54 +01:00
for ( int i = 0; i < invokable.parameterCount(); i++ )
2018-02-26 09:39:53 +01:00
{
2018-03-12 09:27:54 +01:00
switch ( invokable.parameterType( i ) )
2018-02-26 09:39:53 +01:00
{
case QMetaType::Int:
{
2018-03-12 09:27:54 +01:00
*a++ = reinterpret_cast< void* >( &intValue );
2018-02-26 09:39:53 +01:00
break;
}
case QMetaType::Double:
{
2018-03-12 09:27:54 +01:00
*a++ = reinterpret_cast< void* >( &realValue );
2018-02-26 09:39:53 +01:00
break;
}
case QMetaType::QString:
{
2018-03-12 09:27:54 +01:00
*a++ = reinterpret_cast< void* >( &s );
break;
}
2018-02-26 09:39:53 +01:00
default:
break;
}
}
switch( connectionType & 0x3 )
2018-03-02 11:26:25 +01:00
{
case Qt::DirectConnection:
{
callback.invoke( args, connectionType );
2018-03-02 11:26:25 +01:00
break;
}
case Qt::QueuedConnection:
{
if ( callback.context() )
callback.invoke( args, connectionType );
2018-03-02 11:26:25 +01:00
break;
}
case Qt::BlockingQueuedConnection:
{
const auto receiver = callback.context();
2018-03-02 11:26:25 +01:00
if ( receiver && receiver->thread() != QThread::currentThread() )
callback.invoke( args, connectionType );
2018-03-02 11:26:25 +01:00
break;
}
}
2018-02-26 09:39:53 +01:00
}
}