mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 16:56:17 +08:00
This commit is contained in:
@@ -31,7 +31,7 @@ auto read(const InputVarType& _obj) {
|
|||||||
/// Parses an object from JSON using reflection.
|
/// Parses an object from JSON using reflection.
|
||||||
template <class T, class... Ps>
|
template <class T, class... Ps>
|
||||||
Result<internal::wrap_in_rfl_array_t<T>> read(
|
Result<internal::wrap_in_rfl_array_t<T>> read(
|
||||||
std::string_view const _json_str, const yyjson_read_flag _flag = 0) {
|
const std::string_view _json_str, const yyjson_read_flag _flag = 0) {
|
||||||
yyjson_doc* doc = yyjson_read(_json_str.data(), _json_str.size(), _flag);
|
yyjson_doc* doc = yyjson_read(_json_str.data(), _json_str.size(), _flag);
|
||||||
if (!doc) {
|
if (!doc) {
|
||||||
return error("Could not parse document");
|
return error("Could not parse document");
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <istream>
|
#include <istream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
#pragma GCC diagnostic push
|
#pragma GCC diagnostic push
|
||||||
#pragma GCC diagnostic ignored "-Warray-bounds"
|
#pragma GCC diagnostic ignored "-Warray-bounds"
|
||||||
@@ -29,7 +30,7 @@ auto read(InputVarType _var) {
|
|||||||
return Parser<T, ProcessorsType>::read(r, _var);
|
return Parser<T, ProcessorsType>::read(r, _var);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses an object from TOML using reflection.
|
/// Reads a TOML string.
|
||||||
template <class T, class... Ps>
|
template <class T, class... Ps>
|
||||||
Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _toml_str) {
|
Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _toml_str) {
|
||||||
auto res = ::toml::try_parse_str(_toml_str);
|
auto res = ::toml::try_parse_str(_toml_str);
|
||||||
@@ -40,6 +41,13 @@ Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _toml_str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reads a TOML string.
|
||||||
|
template <class T, class... Ps>
|
||||||
|
Result<internal::wrap_in_rfl_array_t<T>> read(
|
||||||
|
const std::string_view _toml_str) {
|
||||||
|
return read<T, Ps...>(std::string(_toml_str));
|
||||||
|
}
|
||||||
|
|
||||||
/// Parses an object from a stringstream.
|
/// Parses an object from a stringstream.
|
||||||
template <class T, class... Ps>
|
template <class T, class... Ps>
|
||||||
auto read(std::istream& _stream) {
|
auto read(std::istream& _stream) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <istream>
|
#include <istream>
|
||||||
#include <pugixml.hpp>
|
#include <pugixml.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
#include "../Processors.hpp"
|
#include "../Processors.hpp"
|
||||||
#include "../internal/get_type_name.hpp"
|
#include "../internal/get_type_name.hpp"
|
||||||
@@ -29,9 +30,9 @@ auto read(const InputVarType& _var) {
|
|||||||
|
|
||||||
/// Parses an object from XML using reflection.
|
/// Parses an object from XML using reflection.
|
||||||
template <class T, class... Ps>
|
template <class T, class... Ps>
|
||||||
Result<T> read(const std::string& _xml_str) {
|
Result<T> read(const std::string_view _xml_str) {
|
||||||
pugi::xml_document doc;
|
pugi::xml_document doc;
|
||||||
const auto result = doc.load_string(_xml_str.c_str());
|
const auto result = doc.load_string(_xml_str.data());
|
||||||
if (!result) {
|
if (!result) {
|
||||||
return error("XML string could not be parsed: " +
|
return error("XML string could not be parsed: " +
|
||||||
std::string(result.description()));
|
std::string(result.description()));
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <istream>
|
#include <istream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
#include "../Processors.hpp"
|
#include "../Processors.hpp"
|
||||||
#include "../internal/wrap_in_rfl_array_t.hpp"
|
#include "../internal/wrap_in_rfl_array_t.hpp"
|
||||||
@@ -37,6 +38,13 @@ Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _yaml_str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses an object from YAML using reflection.
|
||||||
|
template <class T, class... Ps>
|
||||||
|
Result<internal::wrap_in_rfl_array_t<T>> read(
|
||||||
|
const std::string_view _yaml_str) {
|
||||||
|
return read<T, Ps...>(std::string(_yaml_str));
|
||||||
|
}
|
||||||
|
|
||||||
/// Parses an object from a stringstream.
|
/// Parses an object from a stringstream.
|
||||||
template <class T, class... Ps>
|
template <class T, class... Ps>
|
||||||
auto read(std::istream& _stream) {
|
auto read(std::istream& _stream) {
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ template <class... Ps>
|
|||||||
void write_and_read(const auto& _struct) {
|
void write_and_read(const auto& _struct) {
|
||||||
using T = std::remove_cvref_t<decltype(_struct)>;
|
using T = std::remove_cvref_t<decltype(_struct)>;
|
||||||
const auto serialized1 = rfl::toml::write<Ps...>(_struct);
|
const auto serialized1 = rfl::toml::write<Ps...>(_struct);
|
||||||
const auto res = rfl::toml::read<T, Ps...>(serialized1);
|
const auto res = rfl::toml::read<T, Ps...>(
|
||||||
|
std::string_view(serialized1.c_str(), serialized1.size()));
|
||||||
EXPECT_TRUE(res && true) << "Test failed on read. Error: "
|
EXPECT_TRUE(res && true) << "Test failed on read. Error: "
|
||||||
<< res.error().what();
|
<< res.error().what();
|
||||||
const auto serialized2 = rfl::toml::write<Ps...>(res.value());
|
const auto serialized2 = rfl::toml::write<Ps...>(res.value());
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ template <class... Ps>
|
|||||||
void write_and_read(const auto& _struct) {
|
void write_and_read(const auto& _struct) {
|
||||||
using T = std::remove_cvref_t<decltype(_struct)>;
|
using T = std::remove_cvref_t<decltype(_struct)>;
|
||||||
const auto serialized1 = rfl::yaml::write<Ps...>(_struct);
|
const auto serialized1 = rfl::yaml::write<Ps...>(_struct);
|
||||||
const auto res = rfl::yaml::read<T, Ps...>(serialized1);
|
const auto res = rfl::yaml::read<T, Ps...>(
|
||||||
|
std::string_view(serialized1.c_str(), serialized1.size()));
|
||||||
EXPECT_TRUE(res && true) << "Test failed on read. Error: "
|
EXPECT_TRUE(res && true) << "Test failed on read. Error: "
|
||||||
<< res.error().what();
|
<< res.error().what();
|
||||||
const auto serialized2 = rfl::yaml::write<Ps...>(res.value());
|
const auto serialized2 = rfl::yaml::write<Ps...>(res.value());
|
||||||
|
|||||||
Reference in New Issue
Block a user