:3
This commit is contained in:
parent
a743cdabe5
commit
bd402f57f5
276 changed files with 37936 additions and 22932 deletions
|
@ -6,12 +6,12 @@
|
|||
#include "Writer.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
namespace yaml {
|
||||
|
||||
template <class T, class ProcessorsType>
|
||||
using Parser = parsing::Parser<Reader, Writer, T, ProcessorsType>;
|
||||
template <class T, class ProcessorsType>
|
||||
using Parser = parsing::Parser<Reader, Writer, T, ProcessorsType>;
|
||||
|
||||
}
|
||||
} // namespace rfl
|
||||
}
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
#ifndef RFL_YAML_READER_HPP_
|
||||
#define RFL_YAML_READER_HPP_
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <array>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
|
@ -14,124 +12,119 @@
|
|||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
namespace yaml {
|
||||
|
||||
struct Reader {
|
||||
struct YAMLInputArray {
|
||||
YAMLInputArray(const YAML::Node& _node) : node_(_node) {}
|
||||
YAML::Node node_;
|
||||
};
|
||||
struct Reader {
|
||||
struct YAMLInputArray {
|
||||
YAMLInputArray(const YAML::Node& _node) : node_(_node) {}
|
||||
YAML::Node node_;
|
||||
};
|
||||
|
||||
struct YAMLInputObject {
|
||||
YAMLInputObject(const YAML::Node& _node) : node_(_node) {}
|
||||
YAML::Node node_;
|
||||
};
|
||||
struct YAMLInputObject {
|
||||
YAMLInputObject(const YAML::Node& _node) : node_(_node) {}
|
||||
YAML::Node node_;
|
||||
};
|
||||
|
||||
struct YAMLInputVar {
|
||||
YAMLInputVar(const YAML::Node& _node) : node_(_node) {}
|
||||
YAML::Node node_;
|
||||
};
|
||||
struct YAMLInputVar {
|
||||
YAMLInputVar(const YAML::Node& _node) : node_(_node) {}
|
||||
YAML::Node node_;
|
||||
};
|
||||
|
||||
using InputArrayType = YAMLInputArray;
|
||||
using InputObjectType = YAMLInputObject;
|
||||
using InputVarType = YAMLInputVar;
|
||||
using InputArrayType = YAMLInputArray;
|
||||
using InputObjectType = YAMLInputObject;
|
||||
using InputVarType = YAMLInputVar;
|
||||
|
||||
template <class T, class = void>
|
||||
struct has_from_json_obj : std::false_type {};
|
||||
template <class T, class = void>
|
||||
struct has_from_json_obj : std::false_type {};
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
|
||||
T::from_yaml_obj(var);
|
||||
});
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor =
|
||||
(requires(InputVarType var) { T::from_yaml_obj(var); });
|
||||
|
||||
rfl::Result<InputVarType> get_field(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
auto var = InputVarType(_obj.node_[_name]);
|
||||
if (!var.node_) {
|
||||
return rfl::Error("Object contains no field named '" + _name + "'.");
|
||||
}
|
||||
return var;
|
||||
}
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return !_var.node_ && true;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
try {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>() ||
|
||||
std::is_same<std::remove_cvref_t<T>, bool>() ||
|
||||
std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
return _var.node_.as<std::remove_cvref_t<T>>();
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
rfl::Result<InputVarType> get_field(
|
||||
const std::string& _name,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
auto var = InputVarType(_obj.node_[_name]);
|
||||
if (!var.node_) {
|
||||
return rfl::Error("Object contains no field named '" + _name + "'.");
|
||||
}
|
||||
return var;
|
||||
}
|
||||
} catch (std::exception& e) {
|
||||
return rfl::Error(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.node_.IsSequence()) {
|
||||
return rfl::Error("Could not cast to sequence!");
|
||||
}
|
||||
return InputArrayType(_var.node_);
|
||||
}
|
||||
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
const InputArrayType& _arr) const noexcept {
|
||||
for (size_t i = 0; i < _arr.node_.size(); ++i) {
|
||||
const auto err = _array_reader.read(_arr.node_[i]);
|
||||
if (err) {
|
||||
return err;
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return !_var.node_ && true;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
template <class ObjectReader>
|
||||
std::optional<Error> read_object(const ObjectReader& _object_reader,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
for (const auto& p : _obj.node_) {
|
||||
try {
|
||||
const auto k = p.first.as<std::string>();
|
||||
_object_reader.read(std::string_view(k), InputVarType(p.second));
|
||||
} catch (std::exception& e) {
|
||||
continue;
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
try {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>() ||
|
||||
std::is_same<std::remove_cvref_t<T>, bool>() ||
|
||||
std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
return _var.node_.as<std::remove_cvref_t<T>>();
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
} catch (std::exception& e) { return rfl::Error(e.what()); }
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.node_.IsMap()) {
|
||||
return rfl::Error("Could not cast to map!");
|
||||
}
|
||||
return InputObjectType(_var.node_);
|
||||
}
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.node_.IsSequence()) {
|
||||
return rfl::Error("Could not cast to sequence!");
|
||||
}
|
||||
return InputArrayType(_var.node_);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
const InputVarType _var) const noexcept {
|
||||
try {
|
||||
return T::from_yaml_obj(_var);
|
||||
} catch (std::exception& e) {
|
||||
return rfl::Error(e.what());
|
||||
}
|
||||
}
|
||||
};
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(
|
||||
const ArrayReader& _array_reader,
|
||||
const InputArrayType& _arr) const noexcept {
|
||||
for (size_t i = 0; i < _arr.node_.size(); ++i) {
|
||||
const auto err = _array_reader.read(_arr.node_[i]);
|
||||
if (err) { return err; }
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
template <class ObjectReader>
|
||||
std::optional<Error> read_object(
|
||||
const ObjectReader& _object_reader,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
for (const auto& p : _obj.node_) {
|
||||
try {
|
||||
const auto k = p.first.as<std::string>();
|
||||
_object_reader.read(std::string_view(k), InputVarType(p.second));
|
||||
} catch (std::exception& e) { continue; }
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.node_.IsMap()) {
|
||||
return rfl::Error("Could not cast to map!");
|
||||
}
|
||||
return InputObjectType(_var.node_);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
const InputVarType _var) const noexcept {
|
||||
try {
|
||||
return T::from_yaml_obj(_var);
|
||||
} catch (std::exception& e) { return rfl::Error(e.what()); }
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
#ifndef RFL_YAML_WRITER_HPP_
|
||||
#define RFL_YAML_WRITER_HPP_
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
|
@ -11,138 +9,146 @@
|
|||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
namespace yaml {
|
||||
|
||||
class Writer {
|
||||
public:
|
||||
struct YAMLArray {};
|
||||
class Writer {
|
||||
public:
|
||||
struct YAMLArray {};
|
||||
|
||||
struct YAMLObject {};
|
||||
struct YAMLObject {};
|
||||
|
||||
struct YAMLVar {};
|
||||
struct YAMLVar {};
|
||||
|
||||
using OutputArrayType = YAMLArray;
|
||||
using OutputObjectType = YAMLObject;
|
||||
using OutputVarType = YAMLVar;
|
||||
using OutputArrayType = YAMLArray;
|
||||
using OutputObjectType = YAMLObject;
|
||||
using OutputVarType = YAMLVar;
|
||||
|
||||
Writer(const Ref<YAML::Emitter>& _out) : out_(_out) {}
|
||||
Writer(const Ref<YAML::Emitter>& _out) : out_(_out) {}
|
||||
|
||||
~Writer() = default;
|
||||
~Writer() = default;
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
return insert_value(YAML::Null);
|
||||
}
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
return insert_value(YAML::Null);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
return insert_value(_var);
|
||||
}
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
return insert_value(_var);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
OutputArrayType add_array_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_array(_name);
|
||||
}
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_array(_name);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_object(_name);
|
||||
}
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_object(_name);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return insert_value(_var);
|
||||
}
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var, OutputArrayType* _parent)
|
||||
const noexcept {
|
||||
return insert_value(_var);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return insert_value(_name, _var);
|
||||
}
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(
|
||||
const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return insert_value(_name, _var);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
return insert_value(YAML::Null);
|
||||
}
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
return insert_value(YAML::Null);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return insert_value(_name, YAML::Null);
|
||||
}
|
||||
OutputVarType add_null_to_object(
|
||||
const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return insert_value(_name, YAML::Null);
|
||||
}
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {
|
||||
(*out_) << YAML::EndSeq;
|
||||
}
|
||||
void end_array(OutputArrayType* _arr) const noexcept {
|
||||
(*out_) << YAML::EndSeq;
|
||||
}
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {
|
||||
(*out_) << YAML::EndMap;
|
||||
}
|
||||
void end_object(OutputObjectType* _obj) const noexcept {
|
||||
(*out_) << YAML::EndMap;
|
||||
}
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
OutputVarType insert_value(const std::string_view& _name,
|
||||
const T& _var) const noexcept {
|
||||
(*out_) << YAML::Key << _name.data() << YAML::Value << _var;
|
||||
return OutputVarType{};
|
||||
}
|
||||
private:
|
||||
template <class T>
|
||||
OutputVarType insert_value(const std::string_view& _name,
|
||||
const T& _var) const noexcept {
|
||||
(*out_) << YAML::Key << _name.data() << YAML::Value << _var;
|
||||
return OutputVarType {};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType insert_value(const T& _var) const noexcept {
|
||||
(*out_) << _var;
|
||||
return OutputVarType{};
|
||||
}
|
||||
template <class T>
|
||||
OutputVarType insert_value(const T& _var) const noexcept {
|
||||
(*out_) << _var;
|
||||
return OutputVarType {};
|
||||
}
|
||||
|
||||
OutputArrayType new_array(const std::string_view& _name) const noexcept {
|
||||
(*out_) << YAML::Key << _name.data() << YAML::Value << YAML::BeginSeq;
|
||||
return OutputArrayType{};
|
||||
}
|
||||
OutputArrayType new_array(const std::string_view& _name) const noexcept {
|
||||
(*out_) << YAML::Key << _name.data() << YAML::Value << YAML::BeginSeq;
|
||||
return OutputArrayType {};
|
||||
}
|
||||
|
||||
OutputArrayType new_array() const noexcept {
|
||||
(*out_) << YAML::BeginSeq;
|
||||
return OutputArrayType{};
|
||||
}
|
||||
OutputArrayType new_array() const noexcept {
|
||||
(*out_) << YAML::BeginSeq;
|
||||
return OutputArrayType {};
|
||||
}
|
||||
|
||||
OutputObjectType new_object(const std::string_view& _name) const noexcept {
|
||||
(*out_) << YAML::Key << _name.data() << YAML::Value << YAML::BeginMap;
|
||||
return OutputObjectType{};
|
||||
}
|
||||
OutputObjectType new_object(
|
||||
const std::string_view& _name) const noexcept {
|
||||
(*out_) << YAML::Key << _name.data() << YAML::Value << YAML::BeginMap;
|
||||
return OutputObjectType {};
|
||||
}
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
(*out_) << YAML::BeginMap;
|
||||
return OutputObjectType{};
|
||||
}
|
||||
OutputObjectType new_object() const noexcept {
|
||||
(*out_) << YAML::BeginMap;
|
||||
return OutputObjectType {};
|
||||
}
|
||||
|
||||
public:
|
||||
const Ref<YAML::Emitter> out_;
|
||||
};
|
||||
public:
|
||||
const Ref<YAML::Emitter> out_;
|
||||
};
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
#endif // JSON_PARSER_HPP_
|
||||
|
|
|
@ -6,17 +6,17 @@
|
|||
#include "read.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
namespace yaml {
|
||||
|
||||
template <class T, class... Ps>
|
||||
Result<T> load(const std::string& _fname) {
|
||||
const auto read_string = [](const auto& _str) {
|
||||
return read<T, Ps...>(_str);
|
||||
};
|
||||
return rfl::io::load_string(_fname).and_then(read_string);
|
||||
}
|
||||
template <class T, class... Ps>
|
||||
Result<T> load(const std::string& _fname) {
|
||||
const auto read_string = [](const auto& _str) {
|
||||
return read<T, Ps...>(_str);
|
||||
};
|
||||
return rfl::io::load_string(_fname).and_then(read_string);
|
||||
}
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,47 +1,45 @@
|
|||
#ifndef RFL_YAML_READ_HPP_
|
||||
#define RFL_YAML_READ_HPP_
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <istream>
|
||||
#include <string>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include "../Processors.hpp"
|
||||
#include "../internal/wrap_in_rfl_array_t.hpp"
|
||||
#include "Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
namespace yaml {
|
||||
|
||||
using InputVarType = typename Reader::InputVarType;
|
||||
using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from a YAML var.
|
||||
template <class T, class... Ps>
|
||||
auto read(const InputVarType& _var) {
|
||||
const auto r = Reader();
|
||||
return Parser<T, Processors<Ps...>>::read(r, _var);
|
||||
}
|
||||
/// Parses an object from a YAML var.
|
||||
template <class T, class... Ps>
|
||||
auto read(const InputVarType& _var) {
|
||||
const auto r = Reader();
|
||||
return Parser<T, Processors<Ps...>>::read(r, _var);
|
||||
}
|
||||
|
||||
/// Parses an object from YAML using reflection.
|
||||
template <class T, class... Ps>
|
||||
Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _yaml_str) {
|
||||
try {
|
||||
const auto var = InputVarType(YAML::Load(_yaml_str));
|
||||
return read<T, Ps...>(var);
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
}
|
||||
/// Parses an object from YAML using reflection.
|
||||
template <class T, class... Ps>
|
||||
Result<internal::wrap_in_rfl_array_t<T>> read(
|
||||
const std::string& _yaml_str) {
|
||||
try {
|
||||
const auto var = InputVarType(YAML::Load(_yaml_str));
|
||||
return read<T, Ps...>(var);
|
||||
} catch (std::exception& e) { return Error(e.what()); }
|
||||
}
|
||||
|
||||
/// Parses an object from a stringstream.
|
||||
template <class T, class... Ps>
|
||||
auto read(std::istream& _stream) {
|
||||
const auto yaml_str = std::string(std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>());
|
||||
return read<T, Ps...>(yaml_str);
|
||||
}
|
||||
/// Parses an object from a stringstream.
|
||||
template <class T, class... Ps>
|
||||
auto read(std::istream& _stream) {
|
||||
const auto yaml_str = std::string(std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>());
|
||||
return read<T, Ps...>(yaml_str);
|
||||
}
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
|
|
@ -11,17 +11,17 @@
|
|||
#include "write.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
namespace yaml {
|
||||
|
||||
template <class... Ps>
|
||||
Result<Nothing> save(const std::string& _fname, const auto& _obj) {
|
||||
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
|
||||
return write<Ps...>(_obj, _stream);
|
||||
};
|
||||
return rfl::io::save_string(_fname, _obj, write_func);
|
||||
}
|
||||
template <class... Ps>
|
||||
Result<Nothing> save(const std::string& _fname, const auto& _obj) {
|
||||
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
|
||||
return write<Ps...>(_obj, _stream);
|
||||
};
|
||||
return rfl::io::save_string(_fname, _obj, write_func);
|
||||
}
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,44 +1,45 @@
|
|||
#ifndef RFL_YAML_WRITE_HPP_
|
||||
#define RFL_YAML_WRITE_HPP_
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include "../Processors.hpp"
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
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();
|
||||
auto w = Writer(out);
|
||||
Parser<T, Processors<Ps...>>::write(w, _obj, typename ParentType::Root{});
|
||||
_stream << out->c_str();
|
||||
return _stream;
|
||||
}
|
||||
/// 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();
|
||||
auto w = Writer(out);
|
||||
Parser<T, Processors<Ps...>>::write(w, _obj,
|
||||
typename ParentType::Root {});
|
||||
_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();
|
||||
auto w = Writer(out);
|
||||
Parser<T, Processors<Ps...>>::write(w, _obj, typename ParentType::Root{});
|
||||
return out->c_str();
|
||||
}
|
||||
/// 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();
|
||||
auto w = Writer(out);
|
||||
Parser<T, Processors<Ps...>>::write(w, _obj,
|
||||
typename ParentType::Root {});
|
||||
return out->c_str();
|
||||
}
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue