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
2023-04-06 09:23:37 +02:00
* SPDX-License-Identifier: BSD-3-Clause
2018-02-26 09:39:53 +01:00
*****************************************************************************/
#include "Invoker.h"
#include <QMetaProperty>
2018-08-03 08:15:28 +02:00
#include <QThread>
2018-02-26 09:39:53 +01:00
2018-08-03 08:15:28 +02:00
Invoker::Invoker( QObject* parent )
: QObject( parent )
2018-02-26 09:39:53 +01:00
{
}
2018-08-03 08:15:28 +02:00
void Invoker::addFunctionCall(
const QObject* object, const QskMetaFunction& function )
2018-02-26 09:39:53 +01:00
{
m_callbacks.append( Callback( object, function ) );
2018-02-26 09:39:53 +01:00
}
2018-08-03 08:15:28 +02:00
void Invoker::addMethodCall(
const QObject* object, const char* methodName )
{
m_callbacks.append( Callback( object, methodName ) );
}
2018-08-03 08:15:28 +02:00
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 ) );
}
2018-08-03 08:15:28 +02:00
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
{
2018-08-03 08:15:28 +02:00
void* args[ 4 ] = { nullptr };
2018-02-26 09:39:53 +01:00
2018-08-03 08:15:28 +02:00
void** a = args + 1;
2018-03-12 09:27:54 +01:00
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;
}
}
2018-08-03 08:15:28 +02:00
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
}
}