draconisplusplus/include/rfl/toml/write.hpp

44 lines
1.1 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_TOML_WRITE_HPP_
#define RFL_TOML_WRITE_HPP_
#include <ostream>
#include <sstream>
#include <string>
#include <toml++/toml.hpp>
#include <type_traits>
#include "../Processors.hpp"
#include "../parsing/Parent.hpp"
#include "Parser.hpp"
namespace rfl::toml {
2024-06-08 14:10:59 -04:00
/// Writes a TOML into an ostream.
template <class... Ps>
std::ostream& write(const auto& _obj, std::ostream& _stream) {
using T = std::remove_cvref_t<decltype(_obj)>;
using ParentType = parsing::Parent<Writer>;
::toml::table root;
auto w = Writer(&root);
Parser<T, Processors<Ps...>>::write(w, _obj, typename ParentType::Root {});
_stream << root;
return _stream;
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// Returns a TOML string.
template <class... Ps>
std::string write(const auto& _obj) {
using T = std::remove_cvref_t<decltype(_obj)>;
using ParentType = parsing::Parent<Writer>;
std::stringstream sstream;
::toml::table root;
auto w = Writer(&root);
Parser<T, Processors<Ps...>>::write(w, _obj, typename ParentType::Root {});
sstream << root;
return sstream.str();
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
} // namespace rfl::toml
2024-05-31 22:59:00 -04:00
#endif