#ifndef FLEXBUF_WRITE_HPP_ #define FLEXBUF_WRITE_HPP_ #include #include #include #include #include #include "../Processors.hpp" #include "../Ref.hpp" #include "../parsing/Parent.hpp" #include "Parser.hpp" namespace rfl { namespace flexbuf { template std::vector to_buffer(const auto& _obj) { using T = std::remove_cvref_t; using ParentType = parsing::Parent; const auto fbb = Ref::make(); auto w = Writer(fbb); Parser>::write(w, _obj, typename ParentType::Root{}); fbb->Finish(); return fbb->GetBuffer(); } /// Writes an object to flexbuf. template std::vector write(const auto& _obj) { const auto buffer = to_buffer(_obj); const auto data = reinterpret_cast(buffer.data()); return std::vector(data, data + buffer.size()); } /// Writes an object to an ostream. template std::ostream& write(const auto& _obj, std::ostream& _stream) { const auto buffer = to_buffer(_obj); const auto data = reinterpret_cast(buffer.data()); _stream.write(data, buffer.size()); return _stream; } } // namespace flexbuf } // namespace rfl #endif