2018-02-26 09:09:21 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* QSkinny - Copyright (C) 2016 Uwe Rathmann
|
|
|
|
* This file may be used under the terms of the QSkinny License, Version 1.0
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "QskGlobal.h"
|
|
|
|
#include "QskMetaFunction.h"
|
|
|
|
|
2018-02-28 10:43:15 +01:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QSemaphore>
|
|
|
|
|
|
|
|
QSK_QT_PRIVATE_BEGIN
|
|
|
|
#include <private/qobject_p.h>
|
|
|
|
QSK_QT_PRIVATE_END
|
|
|
|
|
|
|
|
static inline void qskInvokeFunctionQueued( QObject* object,
|
2018-03-03 15:52:42 +01:00
|
|
|
QskMetaFunction::FunctionCall* functionCall, int argc, int* types, void* argv[],
|
2018-02-28 10:43:15 +01:00
|
|
|
QSemaphore* semaphore = nullptr )
|
|
|
|
{
|
|
|
|
constexpr QObject* sender = nullptr;
|
|
|
|
constexpr int signalId = 0;
|
|
|
|
|
|
|
|
auto event = new QMetaCallEvent(
|
2018-03-03 15:52:42 +01:00
|
|
|
functionCall, sender, signalId, argc, types, argv, semaphore );
|
2018-02-28 10:43:15 +01:00
|
|
|
|
|
|
|
QCoreApplication::postEvent( object, event );
|
|
|
|
}
|
|
|
|
|
2018-03-03 15:52:42 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
using FunctionCall = QskMetaFunction::FunctionCall;
|
|
|
|
|
|
|
|
// to have access to the private section of QSlotObjectBase
|
|
|
|
struct SlotObject
|
|
|
|
{
|
|
|
|
QAtomicInt ref;
|
|
|
|
FunctionCall::InvokeFunction invoke;
|
|
|
|
const int* parameterTypes;
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert( sizeof( SlotObject ) == sizeof( FunctionCall ),
|
|
|
|
"Bad cast: QskMetaFunction does not match" );
|
|
|
|
}
|
|
|
|
|
|
|
|
int QskMetaFunction::FunctionCall::typeInfo() const
|
|
|
|
{
|
|
|
|
auto that = const_cast< FunctionCall* >( this );
|
|
|
|
|
|
|
|
int value;
|
|
|
|
|
|
|
|
reinterpret_cast< SlotObject* >( that )->invoke( TypeInfo, that,
|
|
|
|
nullptr, reinterpret_cast< void** >( &value ), nullptr );
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int QskMetaFunction::FunctionCall::refCount() const
|
|
|
|
{
|
|
|
|
auto that = const_cast< FunctionCall* >( this );
|
|
|
|
return reinterpret_cast< SlotObject* >( that )->ref.load();
|
|
|
|
}
|
|
|
|
|
2018-02-26 09:09:21 +01:00
|
|
|
QskMetaFunction::QskMetaFunction():
|
2018-03-03 15:52:42 +01:00
|
|
|
m_functionCall( nullptr )
|
2018-02-26 09:09:21 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-03 15:52:42 +01:00
|
|
|
QskMetaFunction::QskMetaFunction( FunctionCall* functionCall ):
|
|
|
|
m_functionCall( functionCall )
|
2018-02-28 16:48:46 +01:00
|
|
|
{
|
2018-03-03 15:52:42 +01:00
|
|
|
if ( m_functionCall )
|
|
|
|
m_functionCall->ref();
|
2018-02-28 16:48:46 +01:00
|
|
|
}
|
|
|
|
|
2018-02-26 09:09:21 +01:00
|
|
|
QskMetaFunction::QskMetaFunction( const QskMetaFunction& other ):
|
2018-03-03 15:52:42 +01:00
|
|
|
m_functionCall( other.m_functionCall )
|
2018-02-26 09:09:21 +01:00
|
|
|
{
|
2018-03-03 15:52:42 +01:00
|
|
|
if ( m_functionCall )
|
|
|
|
m_functionCall->ref();
|
2018-02-26 09:09:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QskMetaFunction::QskMetaFunction( QskMetaFunction&& other ):
|
2018-03-03 15:52:42 +01:00
|
|
|
m_functionCall( other.m_functionCall )
|
2018-02-26 09:09:21 +01:00
|
|
|
{
|
2018-03-03 15:52:42 +01:00
|
|
|
other.m_functionCall = nullptr;
|
2018-02-26 09:09:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QskMetaFunction::~QskMetaFunction()
|
|
|
|
{
|
2018-03-03 15:52:42 +01:00
|
|
|
if ( m_functionCall )
|
|
|
|
m_functionCall->destroyIfLastRef();
|
2018-02-26 09:09:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QskMetaFunction& QskMetaFunction::operator=( QskMetaFunction&& other )
|
|
|
|
{
|
2018-03-03 15:52:42 +01:00
|
|
|
if ( m_functionCall != other.m_functionCall )
|
2018-02-26 09:09:21 +01:00
|
|
|
{
|
2018-03-03 15:52:42 +01:00
|
|
|
if ( m_functionCall )
|
|
|
|
m_functionCall->destroyIfLastRef();
|
2018-02-26 09:09:21 +01:00
|
|
|
|
2018-03-03 15:52:42 +01:00
|
|
|
m_functionCall = other.m_functionCall;
|
|
|
|
other.m_functionCall = nullptr;
|
2018-02-26 09:09:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QskMetaFunction& QskMetaFunction::operator=( const QskMetaFunction& other )
|
|
|
|
{
|
2018-03-03 15:52:42 +01:00
|
|
|
if ( m_functionCall != other.m_functionCall )
|
2018-02-26 09:09:21 +01:00
|
|
|
{
|
2018-03-03 15:52:42 +01:00
|
|
|
if ( m_functionCall )
|
|
|
|
m_functionCall->destroyIfLastRef();
|
2018-02-26 09:09:21 +01:00
|
|
|
|
2018-03-03 15:52:42 +01:00
|
|
|
m_functionCall = other.m_functionCall;
|
2018-02-26 09:09:21 +01:00
|
|
|
|
2018-03-03 15:52:42 +01:00
|
|
|
if ( m_functionCall )
|
|
|
|
m_functionCall->ref();
|
2018-02-26 09:09:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-03-04 13:31:49 +01:00
|
|
|
bool QskMetaFunction::operator==( const QskMetaFunction& other ) const
|
|
|
|
{
|
|
|
|
if ( m_functionCall == other.m_functionCall )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
There is no way to compmare functors/members without
|
|
|
|
std::type_info, what we don't want to use as it is
|
|
|
|
another template creating symbols.
|
|
|
|
|
|
|
|
So this implementation can't do much more than finding
|
|
|
|
out if one instance is a copy from another.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ( m_functionCall && other.m_functionCall )
|
|
|
|
{
|
2018-03-14 14:45:39 +01:00
|
|
|
if ( m_functionCall->typeInfo() == StaticFunction &&
|
|
|
|
other.m_functionCall->typeInfo() == StaticFunction )
|
2018-03-04 13:31:49 +01:00
|
|
|
{
|
|
|
|
// only static functions can be compared
|
|
|
|
return m_functionCall->compare(
|
|
|
|
reinterpret_cast< void** >( other.m_functionCall ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-08 08:37:44 +01:00
|
|
|
int QskMetaFunction::returnType() const
|
|
|
|
{
|
|
|
|
return QMetaType::Void; // TODO ...
|
|
|
|
}
|
|
|
|
|
2018-02-27 17:47:23 +01:00
|
|
|
size_t QskMetaFunction::parameterCount() const
|
|
|
|
{
|
2018-03-08 08:37:44 +01:00
|
|
|
int count = 0;
|
|
|
|
|
2018-03-02 06:57:08 +01:00
|
|
|
if ( auto types = parameterTypes() )
|
2018-02-27 17:47:23 +01:00
|
|
|
{
|
2018-03-08 08:37:44 +01:00
|
|
|
while ( types[ count ] != QMetaType::UnknownType )
|
|
|
|
count++;
|
2018-02-27 17:47:23 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 08:37:44 +01:00
|
|
|
return count;
|
2018-02-27 17:47:23 +01:00
|
|
|
}
|
|
|
|
|
2018-02-26 09:09:21 +01:00
|
|
|
QskMetaFunction::Type QskMetaFunction::functionType() const
|
|
|
|
{
|
2018-03-03 15:52:42 +01:00
|
|
|
if ( m_functionCall == nullptr )
|
2018-02-26 09:09:21 +01:00
|
|
|
return Invalid;
|
|
|
|
|
2018-03-03 15:52:42 +01:00
|
|
|
return static_cast< QskMetaFunction::Type >( m_functionCall->typeInfo() );
|
2018-02-26 09:09:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void QskMetaFunction::invoke(
|
2018-02-27 17:47:23 +01:00
|
|
|
QObject* object, void* argv[], Qt::ConnectionType connectionType )
|
2018-02-26 09:09:21 +01:00
|
|
|
{
|
2018-05-09 15:45:10 +02:00
|
|
|
#if 1
|
|
|
|
/*
|
|
|
|
Since Qt 5.10 we also have QMetaObject::invokeMethod
|
|
|
|
with functor based callbacks. TODO ...
|
|
|
|
*/
|
|
|
|
#endif
|
|
|
|
|
2018-03-02 11:26:25 +01:00
|
|
|
// code is not thread safe - TODO ...
|
|
|
|
|
2018-03-02 14:58:43 +01:00
|
|
|
QPointer< QObject > receiver( object );
|
2018-03-02 11:26:25 +01:00
|
|
|
|
2018-03-03 15:52:42 +01:00
|
|
|
if ( m_functionCall == nullptr )
|
2018-02-28 10:43:15 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
int invokeType = connectionType & 0x3;
|
|
|
|
|
|
|
|
if ( invokeType == Qt::AutoConnection )
|
|
|
|
{
|
2018-03-02 11:26:25 +01:00
|
|
|
invokeType = ( receiver && receiver->thread() != QThread::currentThread() )
|
2018-02-28 16:48:46 +01:00
|
|
|
? Qt::QueuedConnection : Qt::DirectConnection;
|
2018-02-28 10:43:15 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 11:26:25 +01:00
|
|
|
switch( invokeType )
|
2018-02-27 17:47:23 +01:00
|
|
|
{
|
2018-03-02 11:26:25 +01:00
|
|
|
case Qt::DirectConnection:
|
2018-02-28 10:43:15 +01:00
|
|
|
{
|
2018-03-03 15:52:42 +01:00
|
|
|
m_functionCall->call( receiver, argv );
|
2018-03-02 11:26:25 +01:00
|
|
|
break;
|
2018-02-28 10:43:15 +01:00
|
|
|
}
|
2018-03-02 11:26:25 +01:00
|
|
|
case Qt::BlockingQueuedConnection:
|
|
|
|
{
|
|
|
|
if ( receiver.isNull()
|
|
|
|
|| ( receiver->thread() == QThread::currentThread() ) )
|
|
|
|
{
|
|
|
|
// We would end up in a deadlock, better do nothing
|
|
|
|
return;
|
|
|
|
}
|
2018-02-28 10:43:15 +01:00
|
|
|
|
2018-03-02 11:26:25 +01:00
|
|
|
QSemaphore semaphore;
|
2018-02-28 10:43:15 +01:00
|
|
|
|
2018-03-03 15:52:42 +01:00
|
|
|
qskInvokeFunctionQueued( receiver, m_functionCall,
|
2018-03-02 11:26:25 +01:00
|
|
|
0, nullptr, argv, &semaphore );
|
2018-02-28 10:43:15 +01:00
|
|
|
|
2018-03-02 11:26:25 +01:00
|
|
|
semaphore.acquire();
|
2018-02-28 10:43:15 +01:00
|
|
|
|
2018-03-02 11:26:25 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Qt::QueuedConnection:
|
2018-02-28 10:43:15 +01:00
|
|
|
{
|
2018-03-02 11:26:25 +01:00
|
|
|
if ( receiver.isNull() )
|
2018-02-28 10:43:15 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-08 08:37:44 +01:00
|
|
|
const auto argc = parameterCount() + 1; // return value + arguments
|
2018-02-28 10:43:15 +01:00
|
|
|
|
2018-03-02 11:26:25 +01:00
|
|
|
auto types = static_cast< int* >( malloc( argc * sizeof( int ) ) );
|
|
|
|
auto arguments = static_cast< void** >( malloc( argc * sizeof( void* ) ) );
|
2018-02-28 10:43:15 +01:00
|
|
|
|
2018-03-08 08:37:44 +01:00
|
|
|
types[0] = QMetaType::UnknownType;
|
2018-03-02 11:26:25 +01:00
|
|
|
arguments[0] = nullptr;
|
2018-02-28 10:43:15 +01:00
|
|
|
|
2018-03-03 15:52:42 +01:00
|
|
|
const int* parameterTypes = m_functionCall->parameterTypes();
|
2018-03-02 11:26:25 +01:00
|
|
|
for ( uint i = 1; i < argc; i++ )
|
|
|
|
{
|
|
|
|
if ( argv[i] == nullptr )
|
|
|
|
{
|
|
|
|
Q_ASSERT( arguments[i] != nullptr );
|
|
|
|
receiver = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
types[i] = parameterTypes[i - 1];
|
2018-03-08 08:37:44 +01:00
|
|
|
arguments[i] = QMetaType::create( types[i], argv[i] );
|
2018-03-02 11:26:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( receiver.isNull() )
|
|
|
|
{
|
|
|
|
// object might have died in the meantime
|
|
|
|
free( types );
|
|
|
|
free( arguments );
|
2018-03-02 14:58:43 +01:00
|
|
|
|
|
|
|
return;
|
2018-03-02 11:26:25 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 15:52:42 +01:00
|
|
|
qskInvokeFunctionQueued( object, m_functionCall, argc, types, arguments );
|
2018-03-02 11:26:25 +01:00
|
|
|
break;
|
2018-02-28 10:43:15 +01:00
|
|
|
}
|
2018-02-27 17:47:23 +01:00
|
|
|
}
|
2018-02-26 09:09:21 +01:00
|
|
|
}
|
2018-02-28 16:48:46 +01:00
|
|
|
|
|
|
|
#include "moc_QskMetaFunction.cpp"
|