#ifndef RFL_MSGPACK_WRITE_HPP_ #define RFL_MSGPACK_WRITE_HPP_ #include #include #include #include #include #include #include "../Processors.hpp" #include "../parsing/Parent.hpp" #include "Parser.hpp" namespace rfl::msgpack { /// Returns msgpack bytes. template std::vector write(const auto& _obj) noexcept { using T = std::remove_cvref_t; using ParentType = parsing::Parent; msgpack_sbuffer sbuf; msgpack_sbuffer_init(&sbuf); msgpack_packer pk; msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); auto w = Writer(&pk); Parser>::write(w, _obj, typename ParentType::Root{}); auto bytes = std::vector(sbuf.data, sbuf.data + sbuf.size); msgpack_sbuffer_destroy(&sbuf); return bytes; } /// Writes a MSGPACK into an ostream. template std::ostream& write(const auto& _obj, std::ostream& _stream) noexcept { auto buffer = write(_obj); _stream.write(buffer.data(), buffer.size()); return _stream; } } // namespace rfl::msgpack #endif