Introspectable Visitor
You could leverage the introspection information generated by moc. Declare your visitor to be Q_GADGET
. This adds a single static staticMetaObject
member to the visitor, containing the information about the invokable methods there.
// https://github.com/KubaO/stackoverflown/tree/master/questions/variant-convert-38071414
#include <QtCore>
struct Foo {
int a;
Foo() = default;
explicit Foo(int a) : a(a) {}
};
QDebug operator<<(QDebug debug, const Foo & f) {
return debug << f.a;
}
Q_DECLARE_METATYPE(Foo)
struct Visitor
{
Q_GADGET
Q_INVOKABLE void visit(int i) { qDebug() << "got int" << i; }
Q_INVOKABLE void visit(const QString & s) { qDebug() << "got string" << s; }
Q_INVOKABLE void visit(const Foo & f) { qDebug() << "got foo" << f; }
};
Qt has all the information necessary to pass opaque types around as arguments to invokable methods:
template <typename V>
bool visit(const QVariant & variant, const V & visitor) {
auto & metaObject = V::staticMetaObject;
for (int i = 0; i < metaObject.methodCount(); ++i) {
auto method = metaObject.method(i);
if (method.parameterCount() != 1)
continue;
auto arg0Type = method.parameterType(0);
if (variant.type() != (QVariant::Type)arg0Type)
continue;
QGenericArgument arg0{variant.typeName(), variant.constData()};
if (method.invokeOnGadget((void*)&visitor, arg0))
return true;
}
return false;
}
Perhaps this is then what you were after:
int main() {
visit(QVariant{1}, Visitor{});
visit(QVariant{QStringLiteral("foo")}, Visitor{});
visit(QVariant::fromValue(Foo{10}), Visitor{});
}
#include "main.moc"
This concludes the example.
Non-Introspectable Visitor
You can factor out the conversion to a type and conditional code execution:
void visitor(const QVariant & val) {
withConversion(val, [](int v){
qDebug() << "got an int" << v;
})
|| withConversion(val, [](const QString & s){
qDebug() << "got a string" << s;
});
}
int main() {
visitor(QVariant{1});
visitor(QVariant{QStringLiteral("foo")});
}
The withConversion
function deduces the argument type of the callable and invokes the callable if the variant is of the matching type:
#include <QtCore>
#include <type_traits>
template <typename T>
struct func_traits : public func_traits<decltype(&T::operator())> {};
template <typename C, typename Ret, typename... Args>
struct func_traits<Ret(C::*)(Args...) const> {
using result_type = Ret;
template <std::size_t i>
struct arg {
using type = typename std::tuple_element<i, std::tuple<Args...>>::type;
};
};
template <typename F> bool withConversion(const QVariant & val, F && fun) {
using traits = func_traits<typename std::decay<F>::type>;
using arg0_t = typename std::decay<typename traits::template arg<0>::type>::type;
if (val.type() == (QVariant::Type)qMetaTypeId<arg0_t>()) {
fun(val.value<arg0_t>());
return true;
}
return false;
}
See this question for more about argument type deduction in callables.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…