0

I'm doing some deserialization from byte array and made a ExtractData variadic template so it works like

QByteArray data; // (this works just like std::vector<char>)
std::vector<std::any> values = ExtractData<float, char>(data); // read a float, char sequentially from data
float readFloat = std::any_cast<float>(values[0]);
float readChar = std::any_cast<char>(values[1]);

but still, a lot of boilerplate to decode stuff.
Ideally I'd want something like

float readFloat;
char readChar;
std::tie(readFloat, readChar) = ExtractData<float, char>(data);

ExtractData structure is basically

using anyVec = std::vector<std::any>;

// one type resolution
template<typename T>
anyVec ExtractData(const QByteArray& data, anyVec out = {}){
    // extract T value, assign to std::any, push_back into out
    return outVec;
}

// multiple types resolution
template<typename T, typename... Rest>
typename std::enable_if<(sizeof...(Rest) > 0), anyVec>::type
ExtractData(const QByteArray& data, anyVec out = {}){
    // extract T value, assign to std::any, push_back into out
    return ExtractData<Rest...>(data, out);
}

I just don't see how I could make ExtractData<type1, type2, ...>(data) return a std::tuple<type1, type2, ...> since all that original type info is lost when template type list "unwinds". Is it even possible? Sorry if I'm missing something obvious, I'm still quite new to c++11 and newer.

I see that I could make a template for fixed amount of types, but that's not my use case sadly.


I'm using Qt 5.11, c++14 (omitted experimental in namespaces here), but happy to hear c++17 advice too. Exact snippet of code I'm using: https://gist.github.com/tjakubo2/dc3e6897bf42f3bed78933031e53786b

2
  • 1
    You should be able to have ExtractData<Args...> return std::tuple<Args...>. Should be reasonably straightforward. Sep 23, 2018 at 16:59
  • "since all that original type info is lost when template type list "unwinds" That is simply wrong! If you "unwind" your typelist all and everything is still known!
    – Klaus
    Sep 23, 2018 at 17:57

2 Answers 2

0

Something along these lines, perhaps:

template <typename T>
T ExtractOnePiece(const QByteArray& data, int& offset);

template <typename... Ts>
std::tuple<Ts...> ExtractData(const QByteArray& data) {
    int offset = 0;
    return {ExtractOnePiece<Ts>(data, offset)...};
}

Demo. Specializations of ExtractOnePiece for each type you want to support are left as an exercise for the reader.

0

Thanks to beached, Ben and Seph from #include Discord server, just like Igor suggested in a response here, I've landed with:

template<typename T>
std::tuple<T> ExtractSingle(const QByteArray& data, size_t offset){
    // pull T_val from data at given offset
    return std::tuple<T>{T_val};
}

template<typename T>
std::tuple<T> ExtractData(const QByteArray& data, size_t offset = 0){
    return ExtractSingle<T>(data, offset);
}

template<typename T, typename... Rest>
typename std::enable_if<(sizeof...(Rest) > 0), std::tuple<T, Rest...>>::type
ExtractData(const QByteArray& data, size_t offset = 0){
    auto val = ExtractSingle<T>(data, offset);
    return std::tuple_cat(std::move(val), ExtractData<Rest...>(data, offset + sizeof(T)));
}

which does exactly what I wanted. I imagine it could be made a little more performant but that's not needed for my application as of now. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.