Skip to content

Instantly share code, notes, and snippets.

@ctin
Created October 31, 2019 16:17
Show Gist options
  • Save ctin/15c21d112a76ad5eb7c51f8473b85038 to your computer and use it in GitHub Desktop.
Save ctin/15c21d112a76ad5eb7c51f8473b85038 to your computer and use it in GitHub Desktop.
namespace Serializer {
template <typename... Args>
std::string serialize(Args&&... args);
template <typename... Args>
void convert(const std::string& str, Args&&... args);
}; // namespace Serializer
namespace Serializer_internal {
//the darkest magic here
struct pass
{
template <typename... T>
pass(T...) {}
};
} // namespace Serializer_internal
template <typename... Args>
std::string Serializer::serialize(Args&&... args)
{
std::stringstream bufferToSend;
msgpack::packer<std::stringstream> packer(&bufferToSend);
packer.pack_array(sizeof...(args));
//42 is a reason, why this code is working. Without this number we passing void to c-tor. Really, that is not a joke
Serializer_internal::pass{(msgpack::pack(bufferToSend, std::forward<Args>(args)), 42)...};
return bufferToSend.str();
}
template <typename... Args>
void Serializer::convert(const std::string& str, Args&&... args)
{
msgpack::object_handle oh = msgpack::unpack(str.data(), str.size());
msgpack::object deserialized = oh.get();
std::tuple<Args...> t(std::forward<Args>(args)...);
deserialized.convert(t);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment