@Redman said in QMetaType casting to desired type:
@jeremy_k I'm talking QtRemoteObjects. I intentionally left that piece of information out because every time I mention them in this forum my posts never get answered.
That's an important detail. It sounds like a useful project. I personally have remained minimally interested due to the project's stated lack of interest in compatibility with non-QRO implementations. I'm not lucky enough to work in a distributed environment where all nodes run a compatible Qt application.
This switch statement is gonna get bigger and bigger aswell as be error prone.
Is there any way to make this piece of code dynamic?
I was imagining:
template <class Type, class... Others> std::any convert(void *obj) {
QMetaType fromType = QMetaType::fromType<NetworkClass>();
QMetaType toType = QMetaType::fromType<Type>();
std::any ret;
if QMetaType::canConvert(fromType, toType) {
QMetaType::convert(obj, fromType, &ret, toType);
return ret;
}
else
return convert<Others...>(obj);
}
template <> convert(void *obj) {
return std::any(nullptr);
}
NetworkClass *obj = getObject();
std::any convertedObject = convert<Type1, Type2, Type3>(obj);
if (std::any_cast<void *>(convertedObject) != nullptr)
...
Not expected to compile as is
Not runtime dynamic
The user of the std::any will need to know what it wants to convert it to
I'm not sure this improves the situation.