vendredi 1 janvier 2021

C++ is overloading the right way to do this

In an attempt to improve my c++ I have decided to try to implement msgpack. All sorts of data structures can be passed in order to be serialized.

Example data structure:

vector<tuple<char, vector<int>, int, string, double, map<int, vector<string> >, float > >

Is function overloading the correct way to call methods for the different data structures to be serialized or is there a template based method / paradigm to do so? Wright now I have a bunch of overloaded functions like shown below where I loop through stl data structures and call pack again for their primitive values.

Overloaded functions (how I'm doing it right now)

// container is a buffer to store the serialized data
template<typename T>
void pack(std::vector<T>& src, container& dest, bool initial = true);
template<typename ...T>
void pack(std::tuple<T...>& src, container& dest, bool initial = true);
template<typename T, typename S>
void pack(std::map<T, S>& src, container& dest, bool initial = true);
void pack(const char src, container& dest, bool initial = false);
void pack(const char* src, size_t len, container& dest, bool initial = false);
void pack(const std::string& src, container& dest, bool initial = false);
void pack_uint(const uint64_t& src, container& dest, bool initial = false);
void pack_int(const int64_t& src, container& dest, bool initial = false);
void pack(uint8_t& src, container& dest, bool initial = false);
void pack(uint16_t& src, container& dest, bool initial = false);
void pack(uint32_t& src, container& dest, bool initial = false);
void pack(uint64_t& src, container& dest, bool initial = false);
void pack(int8_t& src, container& dest, bool initial = false);
void pack(int16_t& src, container& dest, bool initial = false);
void pack(int32_t& src, container& dest, bool initial = false);
void pack(int64_t& src, container& dest, bool initial = false);
void pack(double& src, container& dest, bool initial = false);
void pack(float& src, container& dest, bool initial = false);
void pack(bool& src, container& dest, bool initial = false);

Entire repo here

Aucun commentaire:

Enregistrer un commentaire