mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-02 16:16:19 +08:00
Use toml11 instead of tomlplusplus (#381)
This commit is contained in:
+3
-9
@@ -194,16 +194,10 @@ if (REFLECTCPP_TOML)
|
||||
list(APPEND REFLECT_CPP_SOURCES
|
||||
src/reflectcpp_toml.cpp
|
||||
)
|
||||
if(REFLECTCPP_USE_VCPKG)
|
||||
find_package(PkgConfig REQUIRED)
|
||||
pkg_check_modules(tomlplusplus REQUIRED IMPORTED_TARGET tomlplusplus)
|
||||
target_link_libraries(reflectcpp PUBLIC PkgConfig::tomlplusplus)
|
||||
else()
|
||||
if (NOT TARGET tomlplusplus)
|
||||
find_package(tomlplusplus)
|
||||
endif()
|
||||
target_link_libraries(reflectcpp PUBLIC tomlplusplus::tomlplusplus)
|
||||
if (NOT TARGET toml11)
|
||||
find_package(toml11 CONFIG REQUIRED)
|
||||
endif()
|
||||
target_link_libraries(reflectcpp PUBLIC toml11::toml11)
|
||||
endif()
|
||||
|
||||
if (REFLECTCPP_UBJSON)
|
||||
|
||||
@@ -69,7 +69,7 @@ The following table lists the serialization formats currently supported by refle
|
||||
| CBOR | [jsoncons](https://github.com/danielaparker/jsoncons)| >= 0.176.0 | BSL 1.0 | JSON-like binary format |
|
||||
| flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | Schema-less version of flatbuffers, binary format |
|
||||
| msgpack | [msgpack-c](https://github.com/msgpack/msgpack-c) | >= 6.0.0 | BSL 1.0 | JSON-like binary format |
|
||||
| TOML | [toml++](https://github.com/marzer/tomlplusplus) | >= 3.4.0 | MIT | Textual format with an emphasis on readability |
|
||||
| TOML | [toml11](https://github.com/ToruNiina/toml11) | >= 4.2.0 | MIT | Textual format with an emphasis on readability |
|
||||
| UBJSON | [jsoncons](https://github.com/danielaparker/jsoncons)| >= 0.176.0 | BSL 1.0 | JSON-like binary format |
|
||||
| XML | [pugixml](https://github.com/zeux/pugixml) | >= 1.14 | MIT | Textual format used in many legacy projects |
|
||||
| YAML | [yaml-cpp](https://github.com/jbeder/yaml-cpp) | >= 0.8.0 | MIT | Textual format with an emphasis on readability |
|
||||
|
||||
+1
-1
@@ -80,7 +80,7 @@ class ReflectCppConan(ConanFile):
|
||||
if self.options.with_msgpack:
|
||||
self.requires("msgpack-c/6.0.0", transitive_headers=True)
|
||||
if self.options.with_toml:
|
||||
self.requires("tomlplusplus/3.4.0", transitive_headers=True)
|
||||
self.requires("toml11/4.4.0", transitive_headers=True)
|
||||
if self.options.with_xml:
|
||||
self.requires("pugixml/1.14", transitive_headers=True)
|
||||
if self.options.with_yaml:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# TOML
|
||||
|
||||
For TOML support, you must also include the header `<rfl/toml.hpp>` and include the [toml++](https://github.com/marzer/tomlplusplus) library.
|
||||
For TOML support, you must also include the header `<rfl/toml.hpp>` and include the [toml11](https://github.com/ToruNiina/toml11) library.
|
||||
Furthermore, when compiling reflect-cpp, you need to pass `-DREFLECTCPP_TOML=ON` to cmake. If you are using vcpkg or Conan, there
|
||||
should be an appropriate feature (vcpkg) or option (Conan) that will abstract this away for you.
|
||||
|
||||
|
||||
+37
-40
@@ -9,20 +9,24 @@
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <toml++/toml.hpp>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Warray-bounds"
|
||||
#include <toml.hpp>
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl::toml {
|
||||
|
||||
struct Reader {
|
||||
using InputArrayType = ::toml::array*;
|
||||
using InputObjectType = ::toml::table*;
|
||||
using InputVarType = ::toml::node*;
|
||||
using InputArrayType = const ::toml::array*;
|
||||
using InputObjectType = const ::toml::table*;
|
||||
using InputVarType = const ::toml::value*;
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor =
|
||||
@@ -33,66 +37,60 @@ struct Reader {
|
||||
if (_idx >= _arr->size()) {
|
||||
return error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
return _arr->get(_idx);
|
||||
return &(*_arr)[_idx];
|
||||
}
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
auto var = (*_obj)[_name];
|
||||
if (!var) {
|
||||
const std::string& _name, const InputObjectType _obj) const noexcept {
|
||||
const auto it = _obj->find(_name);
|
||||
if (it == _obj->end()) {
|
||||
return error("Object contains no field named '" + _name + "'.");
|
||||
}
|
||||
return var.node();
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return !_var && true;
|
||||
bool is_empty(const InputVarType _var) const noexcept {
|
||||
return _var->is_empty();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
rfl::Result<T> to_basic_type(const InputVarType _var) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
const auto ptr = _var->as<std::string>();
|
||||
if (!ptr) {
|
||||
return error("Could not cast the node to std::string!");
|
||||
if (!_var->is_string()) {
|
||||
return error("Could not cast to std::string!");
|
||||
}
|
||||
return **ptr;
|
||||
return _var->as_string();
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
const auto ptr = _var->as<bool>();
|
||||
if (!ptr) {
|
||||
return error("Could not cast the node to bool!");
|
||||
if (!_var->is_boolean()) {
|
||||
return error("Could not cast to bool!");
|
||||
}
|
||||
return **ptr;
|
||||
return _var->as_boolean();
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
const auto ptr = _var->as<double>();
|
||||
if (!ptr) {
|
||||
return error("Could not cast the node to double!");
|
||||
if (!_var->is_floating()) {
|
||||
return error("Could not cast to double!");
|
||||
}
|
||||
return static_cast<std::remove_cvref_t<T>>(**ptr);
|
||||
return static_cast<std::remove_cvref_t<T>>(_var->as_floating());
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
const auto ptr = _var->as<int64_t>();
|
||||
if (!ptr) {
|
||||
return error("Could not cast the node to int64_t!");
|
||||
if (!_var->is_integer()) {
|
||||
return error("Could not cast to int64_t!");
|
||||
}
|
||||
return static_cast<std::remove_cvref_t<T>>(**ptr);
|
||||
return static_cast<std::remove_cvref_t<T>>(_var->as_integer());
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
const auto ptr = _var->as_array();
|
||||
if (!ptr) {
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType _var) const noexcept {
|
||||
if (!_var->is_array()) {
|
||||
return error("Could not cast to an array!");
|
||||
}
|
||||
return ptr;
|
||||
return &_var->as_array();
|
||||
}
|
||||
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
const InputArrayType& _arr) const noexcept {
|
||||
for (auto& node : *_arr) {
|
||||
const InputArrayType _arr) const noexcept {
|
||||
for (const auto& node : *_arr) {
|
||||
const auto err = _array_reader.read(&node);
|
||||
if (err) {
|
||||
return err;
|
||||
@@ -104,19 +102,18 @@ struct Reader {
|
||||
template <class ObjectReader>
|
||||
std::optional<Error> read_object(const ObjectReader& _object_reader,
|
||||
InputObjectType _obj) const noexcept {
|
||||
for (auto& [k, v] : *_obj) {
|
||||
for (const auto& [k, v] : *_obj) {
|
||||
_object_reader.read(std::string_view(k), &v);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
const auto ptr = _var->as_table();
|
||||
if (!ptr) {
|
||||
const InputVarType _var) const noexcept {
|
||||
if (!_var->is_table()) {
|
||||
return error("Could not cast to a table!");
|
||||
}
|
||||
return ptr;
|
||||
return &_var->as_table();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
|
||||
@@ -7,10 +7,14 @@
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <toml++/toml.hpp>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Warray-bounds"
|
||||
#include <toml.hpp>
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
@@ -76,7 +80,7 @@ class Writer {
|
||||
OutputVarType add_value_to_object(const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
_parent->val_->emplace(_name, ::toml::value(_var));
|
||||
(*_parent->val_)[std::string(_name)] = ::toml::value(_var);
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,11 @@
|
||||
|
||||
#include <istream>
|
||||
#include <string>
|
||||
#include <toml++/toml.hpp>
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Warray-bounds"
|
||||
#include <toml.hpp>
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
#include "../Processors.hpp"
|
||||
#include "../internal/wrap_in_rfl_array_t.hpp"
|
||||
@@ -28,8 +32,12 @@ auto read(InputVarType _var) {
|
||||
/// Parses an object from TOML using reflection.
|
||||
template <class T, class... Ps>
|
||||
Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _toml_str) {
|
||||
auto table = ::toml::parse(_toml_str);
|
||||
return read<T, Ps...>(&table);
|
||||
auto res = ::toml::try_parse_str(_toml_str);
|
||||
if (res.is_ok()) {
|
||||
return read<T, Ps...>(&res.unwrap());
|
||||
} else {
|
||||
return error(::toml::format_error(res.unwrap_err().at(0)));
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses an object from a stringstream.
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <toml++/toml.hpp>
|
||||
#include <toml.hpp>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Processors.hpp"
|
||||
@@ -13,22 +13,6 @@
|
||||
|
||||
namespace rfl::toml {
|
||||
|
||||
/// 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);
|
||||
using ProcessorsType = Processors<Ps...>;
|
||||
static_assert(!ProcessorsType::no_field_names_,
|
||||
"The NoFieldNames processor is not supported for BSON, XML, "
|
||||
"TOML, or YAML.");
|
||||
Parser<T, ProcessorsType>::write(w, _obj, typename ParentType::Root{});
|
||||
_stream << root;
|
||||
return _stream;
|
||||
}
|
||||
|
||||
/// Returns a TOML string.
|
||||
template <class... Ps>
|
||||
std::string write(const auto& _obj) {
|
||||
@@ -42,8 +26,14 @@ std::string write(const auto& _obj) {
|
||||
"The NoFieldNames processor is not supported for BSON, XML, "
|
||||
"TOML, or YAML.");
|
||||
Parser<T, ProcessorsType>::write(w, _obj, typename ParentType::Root{});
|
||||
sstream << root;
|
||||
return sstream.str();
|
||||
return ::toml::format(::toml::value(root));
|
||||
}
|
||||
|
||||
/// Writes a TOML into an ostream.
|
||||
template <class... Ps>
|
||||
std::ostream& write(const auto& _obj, std::ostream& _stream) {
|
||||
_stream << write(_obj);
|
||||
return _stream;
|
||||
}
|
||||
|
||||
} // namespace rfl::toml
|
||||
|
||||
@@ -17,30 +17,28 @@ Writer::OutputVarType Writer::null_as_root() const noexcept {
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
const auto i = _parent->val_->size();
|
||||
_parent->val_->push_back(::toml::array());
|
||||
return OutputArrayType{_parent->val_->at(i).as_array()};
|
||||
return OutputArrayType{&_parent->val_->back().as_array()};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
_parent->val_->emplace(_name, ::toml::array());
|
||||
return OutputArrayType{_parent->val_->at_path(_name).as_array()};
|
||||
return OutputArrayType{&(*_parent->val_)[std::string(_name)].as_array()};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
const auto i = _parent->val_->size();
|
||||
_parent->val_->push_back(::toml::table());
|
||||
return OutputObjectType{_parent->val_->at(i).as_table()};
|
||||
return OutputObjectType{&_parent->val_->back().as_table()};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
_parent->val_->emplace(_name, ::toml::table());
|
||||
return OutputObjectType{_parent->val_->at_path(_name).as_table()};
|
||||
return OutputObjectType{&(*_parent->val_)[std::string(_name)].as_table()};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_array(
|
||||
|
||||
+2
-2
@@ -48,8 +48,8 @@
|
||||
"version>=": "3.9.3"
|
||||
},
|
||||
{
|
||||
"name": "tomlplusplus",
|
||||
"version>=": "3.4.0"
|
||||
"name": "toml11",
|
||||
"version>=": "4.2.0"
|
||||
},
|
||||
{
|
||||
"name": "yaml-cpp",
|
||||
|
||||
Reference in New Issue
Block a user