Skip to content

Commit d73d98f

Browse files
Change back to tomlplusplus (#401)
For a long time, tomlplusplus did generate a config.cmake file, which made it difficult to use on vcpkg. As a fix, we switched to toml11. But this has now finally been added to vcpkg, and since tomlplusplus is faster than toml11 in terms of compile time and runtime, we are changing back.
1 parent 0530379 commit d73d98f

File tree

12 files changed

+83
-85
lines changed

12 files changed

+83
-85
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ if (REFLECTCPP_TOML)
194194
list(APPEND REFLECT_CPP_SOURCES
195195
src/reflectcpp_toml.cpp
196196
)
197-
if (NOT TARGET toml11)
198-
find_package(toml11 CONFIG REQUIRED)
197+
if (NOT TARGET tomlplusplus)
198+
find_package(tomlplusplus)
199199
endif()
200-
target_link_libraries(reflectcpp PUBLIC toml11::toml11)
200+
target_link_libraries(reflectcpp PUBLIC tomlplusplus::tomlplusplus)
201201
endif()
202202

203203
if (REFLECTCPP_UBJSON)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ The following table lists the serialization formats currently supported by refle
6969
| CBOR | [jsoncons](https://github.com/danielaparker/jsoncons)| >= 0.176.0 | BSL 1.0 | JSON-like binary format |
7070
| flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | Schema-less version of flatbuffers, binary format |
7171
| msgpack | [msgpack-c](https://github.com/msgpack/msgpack-c) | >= 6.0.0 | BSL 1.0 | JSON-like binary format |
72-
| TOML | [toml11](https://github.com/ToruNiina/toml11) | >= 4.2.0 | MIT | Textual format with an emphasis on readability |
72+
| TOML | [toml++](https://github.com/marzer/tomlplusplus) | >= 3.4.0 | MIT | Textual format with an emphasis on readability |
7373
| UBJSON | [jsoncons](https://github.com/danielaparker/jsoncons)| >= 0.176.0 | BSL 1.0 | JSON-like binary format |
7474
| XML | [pugixml](https://github.com/zeux/pugixml) | >= 1.14 | MIT | Textual format used in many legacy projects |
7575
| YAML | [yaml-cpp](https://github.com/jbeder/yaml-cpp) | >= 0.8.0 | MIT | Textual format with an emphasis on readability |

conanfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def requirements(self):
8080
if self.options.with_msgpack:
8181
self.requires("msgpack-c/6.0.0", transitive_headers=True)
8282
if self.options.with_toml:
83-
self.requires("toml11/4.4.0", transitive_headers=True)
83+
self.requires("tomlplusplus/3.4.0", transitive_headers=True)
8484
if self.options.with_xml:
8585
self.requires("pugixml/1.14", transitive_headers=True)
8686
if self.options.with_yaml:

docs/supported_formats/toml.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# TOML
22

3-
For TOML support, you must also include the header `<rfl/toml.hpp>` and include the [toml11](https://github.com/ToruNiina/toml11) library.
3+
For TOML support, you must also include the header `<rfl/toml.hpp>` and include the [toml++](https://github.com/marzer/tomlplusplus) library.
44
Furthermore, when compiling reflect-cpp, you need to pass `-DREFLECTCPP_TOML=ON` to cmake. If you are using vcpkg or Conan, there
55
should be an appropriate feature (vcpkg) or option (Conan) that will abstract this away for you.
66

include/rfl/toml/Reader.hpp

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,20 @@
99
#include <stdexcept>
1010
#include <string>
1111
#include <string_view>
12+
#include <toml++/toml.hpp>
1213
#include <type_traits>
1314
#include <unordered_map>
1415
#include <vector>
1516

16-
#pragma GCC diagnostic push
17-
#pragma GCC diagnostic ignored "-Warray-bounds"
18-
#include <toml.hpp>
19-
#pragma GCC diagnostic pop
20-
2117
#include "../Result.hpp"
2218
#include "../always_false.hpp"
2319

2420
namespace rfl::toml {
2521

2622
struct Reader {
27-
using InputArrayType = const ::toml::array*;
28-
using InputObjectType = const ::toml::table*;
29-
using InputVarType = const ::toml::value*;
23+
using InputArrayType = ::toml::array*;
24+
using InputObjectType = ::toml::table*;
25+
using InputVarType = ::toml::node*;
3026

3127
template <class T>
3228
static constexpr bool has_custom_constructor =
@@ -37,64 +33,66 @@ struct Reader {
3733
if (_idx >= _arr->size()) {
3834
return error("Index " + std::to_string(_idx) + " of of bounds.");
3935
}
40-
return &(*_arr)[_idx];
36+
return _arr->get(_idx);
4137
}
4238

4339
rfl::Result<InputVarType> get_field_from_object(
44-
const std::string& _name, const InputObjectType _obj) const noexcept {
45-
const auto it = _obj->find(_name);
46-
if (it == _obj->end()) {
40+
const std::string& _name, const InputObjectType& _obj) const noexcept {
41+
auto var = (*_obj)[_name];
42+
if (!var) {
4743
return error("Object contains no field named '" + _name + "'.");
4844
}
49-
return &it->second;
45+
return var.node();
5046
}
5147

52-
bool is_empty(const InputVarType _var) const noexcept {
53-
return _var->is_empty();
48+
bool is_empty(const InputVarType& _var) const noexcept {
49+
return !_var && true;
5450
}
5551

5652
template <class T>
57-
rfl::Result<T> to_basic_type(const InputVarType _var) const noexcept {
53+
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
5854
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
59-
if (!_var->is_string()) {
60-
return error("Could not cast to std::string!");
55+
const auto ptr = _var->as<std::string>();
56+
if (!ptr) {
57+
return error("Could not cast the node to std::string!");
6158
}
62-
return _var->as_string();
63-
59+
return **ptr;
6460
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
65-
if (!_var->is_boolean()) {
66-
return error("Could not cast to bool!");
61+
const auto ptr = _var->as<bool>();
62+
if (!ptr) {
63+
return error("Could not cast the node to bool!");
6764
}
68-
return _var->as_boolean();
69-
65+
return **ptr;
7066
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
71-
if (!_var->is_floating()) {
72-
return error("Could not cast to double!");
67+
const auto ptr = _var->as<double>();
68+
if (!ptr) {
69+
return error("Could not cast the node to double!");
7370
}
74-
return static_cast<std::remove_cvref_t<T>>(_var->as_floating());
75-
71+
return static_cast<std::remove_cvref_t<T>>(**ptr);
7672
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
77-
if (!_var->is_integer()) {
78-
return error("Could not cast to int64_t!");
73+
const auto ptr = _var->as<int64_t>();
74+
if (!ptr) {
75+
return error("Could not cast the node to int64_t!");
7976
}
80-
return static_cast<std::remove_cvref_t<T>>(_var->as_integer());
81-
77+
return static_cast<std::remove_cvref_t<T>>(**ptr);
8278
} else {
8379
static_assert(rfl::always_false_v<T>, "Unsupported type.");
8480
}
8581
}
8682

87-
rfl::Result<InputArrayType> to_array(const InputVarType _var) const noexcept {
88-
if (!_var->is_array()) {
83+
rfl::Result<InputArrayType> to_array(
84+
const InputVarType& _var) const noexcept {
85+
const auto ptr = _var->as_array();
86+
if (!ptr) {
8987
return error("Could not cast to an array!");
9088
}
91-
return &_var->as_array();
89+
return ptr;
9290
}
9391

9492
template <class ArrayReader>
9593
std::optional<Error> read_array(const ArrayReader& _array_reader,
96-
const InputArrayType _arr) const noexcept {
97-
for (const auto& node : *_arr) {
94+
const InputArrayType& _arr) const noexcept {
95+
for (auto& node : *_arr) {
9896
const auto err = _array_reader.read(&node);
9997
if (err) {
10098
return err;
@@ -106,18 +104,19 @@ struct Reader {
106104
template <class ObjectReader>
107105
std::optional<Error> read_object(const ObjectReader& _object_reader,
108106
InputObjectType _obj) const noexcept {
109-
for (const auto& [k, v] : *_obj) {
107+
for (auto& [k, v] : *_obj) {
110108
_object_reader.read(std::string_view(k), &v);
111109
}
112110
return std::nullopt;
113111
}
114112

115113
rfl::Result<InputObjectType> to_object(
116-
const InputVarType _var) const noexcept {
117-
if (!_var->is_table()) {
114+
const InputVarType& _var) const noexcept {
115+
const auto ptr = _var->as_table();
116+
if (!ptr) {
118117
return error("Could not cast to a table!");
119118
}
120-
return &_var->as_table();
119+
return ptr;
121120
}
122121

123122
template <class T>

include/rfl/toml/Writer.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77
#include <stdexcept>
88
#include <string>
99
#include <string_view>
10+
#include <toml++/toml.hpp>
1011
#include <type_traits>
1112
#include <vector>
1213

13-
#pragma GCC diagnostic push
14-
#pragma GCC diagnostic ignored "-Warray-bounds"
15-
#include <toml.hpp>
16-
#pragma GCC diagnostic pop
17-
1814
#include "../Ref.hpp"
1915
#include "../Result.hpp"
2016
#include "../always_false.hpp"
@@ -80,7 +76,7 @@ class Writer {
8076
OutputVarType add_value_to_object(const std::string_view& _name,
8177
const T& _var,
8278
OutputObjectType* _parent) const noexcept {
83-
(*_parent->val_)[std::string(_name)] = ::toml::value(_var);
79+
_parent->val_->emplace(_name, ::toml::value(_var));
8480
return OutputVarType{};
8581
}
8682

include/rfl/toml/read.hpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33

44
#include <istream>
55
#include <string>
6-
#include <string_view>
7-
8-
#pragma GCC diagnostic push
9-
#pragma GCC diagnostic ignored "-Warray-bounds"
10-
#include <toml.hpp>
11-
#pragma GCC diagnostic pop
6+
#include <toml++/toml.hpp>
127

138
#include "../Processors.hpp"
149
#include "../internal/wrap_in_rfl_array_t.hpp"
@@ -33,12 +28,8 @@ auto read(InputVarType _var) {
3328
/// Reads a TOML string.
3429
template <class T, class... Ps>
3530
Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _toml_str) {
36-
auto res = ::toml::try_parse_str(_toml_str);
37-
if (res.is_ok()) {
38-
return read<T, Ps...>(&res.unwrap());
39-
} else {
40-
return error(::toml::format_error(res.unwrap_err().at(0)));
41-
}
31+
auto table = ::toml::parse(_toml_str);
32+
return read<T, Ps...>(&table);
4233
}
4334

4435
/// Reads a TOML string.

include/rfl/toml/write.hpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <ostream>
55
#include <sstream>
66
#include <string>
7-
#include <toml.hpp>
7+
#include <toml++/toml.hpp>
88
#include <type_traits>
99

1010
#include "../Processors.hpp"
@@ -13,27 +13,37 @@
1313

1414
namespace rfl::toml {
1515

16-
/// Returns a TOML string.
16+
/// Writes a TOML into an ostream.
1717
template <class... Ps>
18-
std::string write(const auto& _obj) {
18+
std::ostream& write(const auto& _obj, std::ostream& _stream) {
1919
using T = std::remove_cvref_t<decltype(_obj)>;
2020
using ParentType = parsing::Parent<Writer>;
21-
std::stringstream sstream;
2221
::toml::table root;
2322
auto w = Writer(&root);
2423
using ProcessorsType = Processors<Ps...>;
2524
static_assert(!ProcessorsType::no_field_names_,
2625
"The NoFieldNames processor is not supported for BSON, XML, "
2726
"TOML, or YAML.");
2827
Parser<T, ProcessorsType>::write(w, _obj, typename ParentType::Root{});
29-
return ::toml::format(::toml::value(root));
28+
_stream << root;
29+
return _stream;
3030
}
3131

32-
/// Writes a TOML into an ostream.
32+
/// Returns a TOML string.
3333
template <class... Ps>
34-
std::ostream& write(const auto& _obj, std::ostream& _stream) {
35-
_stream << write(_obj);
36-
return _stream;
34+
std::string write(const auto& _obj) {
35+
using T = std::remove_cvref_t<decltype(_obj)>;
36+
using ParentType = parsing::Parent<Writer>;
37+
std::stringstream sstream;
38+
::toml::table root;
39+
auto w = Writer(&root);
40+
using ProcessorsType = Processors<Ps...>;
41+
static_assert(!ProcessorsType::no_field_names_,
42+
"The NoFieldNames processor is not supported for BSON, XML, "
43+
"TOML, or YAML.");
44+
Parser<T, ProcessorsType>::write(w, _obj, typename ParentType::Root{});
45+
sstream << root;
46+
return sstream.str();
3747
}
3848

3949
} // namespace rfl::toml

src/rfl/toml/Writer.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,30 @@ Writer::OutputVarType Writer::null_as_root() const noexcept {
1717

1818
Writer::OutputArrayType Writer::add_array_to_array(
1919
const size_t _size, OutputArrayType* _parent) const noexcept {
20+
const auto i = _parent->val_->size();
2021
_parent->val_->push_back(::toml::array());
21-
return OutputArrayType{&_parent->val_->back().as_array()};
22+
return OutputArrayType{_parent->val_->at(i).as_array()};
2223
}
2324

2425
Writer::OutputArrayType Writer::add_array_to_object(
2526
const std::string_view& _name, const size_t _size,
2627
OutputObjectType* _parent) const noexcept {
2728
_parent->val_->emplace(_name, ::toml::array());
28-
return OutputArrayType{&(*_parent->val_)[std::string(_name)].as_array()};
29+
return OutputArrayType{_parent->val_->at_path(_name).as_array()};
2930
}
3031

3132
Writer::OutputObjectType Writer::add_object_to_array(
3233
const size_t _size, OutputArrayType* _parent) const noexcept {
34+
const auto i = _parent->val_->size();
3335
_parent->val_->push_back(::toml::table());
34-
return OutputObjectType{&_parent->val_->back().as_table()};
36+
return OutputObjectType{_parent->val_->at(i).as_table()};
3537
}
3638

3739
Writer::OutputObjectType Writer::add_object_to_object(
3840
const std::string_view& _name, const size_t _size,
3941
OutputObjectType* _parent) const noexcept {
4042
_parent->val_->emplace(_name, ::toml::table());
41-
return OutputObjectType{&(*_parent->val_)[std::string(_name)].as_table()};
43+
return OutputObjectType{_parent->val_->at_path(_name).as_table()};
4244
}
4345

4446
Writer::OutputVarType Writer::add_null_to_array(

tests/json/test_json_schema4.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "write_and_read.hpp"
1010

11-
namespace test_json_schema {
11+
namespace test_json_schema4 {
1212

1313
enum class Color { red, green, blue };
1414

@@ -66,8 +66,8 @@ TEST(json, test_json_schema4) {
6666
rfl::json::to_schema<Person, rfl::DefaultIfMissing>();
6767

6868
const std::string expected =
69-
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/definitions/test_json_schema__Person","definitions":{"rfl__Generic":{"anyOf":[{"anyOf":[{"type":"boolean"},{"type":"integer"},{"type":"number"},{"type":"string"},{"type":"object","additionalProperties":{"$ref":"#/definitions/rfl__Generic"}},{"type":"array","items":{"$ref":"#/definitions/rfl__Generic"}}]},{"type":"null"}]},"test_json_schema__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":[]},"test_json_schema__Circle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Circle"]},"radius":{"type":"number"}},"required":[]},"test_json_schema__Person":{"type":"object","properties":{"firstName":{"type":"string"},"lastName":{"type":"string"},"email":{"type":"string","description":"Must be a proper email in the form [email protected].","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"town":{"type":"string"},"color":{"type":"string","enum":["red","green","blue"]},"age":{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},"salary":{"type":"number"},"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/definitions/test_json_schema__Person"}},"variant":{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema__Person"}},{"type":"integer"}]},"tuple":{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema__Person"}},{"type":"integer"}],"items":false},"taggedUnion":{"anyOf":[{"$ref":"#/definitions/test_json_schema__Rectangle__tagged"},{"$ref":"#/definitions/test_json_schema__Square__tagged"},{"$ref":"#/definitions/test_json_schema__Circle__tagged"}]},"fieldVariant":{"anyOf":[{"type":"object","properties":{"rectangle":{"$ref":"#/definitions/test_json_schema__Rectangle"}},"required":[]},{"type":"object","properties":{"square":{"$ref":"#/definitions/test_json_schema__Square"}},"required":[]},{"type":"object","properties":{"circle":{"$ref":"#/definitions/test_json_schema__Circle"}},"required":[]}]},"generic":{"$ref":"#/definitions/rfl__Generic"},"with_extra_fields":{"$ref":"#/definitions/test_json_schema__WithExtraFields"}},"required":[]},"test_json_schema__Rectangle":{"type":"object","properties":{"width":{"type":"number"},"height":{"type":"number"}},"required":[]},"test_json_schema__Rectangle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Rectangle"]},"width":{"type":"number"},"height":{"type":"number"}},"required":[]},"test_json_schema__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":[]},"test_json_schema__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":[]},"test_json_schema__WithExtraFields":{"type":"object","properties":{"f1":{"type":"string"},"f2":{"type":"string"}},"required":[],"additionalProperties":{"type":"string"}}}})";
69+
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/definitions/test_json_schema4__Person","definitions":{"rfl__Generic":{"anyOf":[{"anyOf":[{"type":"boolean"},{"type":"integer"},{"type":"number"},{"type":"string"},{"type":"object","additionalProperties":{"$ref":"#/definitions/rfl__Generic"}},{"type":"array","items":{"$ref":"#/definitions/rfl__Generic"}}]},{"type":"null"}]},"test_json_schema4__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":[]},"test_json_schema4__Circle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Circle"]},"radius":{"type":"number"}},"required":[]},"test_json_schema4__Person":{"type":"object","properties":{"firstName":{"type":"string"},"lastName":{"type":"string"},"email":{"type":"string","description":"Must be a proper email in the form [email protected].","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"town":{"type":"string"},"color":{"type":"string","enum":["red","green","blue"]},"age":{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},"salary":{"type":"number"},"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/definitions/test_json_schema4__Person"}},"variant":{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema4__Person"}},{"type":"integer"}]},"tuple":{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema4__Person"}},{"type":"integer"}],"items":false},"taggedUnion":{"anyOf":[{"$ref":"#/definitions/test_json_schema4__Rectangle__tagged"},{"$ref":"#/definitions/test_json_schema4__Square__tagged"},{"$ref":"#/definitions/test_json_schema4__Circle__tagged"}]},"fieldVariant":{"anyOf":[{"type":"object","properties":{"rectangle":{"$ref":"#/definitions/test_json_schema4__Rectangle"}},"required":[]},{"type":"object","properties":{"square":{"$ref":"#/definitions/test_json_schema4__Square"}},"required":[]},{"type":"object","properties":{"circle":{"$ref":"#/definitions/test_json_schema4__Circle"}},"required":[]}]},"generic":{"$ref":"#/definitions/rfl__Generic"},"with_extra_fields":{"$ref":"#/definitions/test_json_schema4__WithExtraFields"}},"required":[]},"test_json_schema4__Rectangle":{"type":"object","properties":{"width":{"type":"number"},"height":{"type":"number"}},"required":[]},"test_json_schema4__Rectangle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Rectangle"]},"width":{"type":"number"},"height":{"type":"number"}},"required":[]},"test_json_schema4__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":[]},"test_json_schema4__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":[]},"test_json_schema4__WithExtraFields":{"type":"object","properties":{"f1":{"type":"string"},"f2":{"type":"string"}},"required":[],"additionalProperties":{"type":"string"}}}})";
7070

7171
EXPECT_EQ(json_schema, expected);
7272
}
73-
} // namespace test_json_schema
73+
} // namespace test_json_schema4

0 commit comments

Comments
 (0)