我此刻正在移植FitNesse的Slim-server,我现在有点卡住了.我得到的是这样的字符串:
("id_4", "call", "id", "setNumerator", "20")
("id_5", "call", "id", "setSomethingElse", "10", "8")
Run Code Online (Sandbox Code Playgroud)
其中"setNumerator"和"setSomethingElse"是应该调用的方法的名称,"20","10"和"8"是我传递的参数.
所以我现在的问题是,我不知道如何对两种方法使用一次调用invokeMethod.我目前的解决方法如下所示:
//(if instructionLength==5)
metaObj->invokeMethod(className, methodName.toAscii().constData(), Qt::DirectConnection,
Q_ARG(QVariant, instructions.at(index).at(4)))
//(if instructionLength==6)
metaObj->invokeMethod(className, methodName.toAscii().constData(), Qt::DirectConnection, Q_RETURN_ARG(QVariant, retArg),
Q_ARG(QVariant, instructions.at(index).at(4)),
Q_ARG(QVariant, instructions.at(index).at(5)))
Run Code Online (Sandbox Code Playgroud)
等等......
一方面,似乎每个invokeMethod调用都需要非常具体的信息,这使得使用可变数量的参数很难做到这一点.另一方面,必须有一种方式,所以我不必做同样的事情两(或后来:十)次.
所以问题是,是否有另一种方法可以通过一次通话完成?
Qt的QVariant类是否有任何现有(且方便)的访问者模式实现?
如果没有,是否有可能实现类似的东西boost::apply_visitor(),即最小化测试类型和铸造的重复?
我希望通过以下方式实现目标:
/* I have a QVariant that can contain anything, including user types */
QVariant variant;
/* But in my Visitor I'm interested only in ints and QStrings (for the sake of the example) */
struct Visitor
{
void operator()(int i) { /* do something with int */ }
void operator()(QString s) { /* ...or QString */ }
};
/* The question is: */
/* Can this be implemented in a generic way (without resorting to …Run Code Online (Sandbox Code Playgroud)