예제 #1
0
void TestCaller::callMegaString()
{
	QMutexLocker locker(&mutex);
	qDebug() << "Iteration!" << this << thread() << QThread::currentThread();
	ReturnValue ret = largeValues(megaString);
	QString value = ret.isError() ? ret.errString() : ret.toString();
	if (value != megaString)
		qFatal(qPrintable(QString("ERROR: Malformed megastring: %1 (%2)").arg(value).arg(ret.errString())));
}
예제 #2
0
/**
 * This function does all the dirty qt stuff to call any given signal or slot manually. This function is for internal use only
 * @sa callCallback emitSignal
 * @param sig Signature of the function to be called
 * @param args Arguments list for the function
 * @return Returns the ReturnValue from the function, or an error.
 */
ReturnValue ProxyBase::callMetacall(Signature sig, Arguments args)
{
	// Test to make sure the argument list matches the signature... this is important else the void*'s will contain the wrong types (breaking everything)
	QString test = sig.test(args);
	if (!test.isEmpty())
	{
		return(ReturnValue(2, test));
	}

	// Create the return value
	ReturnValue ret;
	QList<QByteArray> bagarbageCollector;
	QList<QSharedPointer<const char*> > garbageCollector;

	// Create an array of void pointers and place the QVariants into it... I don't know why this works, but it does.
	void *param[] = {(void *)&ret, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};

	for (int i = 0; i < sig.numArgs(); i++)
	{
		if (sig.arg(i) == "char*" || sig.arg(i) == "const char*")
		{
			garbageCollector.append(QSharedPointer<const char*>(new const char*));
			bagarbageCollector.append(args[i].toString().toLocal8Bit());
			*garbageCollector.last().data() = bagarbageCollector.last().constData();
			param[i+1] = garbageCollector.last().data();
		}
		else if (sig.arg(i) != "QVariant" && args.at(i).type() == QVariant::UserType)
		{
			param[i+1] = args[i].data();
		}
		else
		{
			param[i+1] = static_cast<void*>(&args[i]);
		}
	}

	// Check the make sure the signature is a valid string...
	if (sig.name().isEmpty())
		return(ReturnValue(3, "Failed to call " + sig.toString() + ": Sig is not valid (" + sig.name() + ")"));

	// Get the ID number of the function, if it fails normalize the string and try again...
	int id = metaObject()->indexOfMethod(qPrintable(sig.toString()));
	if (id < 0)
		id = metaObject()->indexOfMethod(QMetaObject::normalizedSignature(qPrintable(sig.toString())).constData()); //try again

	if (id < 0) // failed to find the id number of the function
		return(ReturnValue(4, "Failed to call " + sig.toString() + ": Could not find the index of the slot (id=" + QString("%1)").arg(id)));

	// the return from qt_metacall SHOULD be negative if the slot was found...
	int retid = qt_metacall(QMetaObject::InvokeMetaMethod, id, param);
	if (retid > 0) // return error
		return(ReturnValue(5, "Failed to call " + sig.toString() + ": Failed to find it in metacall (" + QString("id=%1 retid= %2 ret=%3)").arg(id).arg(retid).arg(ret.toString())));

	// return the return value. It's data was filled via the *param[] object...
	return(ret);
}