qskinny/playground/invoker/Invoker.cpp

76 lines
2.0 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>
2018-02-26 09:39:53 +01:00
Invoker::Invoker( QObject* parent ):
QObject( parent )
{
}
void Invoker::addCallback( const QObject* object,
const QskMetaFunction& function )
{
2018-03-01 17:11:59 +01:00
m_callbacks.append( QskMetaCallback( object, function ) );
2018-02-26 09:39:53 +01:00
}
void Invoker::invoke( qreal realValue, int intValue,
Qt::ConnectionType connectionType )
2018-02-26 09:39:53 +01:00
{
for ( auto& callback : m_callbacks )
2018-02-26 09:39:53 +01:00
{
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.setConnectionType( connectionType );
2018-03-02 11:26:25 +01:00
const int callType = connectionType & 0x3;
switch( callType )
{
case Qt::DirectConnection:
{
callback.invoke( args );
break;
}
case Qt::QueuedConnection:
{
if ( callback.object() )
callback.invoke( args );
break;
}
case Qt::BlockingQueuedConnection:
{
const auto receiver = callback.object();
if ( receiver && receiver->thread() != QThread::currentThread() )
callback.invoke( args );
break;
}
}
2018-02-26 09:39:53 +01:00
}
}