draconisplusplus/include/rfl/yaml/write.hpp

48 lines
1.2 KiB
C++
Raw Normal View History

2024-06-05 19:04:53 -04:00
#ifndef RFL_YAML_WRITE_HPP_
#define RFL_YAML_WRITE_HPP_
#include <ostream>
#include <sstream>
#include <string>
#include <type_traits>
2024-06-08 14:10:59 -04:00
#include <yaml-cpp/yaml.h>
2024-06-05 19:04:53 -04:00
#include "../Processors.hpp"
#include "../parsing/Parent.hpp"
#include "Parser.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
namespace yaml {
/// Writes a YAML 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>;
const auto out = Ref<YAML::Emitter>::make();
2024-06-08 15:53:06 -04:00
auto w = Writer(out);
Parser<T, Processors<Ps...>>::write(
w, _obj, typename ParentType::Root {}
);
2024-06-08 14:10:59 -04:00
_stream << out->c_str();
return _stream;
}
/// Returns a YAML string.
template <class... Ps>
std::string write(const auto& _obj) {
using T = std::remove_cvref_t<decltype(_obj)>;
using ParentType = parsing::Parent<Writer>;
const auto out = Ref<YAML::Emitter>::make();
2024-06-08 15:53:06 -04:00
auto w = Writer(out);
Parser<T, Processors<Ps...>>::write(
w, _obj, typename ParentType::Root {}
);
2024-06-08 14:10:59 -04:00
return out->c_str();
}
} // namespace yaml
} // namespace rfl
2024-06-05 19:04:53 -04:00
#endif