I have a variadic template class for keeping answers from own net class
enum class TaskType {readReg, writeReg, readRnd, writeRnd, readBlock, writeBlock, pause};
template<typename... Args> class Reply {
public:
Reply(TaskType t, Args...params): t(t), tuple(std::make_tuple(params...)) {}
Reply();
TaskType t;
auto getData() const {return std::get<4>(tuple);} //simplified getter of safe function that deals with oversizing
private:
std::tuple<Args ...> tuple;
};
I register template signatures to keep them in Qvariant
using ReadRegReply = Reply<TaskType, uint, uint, uint, uint, uint> ;
using WriteReply = Reply<TaskType, uint, uint, uint> ;
using ReadRndReply = Reply<TaskType, uint, uint, uint, QVector<uint>, QVector<uint>> ;
using ReadBlockReply = Reply<TaskType, uint, uint, uint, uint, QVector<uint>> ;
Q_DECLARE_METATYPE(QVector<uint>)
Q_DECLARE_METATYPE(ReadRegReply)
Q_DECLARE_METATYPE(WriteReply)
Q_DECLARE_METATYPE(ReadRndReply)
Q_DECLARE_METATYPE(ReadBlockReply)
Then I work with it like this:
class Task: public QObject{
public:
//c-tor and some functions, virtual functions etc
template <class TReply> bool applyReply(TReply reply){
varReply = QVariant::fromValue(reply);
}
auto getData(){ //here I should return data from tuple inside reply.
QVariant::Type t = varReply.type();
auto l = varReply.value<t>();// t is not a constexp // t is not a reply type but enum QVariant::type, as I understand.
return l.getData(); // l has no method getData
}
QVariant varReply; //this is the qvariant that contains templated reply;
}
There is something i miss in QVariant. I thought that registered type should be stored somehow in Qvariant but it's not like that. The other problems: I can't use c++17; it will be used in many projects with different replies signatures. Is there some way to keep these types and add them in the future without total refactoring? I thought about some kind of manager class, but i might be overthinking