Move some heavy templates into cpp (#204)

This commit is contained in:
George Evmenov
2024-10-08 22:12:03 +02:00
committed by GitHub
parent 099211943d
commit 9dcd49a1e0
80 changed files with 2291 additions and 1330 deletions
-1
View File
@@ -15,7 +15,6 @@ template <class R, class W, class ProcessorsType>
requires AreReaderAndWriter<R, W, bson_oid_t>
struct Parser<R, W, ProcessorsType, bson_oid_t> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
using ParentType = Parent<W>;
+7 -58
View File
@@ -50,49 +50,12 @@ struct Reader {
});
rfl::Result<InputVarType> get_field_from_array(
const size_t _idx, const InputArrayType& _arr) const noexcept {
bson_t b;
bson_iter_t iter;
const auto doc = _arr.val_->value.v_doc;
if (bson_init_static(&b, doc.data, doc.data_len)) {
if (bson_iter_init(&iter, &b)) {
size_t i = 0;
while (bson_iter_next(&iter)) {
if (i == _idx) {
return to_input_var(&iter);
}
++i;
}
} else {
return Error("Could not init the array iteration.");
}
} else {
return Error("Could not init array.");
}
return Error("Index " + std::to_string(_idx) + " of of bounds.");
}
const size_t _idx, const InputArrayType& _arr) const noexcept;
rfl::Result<InputVarType> get_field_from_object(
const std::string& _name, const InputObjectType& _obj) const noexcept {
bson_t b;
bson_iter_t iter;
const auto doc = _obj.val_->value.v_doc;
if (bson_init_static(&b, doc.data, doc.data_len)) {
if (bson_iter_init(&iter, &b)) {
while (bson_iter_next(&iter)) {
auto key = std::string(bson_iter_key(&iter));
if (key == _name) {
return to_input_var(&iter);
}
}
}
}
return Error("No field named '" + _name + "' was found.");
}
const std::string& _name, const InputObjectType& _obj) const noexcept;
bool is_empty(const InputVarType& _var) const noexcept {
return _var.val_->value_type == BSON_TYPE_NULL;
}
bool is_empty(const InputVarType& _var) const noexcept;
template <class T>
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
@@ -159,13 +122,7 @@ struct Reader {
}
rfl::Result<InputArrayType> to_array(
const InputVarType& _var) const noexcept {
const auto btype = _var.val_->value_type;
if (btype != BSON_TYPE_ARRAY && btype != BSON_TYPE_DOCUMENT) {
return Error("Could not cast to an array.");
}
return InputArrayType{_var.val_};
}
const InputVarType& _var) const noexcept;
template <class ArrayReader>
std::optional<Error> read_array(const ArrayReader& _array_reader,
@@ -212,13 +169,7 @@ struct Reader {
}
rfl::Result<InputObjectType> to_object(
const InputVarType& _var) const noexcept {
const auto btype = _var.val_->value_type;
if (btype != BSON_TYPE_DOCUMENT) {
return Error("Could not cast to a document.");
}
return InputObjectType{_var.val_};
}
const InputVarType& _var) const noexcept;
template <class T>
rfl::Result<T> use_custom_constructor(
@@ -231,12 +182,10 @@ struct Reader {
}
private:
InputVarType to_input_var(bson_iter_t* _iter) const noexcept {
return InputVarType{bson_iter_value(_iter)};
}
InputVarType to_input_var(bson_iter_t* _iter) const noexcept;
};
} // namespace bson
} // namespace rfl
#endif // JSON_PARSER_HPP_
#endif
+14 -76
View File
@@ -62,23 +62,15 @@ class Writer {
using OutputObjectType = BSONOutputObject;
using OutputVarType = BSONOutputVar;
Writer(bson_t* _doc) : doc_(_doc) {}
Writer(bson_t* _doc);
~Writer() = default;
~Writer();
OutputArrayType array_as_root(const size_t _size) const noexcept {
bson_array_builder_t* val = bson_array_builder_new();
return OutputArrayType(val, IsRoot{});
}
OutputArrayType array_as_root(const size_t _size) const noexcept;
OutputObjectType object_as_root(const size_t _size) const noexcept {
return OutputObjectType(doc_, IsRoot{});
}
OutputObjectType object_as_root(const size_t _size) const noexcept;
OutputVarType null_as_root() const noexcept {
// Appears to be unsupported by the BSON C API.
return OutputVarType{};
}
OutputVarType null_as_root() const noexcept;
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {
@@ -88,38 +80,18 @@ class Writer {
}
OutputArrayType add_array_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept {
bson_array_builder_t* val;
bson_array_builder_append_array_builder_begin(_parent->val_, &val);
return OutputArrayType(val, IsArray{_parent->val_});
}
OutputArrayType* _parent) const noexcept;
OutputArrayType add_array_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
bson_array_builder_t* val;
bson_append_array_builder_begin(_parent->val_, _name.data(),
static_cast<int>(_name.size()), &val);
return OutputArrayType(val, IsObject{_parent->val_});
}
OutputObjectType* _parent) const noexcept;
OutputObjectType add_object_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
subdocs_->emplace_back(rfl::Box<BSONType>());
bson_array_builder_append_document_begin(_parent->val_,
&(subdocs_->back()->val_));
return OutputObjectType(&subdocs_->back()->val_, IsArray{_parent->val_});
}
const size_t _size, OutputArrayType* _parent) const noexcept;
OutputObjectType add_object_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
subdocs_->emplace_back(rfl::Box<BSONType>());
bson_append_document_begin(_parent->val_, _name.data(),
static_cast<int>(_name.size()),
&(subdocs_->back()->val_));
return OutputObjectType(&subdocs_->back()->val_, IsObject{_parent->val_});
}
OutputObjectType* _parent) const noexcept;
template <class T>
OutputVarType add_value_to_array(const T& _var,
@@ -183,48 +155,14 @@ class Writer {
return OutputVarType{};
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
bson_array_builder_append_null(_parent->val_);
return OutputVarType{};
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
OutputVarType add_null_to_object(const std::string_view& _name,
OutputObjectType* _parent) const noexcept {
bson_append_null(_parent->val_, _name.data(),
static_cast<int>(_name.size()));
return OutputVarType{};
}
OutputObjectType* _parent) const noexcept;
void end_array(OutputArrayType* _arr) const noexcept {
const auto handle = [&](const auto _parent) {
using Type = std::remove_cvref_t<decltype(_parent)>;
if constexpr (std::is_same<Type, IsArray>()) {
bson_array_builder_append_array_builder_end(_parent.ptr_, _arr->val_);
} else if constexpr (std::is_same<Type, IsObject>()) {
bson_append_array_builder_end(_parent.ptr_, _arr->val_);
} else if constexpr (std::is_same<Type, IsRoot>()) {
bson_array_builder_build(_arr->val_, doc_);
} else {
static_assert(rfl::always_false_v<Type>, "Unsupported type.");
}
};
std::visit(handle, _arr->parent_);
}
void end_array(OutputArrayType* _arr) const noexcept;
void end_object(OutputObjectType* _obj) const noexcept {
const auto handle = [&](const auto _parent) {
using Type = std::remove_cvref_t<decltype(_parent)>;
if constexpr (std::is_same<Type, IsArray>()) {
bson_array_builder_append_document_end(_parent.ptr_, _obj->val_);
} else if constexpr (std::is_same<Type, IsObject>()) {
bson_append_document_end(_parent.ptr_, _obj->val_);
} else if constexpr (std::is_same<Type, IsRoot>()) {
} else {
static_assert(rfl::always_false_v<Type>, "Unsupported type.");
}
};
std::visit(handle, _obj->parent_);
}
void end_object(OutputObjectType* _obj) const noexcept;
private:
/// Pointer to the main document. In BSON, documents are what are usually
@@ -238,4 +176,4 @@ class Writer {
} // namespace bson
} // namespace rfl
#endif // BSON_PARSER_HPP_
#endif
+1 -1
View File
@@ -62,4 +62,4 @@ std::ostream& write(const auto& _obj, std::ostream& _stream) noexcept {
} // namespace bson
} // namespace rfl
#endif // BSON_PARSER_HPP_
#endif
+8 -99
View File
@@ -41,68 +41,12 @@ struct Reader {
});
rfl::Result<InputVarType> get_field_from_array(
const size_t _idx, const InputArrayType& _arr) const noexcept {
InputVarType var;
auto err = cbor_value_enter_container(&_arr.val_, &var.val_);
if (err != CborNoError && err != CborErrorOutOfMemory) {
return Error(cbor_error_string(err));
}
size_t length = 0;
err = cbor_value_get_array_length(&_arr.val_, &length);
if (err != CborNoError && err != CborErrorOutOfMemory) {
return Error(cbor_error_string(err));
}
if (_idx >= length) {
return Error("Index " + std::to_string(_idx) + " of of bounds.");
}
for (size_t i = 0; i < _idx; ++i) {
err = cbor_value_advance(&var.val_);
if (err != CborNoError && err != CborErrorOutOfMemory) {
return Error(cbor_error_string(err));
}
}
return var;
}
const size_t _idx, const InputArrayType& _arr) const noexcept;
rfl::Result<InputVarType> get_field_from_object(
const std::string& _name, const InputObjectType& _obj) const noexcept {
InputVarType var;
auto buffer = std::string();
auto err = cbor_value_enter_container(&_obj.val_, &var.val_);
if (err != CborNoError) {
return Error(cbor_error_string(err));
}
size_t length = 0;
err = cbor_value_get_map_length(&_obj.val_, &length);
if (err != CborNoError) {
return Error(cbor_error_string(err));
}
for (size_t i = 0; i < length; ++i) {
if (!cbor_value_is_text_string(&var.val_)) {
return Error("Expected the key to be a string value.");
}
err = get_string(&var.val_, &buffer);
if (err != CborNoError) {
return Error(cbor_error_string(err));
}
err = cbor_value_advance(&var.val_);
if (err != CborNoError) {
return Error(cbor_error_string(err));
}
if (_name == buffer) {
return var;
}
err = cbor_value_advance(&var.val_);
if (err != CborNoError) {
return Error(cbor_error_string(err));
}
}
return Error("No field named '" + _name + "' was found.");
}
const std::string& _name, const InputObjectType& _obj) const noexcept;
bool is_empty(const InputVarType& _var) const noexcept {
return cbor_value_is_null(&_var.val_);
}
bool is_empty(const InputVarType& _var) const noexcept;
template <class T>
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
@@ -171,20 +115,10 @@ struct Reader {
}
rfl::Result<InputArrayType> to_array(
const InputVarType& _var) const noexcept {
if (!cbor_value_is_array(&_var.val_)) {
return Error("Could not cast to an array.");
}
return InputArrayType{_var.val_};
}
const InputVarType& _var) const noexcept;
rfl::Result<InputObjectType> to_object(
const InputVarType& _var) const noexcept {
if (!cbor_value_is_map(&_var.val_)) {
return Error("Could not cast to an object.");
}
return InputObjectType{_var.val_};
}
const InputVarType& _var) const noexcept;
template <class ArrayReader>
std::optional<Error> read_array(const ArrayReader& _array_reader,
@@ -259,38 +193,13 @@ struct Reader {
private:
CborError get_bytestring(const CborValue* _ptr,
rfl::Bytestring* _str) const noexcept {
size_t length = 0;
auto err = cbor_value_get_string_length(_ptr, &length);
if (err != CborNoError && err != CborErrorOutOfMemory) {
return err;
}
_str->resize(length);
if (length > 0) {
return cbor_value_copy_byte_string(
_ptr, reinterpret_cast<uint8_t*>(&((*_str)[0])), &length, NULL);
} else {
return CborNoError;
}
}
rfl::Bytestring* _str) const noexcept;
CborError get_string(const CborValue* _ptr,
std::string* _str) const noexcept {
size_t length = 0;
auto err = cbor_value_get_string_length(_ptr, &length);
if (err != CborNoError && err != CborErrorOutOfMemory) {
return err;
}
_str->resize(length);
if (length > 0) {
return cbor_value_copy_text_string(_ptr, &((*_str)[0]), &length, NULL);
} else {
return CborNoError;
}
}
std::string* _str) const noexcept;
};
} // namespace cbor
} // namespace rfl
#endif // JSON_PARSER_HPP_
#endif
+17 -53
View File
@@ -40,22 +40,15 @@ class Writer {
using OutputObjectType = CBOROutputObject;
using OutputVarType = CBOROutputVar;
Writer(CborEncoder* _encoder) : encoder_(_encoder) {}
Writer(CborEncoder* _encoder);
~Writer() = default;
~Writer();
OutputArrayType array_as_root(const size_t _size) const noexcept {
return new_array(_size, encoder_);
}
OutputArrayType array_as_root(const size_t _size) const noexcept;
OutputObjectType object_as_root(const size_t _size) const noexcept {
return new_object(_size, encoder_);
}
OutputObjectType object_as_root(const size_t _size) const noexcept;
OutputVarType null_as_root() const noexcept {
cbor_encode_null(encoder_);
return OutputVarType{};
}
OutputVarType null_as_root() const noexcept;
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {
@@ -63,28 +56,18 @@ class Writer {
}
OutputArrayType add_array_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept {
return new_array(_size, _parent->encoder_);
}
OutputArrayType* _parent) const noexcept;
OutputArrayType add_array_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
return new_array(_size, _parent->encoder_);
}
OutputObjectType* _parent) const noexcept;
OutputObjectType add_object_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
return new_object(_size, _parent->encoder_);
}
OutputObjectType add_object_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept;
OutputObjectType add_object_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
return new_object(_size, _parent->encoder_);
}
OutputObjectType* _parent) const noexcept;
template <class T>
OutputVarType add_value_to_array(const T& _var,
@@ -100,40 +83,21 @@ class Writer {
return new_value(_var, _parent->encoder_);
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
cbor_encode_null(_parent->encoder_);
return OutputVarType{};
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
OutputVarType add_null_to_object(const std::string_view& _name,
OutputObjectType* _parent) const noexcept {
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
cbor_encode_null(_parent->encoder_);
return OutputVarType{};
}
OutputObjectType* _parent) const noexcept;
void end_array(OutputArrayType* _arr) const noexcept {
cbor_encoder_close_container(_arr->parent_, _arr->encoder_);
}
void end_array(OutputArrayType* _arr) const noexcept;
void end_object(OutputObjectType* _obj) const noexcept {
cbor_encoder_close_container(_obj->parent_, _obj->encoder_);
}
void end_object(OutputObjectType* _obj) const noexcept;
private:
OutputArrayType new_array(const size_t _size,
CborEncoder* _parent) const noexcept {
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
cbor_encoder_create_array(_parent, subencoders_->back().get(), _size);
return OutputArrayType{subencoders_->back().get(), _parent};
}
CborEncoder* _parent) const noexcept;
OutputObjectType new_object(const size_t _size,
CborEncoder* _parent) const noexcept {
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
cbor_encoder_create_map(_parent, subencoders_->back().get(), _size);
return OutputObjectType{subencoders_->back().get(), _parent};
}
CborEncoder* _parent) const noexcept;
template <class T>
OutputVarType new_value(const T& _var, CborEncoder* _parent) const noexcept {
@@ -166,4 +130,4 @@ class Writer {
} // namespace cbor
} // namespace rfl
#endif // CBOR_PARSER_HPP_
#endif
+5 -32
View File
@@ -44,28 +44,12 @@ struct Reader {
static constexpr bool has_custom_constructor = has_from_flexbuf<T>::value;
rfl::Result<InputVarType> get_field_from_array(
const size_t _idx, const InputArrayType& _arr) const noexcept {
if (_idx >= _arr.size()) {
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
}
return _arr[_idx];
}
const size_t _idx, const InputArrayType& _arr) const noexcept;
rfl::Result<InputVarType> get_field_from_object(
const std::string& _name, const InputObjectType& _obj) const noexcept {
const auto keys = _obj.Keys();
for (size_t i = 0; i < keys.size(); ++i) {
if (_name == keys[i].AsString().c_str()) {
return _obj.Values()[i];
}
}
return rfl::Error("Map does not contain any element called '" + _name +
"'.");
}
const std::string& _name, const InputObjectType& _obj) const noexcept;
bool is_empty(const InputVarType& _var) const noexcept {
return _var.IsNull();
}
bool is_empty(const InputVarType& _var) const noexcept;
template <class T>
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
@@ -130,21 +114,10 @@ struct Reader {
return std::nullopt;
}
rfl::Result<InputArrayType> to_array(
const InputVarType& _var) const noexcept {
if (!_var.IsVector()) {
return rfl::Error("Could not cast to Vector.");
}
return _var.AsVector();
}
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept;
rfl::Result<InputObjectType> to_object(
const InputVarType& _var) const noexcept {
if (!_var.IsMap()) {
return rfl::Error("Could not cast to Map!");
}
return _var.AsMap();
}
const InputVarType& _var) const noexcept;
template <class T>
rfl::Result<T> use_custom_constructor(
+17 -54
View File
@@ -38,22 +38,15 @@ struct Writer {
using OutputObjectType = OutputObject;
using OutputVarType = OutputVar;
Writer(const Ref<flexbuffers::Builder>& _fbb) : fbb_(_fbb) {}
Writer(const Ref<flexbuffers::Builder>& _fbb);
~Writer() = default;
~Writer();
OutputArrayType array_as_root(const size_t _size) const noexcept {
return new_array();
}
OutputArrayType array_as_root(const size_t _size) const noexcept;
OutputObjectType object_as_root(const size_t _size) const noexcept {
return new_object();
}
OutputObjectType object_as_root(const size_t _size) const noexcept;
OutputVarType null_as_root() const noexcept {
fbb_->Null();
return OutputVarType{};
}
OutputVarType null_as_root() const noexcept;
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {
@@ -61,26 +54,18 @@ struct Writer {
}
OutputArrayType add_array_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept {
return new_array();
}
OutputArrayType* _parent) const noexcept;
OutputArrayType add_array_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
return new_array(_name);
}
OutputObjectType* _parent) const noexcept;
OutputObjectType add_object_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
return new_object();
}
const size_t _size, OutputArrayType* _parent) const noexcept;
OutputObjectType add_object_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
return new_object(_name);
}
OutputObjectType* _parent) const noexcept;
template <class T>
OutputVarType add_value_to_array(const T& _var,
@@ -95,24 +80,14 @@ struct Writer {
return insert_value(_name, _var);
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
fbb_->Null();
return OutputVarType{};
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
OutputVarType add_null_to_object(const std::string_view& _name,
OutputObjectType* _parent) const noexcept {
fbb_->Null(_name.data());
return OutputVarType{};
}
OutputObjectType* _parent) const noexcept;
void end_array(OutputArrayType* _arr) const noexcept {
fbb_->EndVector(_arr->start_, false, false);
}
void end_array(OutputArrayType* _arr) const noexcept;
void end_object(OutputObjectType* _obj) const noexcept {
fbb_->EndMap(_obj->start_);
}
void end_object(OutputObjectType* _obj) const noexcept;
private:
template <class T>
@@ -154,25 +129,13 @@ struct Writer {
return OutputVarType{};
}
OutputArrayType new_array(const std::string_view& _name) const noexcept {
const auto start = fbb_->StartVector(_name.data());
return OutputArrayType{start};
}
OutputArrayType new_array(const std::string_view& _name) const noexcept;
OutputArrayType new_array() const noexcept {
const auto start = fbb_->StartVector();
return OutputArrayType{start};
}
OutputArrayType new_array() const noexcept;
OutputObjectType new_object(const std::string_view& _name) const noexcept {
const auto start = fbb_->StartMap(_name.data());
return OutputObjectType{start};
}
OutputObjectType new_object(const std::string_view& _name) const noexcept;
OutputObjectType new_object() const noexcept {
const auto start = fbb_->StartMap();
return OutputObjectType{start};
}
OutputObjectType new_object() const noexcept;
private:
Ref<flexbuffers::Builder> fbb_;
+8 -33
View File
@@ -48,31 +48,16 @@ struct Reader {
using InputVarType = YYJSONInputVar;
template <class T>
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
T::from_json_obj(var);
});
static constexpr bool has_custom_constructor =
(requires(InputVarType var) { T::from_json_obj(var); });
rfl::Result<InputVarType> get_field_from_array(
const size_t _idx, const InputArrayType _arr) const noexcept {
const auto var = InputVarType(yyjson_arr_get(_arr.val_, _idx));
if (!var.val_) {
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
}
return var;
}
const size_t _idx, const InputArrayType _arr) const noexcept;
rfl::Result<InputVarType> get_field_from_object(
const std::string& _name, const InputObjectType _obj) const noexcept {
const auto var = InputVarType(yyjson_obj_get(_obj.val_, _name.c_str()));
if (!var.val_) {
return rfl::Error("Object contains no field named '" + _name + "'.");
}
return var;
}
const std::string& _name, const InputObjectType _obj) const noexcept;
bool is_empty(const InputVarType _var) const noexcept {
return !_var.val_ || yyjson_is_null(_var.val_);
}
bool is_empty(const InputVarType _var) const noexcept;
template <class ArrayReader>
std::optional<Error> read_array(const ArrayReader& _array_reader,
@@ -135,20 +120,10 @@ struct Reader {
}
}
rfl::Result<InputArrayType> to_array(const InputVarType _var) const noexcept {
if (!yyjson_is_arr(_var.val_)) {
return rfl::Error("Could not cast to array!");
}
return InputArrayType(_var.val_);
}
rfl::Result<InputArrayType> to_array(const InputVarType _var) const noexcept;
rfl::Result<InputObjectType> to_object(
const InputVarType _var) const noexcept {
if (!yyjson_is_obj(_var.val_)) {
return rfl::Error("Could not cast to object!");
}
return InputObjectType(_var.val_);
}
const InputVarType _var) const noexcept;
template <class T>
rfl::Result<T> use_custom_constructor(
@@ -164,4 +139,4 @@ struct Reader {
} // namespace json
} // namespace rfl
#endif // JSON_PARSER_HPP_
#endif
+16 -57
View File
@@ -48,27 +48,13 @@ class Writer {
using OutputObjectType = YYJSONOutputObject;
using OutputVarType = YYJSONOutputVar;
Writer(yyjson_mut_doc* _doc) : doc_(_doc) {}
Writer(yyjson_mut_doc* _doc);
~Writer() = default;
OutputArrayType array_as_root(const size_t) const noexcept;
OutputArrayType array_as_root(const size_t) const noexcept {
const auto arr = yyjson_mut_arr(doc_);
yyjson_mut_doc_set_root(doc_, arr);
return OutputArrayType(arr);
}
OutputObjectType object_as_root(const size_t) const noexcept;
OutputObjectType object_as_root(const size_t) const noexcept {
const auto obj = yyjson_mut_obj(doc_);
yyjson_mut_doc_set_root(doc_, obj);
return OutputObjectType(obj);
}
OutputVarType null_as_root() const noexcept {
const auto null = yyjson_mut_null(doc_);
yyjson_mut_doc_set_root(doc_, null);
return OutputVarType(null);
}
OutputVarType null_as_root() const noexcept;
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {
@@ -78,36 +64,18 @@ class Writer {
}
OutputArrayType add_array_to_array(const size_t,
OutputArrayType* _parent) const noexcept {
const auto arr = yyjson_mut_arr(doc_);
yyjson_mut_arr_add_val(_parent->val_, arr);
return OutputArrayType(arr);
}
OutputArrayType* _parent) const noexcept;
OutputArrayType add_array_to_object(
const std::string_view& _name, const size_t,
OutputObjectType* _parent) const noexcept {
const auto arr = yyjson_mut_arr(doc_);
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()),
arr);
return OutputArrayType(arr);
}
OutputArrayType add_array_to_object(const std::string_view& _name,
const size_t,
OutputObjectType* _parent) const noexcept;
OutputObjectType add_object_to_array(
const size_t, OutputArrayType* _parent) const noexcept {
const auto obj = yyjson_mut_obj(doc_);
yyjson_mut_arr_add_val(_parent->val_, obj);
return OutputObjectType(obj);
}
OutputObjectType add_object_to_array(const size_t,
OutputArrayType* _parent) const noexcept;
OutputObjectType add_object_to_object(
const std::string_view& _name, const size_t,
OutputObjectType* _parent) const noexcept {
const auto obj = yyjson_mut_obj(doc_);
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()),
obj);
return OutputObjectType(obj);
}
OutputObjectType* _parent) const noexcept;
template <class T>
OutputVarType add_value_to_array(const T& _var,
@@ -127,23 +95,14 @@ class Writer {
return OutputVarType(val);
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
const auto null = yyjson_mut_null(doc_);
yyjson_mut_arr_add_val(_parent->val_, null);
return OutputVarType(null);
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
OutputVarType add_null_to_object(const std::string_view& _name,
OutputObjectType* _parent) const noexcept {
const auto null = yyjson_mut_null(doc_);
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()),
null);
return OutputVarType(null);
}
OutputObjectType* _parent) const noexcept;
void end_array(OutputArrayType*) const noexcept {}
void end_array(OutputArrayType*) const noexcept;
void end_object(OutputObjectType*) const noexcept {}
void end_object(OutputObjectType*) const noexcept;
private:
template <class T>
@@ -170,4 +129,4 @@ class Writer {
} // namespace json
} // namespace rfl
#endif // JSON_PARSER_HPP_
#endif
+5 -16
View File
@@ -25,8 +25,6 @@
namespace rfl::json {
schema::Type type_to_json_schema_type(const parsing::schema::Type& _type);
template <class T>
struct TypeHelper {};
@@ -35,25 +33,16 @@ struct TypeHelper<rfl::Variant<Ts...>> {
using JSONSchemaType = rfl::Variant<schema::JSONSchema<Ts>...>;
};
std::string to_schema_internal_schema(
const parsing::schema::Definition& internal_schema,
const yyjson_write_flag);
/// Returns the JSON schema for a class.
template <class T, class... Ps>
std::string to_schema(const yyjson_write_flag _flag = 0) {
const auto internal_schema =
parsing::schema::make<Reader, Writer, T, Processors<Ps...>>();
auto definitions = std::map<std::string, schema::Type>();
for (const auto& [k, v] : internal_schema.definitions_) {
definitions[k] = type_to_json_schema_type(v);
}
using JSONSchemaType =
typename TypeHelper<schema::Type::ReflectionType>::JSONSchemaType;
const auto to_schema = [&](auto&& _root) -> JSONSchemaType {
using U = std::decay_t<decltype(_root)>;
return schema::JSONSchema<U>{.root = std::move(_root),
.definitions = definitions};
};
auto root = type_to_json_schema_type(internal_schema.root_);
const auto json_schema = rfl::visit(to_schema, std::move(root.value));
return write(json_schema, _flag);
return to_schema_internal_schema(internal_schema, _flag);
}
} // namespace rfl::json
+1 -1
View File
@@ -53,4 +53,4 @@ std::ostream& write(const auto& _obj, std::ostream& _stream,
} // namespace json
} // namespace rfl
#endif // JSON_PARSER_HPP_
#endif
+6 -37
View File
@@ -27,33 +27,12 @@ struct Reader {
});
rfl::Result<InputVarType> get_field_from_array(
const size_t _idx, const InputArrayType _arr) const noexcept {
if (_idx >= _arr.size) {
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
}
return _arr.ptr[_idx];
}
const size_t _idx, const InputArrayType _arr) const noexcept;
rfl::Result<InputVarType> get_field_from_object(
const std::string& _name, const InputObjectType& _obj) const noexcept {
for (uint32_t i = 0; i < _obj.size; ++i) {
const auto& key = _obj.ptr[i].key;
if (key.type != MSGPACK_OBJECT_STR) {
return Error("Key in element " + std::to_string(i) +
" was not a string.");
}
const auto current_name =
std::string_view(key.via.str.ptr, key.via.str.size);
if (_name == current_name) {
return _obj.ptr[i].val;
}
}
return Error("No field named '" + _name + "' was found.");
}
const std::string& _name, const InputObjectType& _obj) const noexcept ;
bool is_empty(const InputVarType& _var) const noexcept {
return _var.type == MSGPACK_OBJECT_NIL;
}
bool is_empty(const InputVarType& _var) const noexcept;
template <class T>
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
@@ -96,20 +75,10 @@ struct Reader {
}
rfl::Result<InputArrayType> to_array(
const InputVarType& _var) const noexcept {
if (_var.type != MSGPACK_OBJECT_ARRAY) {
return Error("Could not cast to an array.");
}
return _var.via.array;
}
const InputVarType& _var) const noexcept;
rfl::Result<InputObjectType> to_object(
const InputVarType& _var) const noexcept {
if (_var.type != MSGPACK_OBJECT_MAP) {
return Error("Could not cast to a map.");
}
return _var.via.map;
}
const InputVarType& _var) const noexcept;
template <class ArrayReader>
std::optional<Error> read_array(const ArrayReader& _array_reader,
@@ -153,4 +122,4 @@ struct Reader {
} // namespace msgpack
} // namespace rfl
#endif // JSON_PARSER_HPP_
#endif
+17 -52
View File
@@ -32,53 +32,32 @@ class Writer {
using OutputObjectType = MsgpackOutputObject;
using OutputVarType = MsgpackOutputVar;
Writer(msgpack_packer* _pk) : pk_(_pk) {}
Writer(msgpack_packer* _pk);
~Writer() = default;
~Writer();
OutputArrayType array_as_root(const size_t _size) const noexcept {
return new_array(_size);
}
OutputArrayType array_as_root(const size_t _size) const noexcept;
OutputObjectType object_as_root(const size_t _size) const noexcept {
return new_object(_size);
}
OutputObjectType object_as_root(const size_t _size) const noexcept;
OutputVarType null_as_root() const noexcept {
msgpack_pack_nil(pk_);
return OutputVarType{};
}
OutputVarType null_as_root() const noexcept;
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {
return new_value(_var);
}
OutputVarType value_as_root(const T& _var) const noexcept;
OutputArrayType add_array_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept {
return new_array(_size);
}
OutputArrayType* _parent) const noexcept;
OutputArrayType add_array_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
msgpack_pack_str(pk_, _name.size());
msgpack_pack_str_body(pk_, _name.data(), _name.size());
return new_array(_size);
}
OutputObjectType* _parent) const noexcept;
OutputObjectType add_object_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
return new_object(_size);
}
const size_t _size, OutputArrayType* _parent) const noexcept;
OutputObjectType add_object_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
msgpack_pack_str(pk_, _name.size());
msgpack_pack_str_body(pk_, _name.data(), _name.size());
return new_object(_size);
}
OutputObjectType* _parent) const noexcept;
template <class T>
OutputVarType add_value_to_array(const T& _var,
@@ -95,33 +74,19 @@ class Writer {
return new_value(_var);
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
msgpack_pack_nil(pk_);
return OutputVarType{};
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
OutputVarType add_null_to_object(const std::string_view& _name,
OutputObjectType* _parent) const noexcept {
msgpack_pack_str(pk_, _name.size());
msgpack_pack_str_body(pk_, _name.data(), _name.size());
msgpack_pack_nil(pk_);
return OutputVarType{};
}
OutputObjectType* _parent) const noexcept;
void end_array(OutputArrayType* _arr) const noexcept {}
void end_array(OutputArrayType* _arr) const noexcept;
void end_object(OutputObjectType* _obj) const noexcept {}
void end_object(OutputObjectType* _obj) const noexcept;
private:
OutputArrayType new_array(const size_t _size) const noexcept {
msgpack_pack_array(pk_, _size);
return OutputArrayType{};
}
OutputArrayType new_array(const size_t _size) const noexcept;
OutputObjectType new_object(const size_t _size) const noexcept {
msgpack_pack_map(pk_, _size);
return OutputObjectType{};
}
OutputObjectType new_object(const size_t _size) const noexcept;
template <class T>
OutputVarType new_value(const T& _var) const noexcept {
@@ -155,4 +120,4 @@ class Writer {
} // namespace rfl::msgpack
#endif // MSGPACK_PARSER_HPP_
#endif
@@ -30,9 +30,6 @@ struct FieldVariantParser {
using InputObjectType = typename R::InputObjectType;
using InputVarType = typename R::InputVarType;
using OutputObjectType = typename W::OutputObjectType;
using OutputVarType = typename W::OutputVarType;
static ResultType read(const R& _r, const InputVarType& _var) noexcept {
static_assert(
internal::no_duplicate_field_names<rfl::Tuple<FieldTypes...>>(),
-3
View File
@@ -25,9 +25,6 @@ struct MapParser {
using InputObjectType = typename R::InputObjectType;
using InputVarType = typename R::InputVarType;
using OutputObjectType = typename W::OutputObjectType;
using OutputVarType = typename W::OutputVarType;
using KeyType = std::remove_cvref_t<typename MapType::value_type::first_type>;
using ValueType =
std::remove_cvref_t<typename MapType::value_type::second_type>;
-1
View File
@@ -40,7 +40,6 @@ template <class R, class W, bool _ignore_empty_containers, bool _all_required,
requires AreReaderAndWriter<R, W, NamedTuple<FieldTypes...>>
struct NamedTupleParser {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
using ParentType = Parent<W>;
-3
View File
@@ -23,9 +23,6 @@ struct Parser<R, W, std::array<T, _size>, ProcessorsType> {
using InputArrayType = typename R::InputArrayType;
using InputVarType = typename R::InputVarType;
using OutputArrayType = typename W::OutputArrayType;
using OutputVarType = typename W::OutputVarType;
using ParentType = Parent<W>;
static Result<std::array<T, _size>> read(const R& _r,
-1
View File
@@ -16,7 +16,6 @@ template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, Box<T>>
struct Parser<R, W, Box<T>, ProcessorsType> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
static Result<Box<T>> read(const R& _r, const InputVarType& _var) noexcept {
const auto to_box = [](auto&& _t) { return Box<T>::make(std::move(_t)); };
-3
View File
@@ -23,9 +23,6 @@ struct Parser<R, W, T[_size], ProcessorsType> {
using InputArrayType = typename R::InputArrayType;
using InputVarType = typename R::InputVarType;
using OutputArrayType = typename W::OutputArrayType;
using OutputVarType = typename W::OutputVarType;
using ParentType = Parent<W>;
using CArray = T[_size];
-1
View File
@@ -36,7 +36,6 @@ requires AreReaderAndWriter<R, W, T>
struct Parser {
public:
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
using ParentType = Parent<W>;
-1
View File
@@ -15,7 +15,6 @@ template <class R, class W, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::filesystem::path>
struct Parser<R, W, std::filesystem::path, ProcessorsType> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
/// Expresses the variables as type T.
static Result<std::filesystem::path> read(const R& _r,
-1
View File
@@ -19,7 +19,6 @@ template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::optional<T>>
struct Parser<R, W, std::optional<T>, ProcessorsType> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
using ParentType = Parent<W>;
-1
View File
@@ -19,7 +19,6 @@ template <class R, class W, class FirstType, class SecondType,
requires AreReaderAndWriter<R, W, std::pair<FirstType, SecondType>>
struct Parser<R, W, std::pair<FirstType, SecondType>, ProcessorsType> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
/// Expresses the variables as type T.
static Result<std::pair<FirstType, SecondType>> read(
-1
View File
@@ -18,7 +18,6 @@ template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, T*>
struct Parser<R, W, T*, ProcessorsType> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
using ParentType = Parent<W>;
-1
View File
@@ -17,7 +17,6 @@ template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, Ref<T>>
struct Parser<R, W, Ref<T>, ProcessorsType> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
static Result<Ref<T>> read(const R& _r, const InputVarType& _var) noexcept {
const auto to_ref = [&](auto&& _t) { return Ref<T>::make(std::move(_t)); };
@@ -17,7 +17,6 @@ template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::reference_wrapper<T>>
struct Parser<R, W, std::reference_wrapper<T>, ProcessorsType> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
static Result<std::reference_wrapper<T>> read(
const R&, const InputVarType&) noexcept {
-1
View File
@@ -19,7 +19,6 @@ template <class R, class W, class T, internal::StringLiteral _name,
requires AreReaderAndWriter<R, W, Rename<_name, T>>
struct Parser<R, W, Rename<_name, T>, ProcessorsType> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
static Result<Rename<_name, T>> read(const R& _r,
const InputVarType& _var) noexcept {
-1
View File
@@ -17,7 +17,6 @@ template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, Result<T>>
struct Parser<R, W, Result<T>, ProcessorsType> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
using ErrorType = NamedTuple<Field<"error", std::string>>;
using VariantType = std::variant<std::remove_cvref_t<T>, ErrorType>;
-3
View File
@@ -22,9 +22,6 @@ struct Parser<R, W, internal::Array<T>, ProcessorsType> {
using InputArrayType = typename R::InputArrayType;
using InputVarType = typename R::InputVarType;
using OutputArrayType = typename W::OutputArrayType;
using OutputVarType = typename W::OutputVarType;
using StdArray = internal::to_std_array_t<T>;
static Result<internal::Array<T>> read(const R& _r,
@@ -20,7 +20,6 @@ requires AreReaderAndWriter<R, W, rfl::Variant<AlternativeTypes...>>
class Parser<R, W, rfl::Variant<AlternativeTypes...>, ProcessorsType> {
public:
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
static Result<rfl::Variant<AlternativeTypes...>> read(
const R& _r, const InputVarType& _var) noexcept {
@@ -18,7 +18,6 @@ template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::shared_ptr<T>>
struct Parser<R, W, std::shared_ptr<T>, ProcessorsType> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
using ParentType = Parent<W>;
-1
View File
@@ -21,7 +21,6 @@ struct Parser<R, W,
internal::Skip<T, _skip_serialization, _skip_deserialization>,
ProcessorsType> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
static Result<internal::Skip<T, _skip_serialization, _skip_deserialization>>
read(const R& _r, const InputVarType& _var) noexcept {
@@ -18,7 +18,6 @@ template <class R, class W, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::string_view>
struct Parser<R, W, std::string_view, ProcessorsType> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
static Result<std::string_view> read(const R&,
const InputVarType&) noexcept {
@@ -30,9 +30,6 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>,
using InputObjectType = typename R::InputObjectType;
using InputVarType = typename R::InputVarType;
using OutputObjectType = typename W::OutputObjectType;
using OutputVarType = typename W::OutputVarType;
constexpr static bool no_field_names_ = ProcessorsType::no_field_names_;
using InputObjectOrArrayType =
@@ -19,7 +19,6 @@ template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::unique_ptr<T>>
struct Parser<R, W, std::unique_ptr<T>, ProcessorsType> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
using ParentType = Parent<W>;
-1
View File
@@ -28,7 +28,6 @@ class Parser<R, W, std::variant<AlternativeTypes...>, ProcessorsType> {
public:
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
static Result<std::variant<AlternativeTypes...>> read(
const R& _r, const InputVarType& _var) noexcept {
-1
View File
@@ -18,7 +18,6 @@ requires AreReaderAndWriter<R, W, std::wstring>
struct Parser<R, W, std::wstring, ProcessorsType> {
public:
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
using ParentType = Parent<W>;
-3
View File
@@ -24,9 +24,6 @@ struct TupleParser {
using InputArrayType = typename R::InputArrayType;
using InputVarType = typename R::InputVarType;
using OutputArrayType = typename W::OutputArrayType;
using OutputVarType = typename W::OutputVarType;
using ParentType = Parent<W>;
static Result<TupleType> read(const R& _r,
-3
View File
@@ -33,9 +33,6 @@ struct VectorParser {
using InputArrayType = typename R::InputArrayType;
using InputVarType = typename R::InputVarType;
using OutputArrayType = typename W::OutputArrayType;
using OutputVarType = typename W::OutputVarType;
using ParentType = Parent<W>;
using T = typename VecType::value_type;
+1
View File
@@ -10,6 +10,7 @@
#include "../Result.hpp"
#include "../Tuple.hpp"
#include "../internal/is_array.hpp"
#include "Parser_base.hpp"
namespace rfl::parsing {
+7 -34
View File
@@ -25,30 +25,16 @@ struct Reader {
using InputVarType = ::toml::node*;
template <class T>
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
T::from_toml_obj(var);
});
static constexpr bool has_custom_constructor =
(requires(InputVarType var) { T::from_toml_obj(var); });
rfl::Result<InputVarType> get_field_from_array(
const size_t _idx, const InputArrayType _arr) const noexcept {
if (_idx >= _arr->size()) {
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
}
return _arr->get(_idx);
}
const size_t _idx, const InputArrayType _arr) const noexcept;
rfl::Result<InputVarType> get_field_from_object(
const std::string& _name, const InputObjectType& _obj) const noexcept {
auto var = (*_obj)[_name];
if (!var) {
return rfl::Error("Object contains no field named '" + _name + "'.");
}
return var.node();
}
const std::string& _name, const InputObjectType& _obj) const noexcept;
bool is_empty(const InputVarType& _var) const noexcept {
return !_var && true;
}
bool is_empty(const InputVarType& _var) const noexcept;
template <class T>
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
@@ -81,14 +67,7 @@ struct Reader {
}
}
rfl::Result<InputArrayType> to_array(
const InputVarType& _var) const noexcept {
const auto ptr = _var->as_array();
if (!ptr) {
return rfl::Error("Could not cast to an array!");
}
return ptr;
}
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept;
template <class ArrayReader>
std::optional<Error> read_array(const ArrayReader& _array_reader,
@@ -112,13 +91,7 @@ struct Reader {
}
rfl::Result<InputObjectType> to_object(
const InputVarType& _var) const noexcept {
const auto ptr = _var->as_table();
if (!ptr) {
return rfl::Error("Could not cast to a table!");
}
return ptr;
}
const InputVarType& _var) const noexcept;
template <class T>
rfl::Result<T> use_custom_constructor(
+17 -43
View File
@@ -33,22 +33,16 @@ class Writer {
using OutputObjectType = TOMLObject;
using OutputVarType = TOMLVar;
Writer(::toml::table* _root) : root_(_root) {}
Writer(::toml::table* _root);
~Writer() = default;
~Writer();
template <class T>
OutputArrayType array_as_root(const T _size) const noexcept {
static_assert(rfl::always_false_v<T>,
"TOML only allows tables as the root element.");
return OutputArrayType{nullptr};
}
OutputArrayType array_as_root(const T _size) const noexcept;
OutputObjectType object_as_root(const size_t _size) const noexcept {
return OutputObjectType{root_};
}
OutputObjectType object_as_root(const size_t _size) const noexcept;
OutputVarType null_as_root() const noexcept { return OutputVarType{}; }
OutputVarType null_as_root() const noexcept;
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {
@@ -58,32 +52,18 @@ class Writer {
}
OutputArrayType 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()};
}
OutputArrayType* _parent) const noexcept;
OutputArrayType 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()};
}
OutputArrayType add_array_to_object(const std::string_view& _name,
const size_t _size,
OutputObjectType* _parent) const noexcept;
OutputObjectType 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()};
}
OutputObjectType add_object_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept;
OutputObjectType 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()};
}
OutputObjectType* _parent) const noexcept;
template <class T>
OutputVarType add_value_to_array(const T& _var,
@@ -100,20 +80,14 @@ class Writer {
return OutputVarType{};
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
_parent->val_->push_back(std::string(""));
return OutputVarType{};
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
OutputVarType add_null_to_object(const std::string_view& _name,
OutputObjectType* _parent) const noexcept {
_parent->val_->emplace(_name, ::toml::value(std::string("")));
return OutputVarType{};
}
OutputObjectType* _parent) const noexcept;
void end_array(OutputArrayType* _arr) const noexcept {}
void end_array(OutputArrayType* _arr) const noexcept;
void end_object(OutputObjectType* _obj) const noexcept {}
void end_object(OutputObjectType* _obj) const noexcept;
private:
::toml::table* root_;
@@ -121,4 +95,4 @@ class Writer {
} // namespace rfl::toml
#endif // JSON_PARSER_HPP_
#endif
+5 -54
View File
@@ -1,20 +1,13 @@
#ifndef RFL_XML_READER_HPP_
#define RFL_XML_READER_HPP_
#include <array>
#include <exception>
#include <map>
#include <memory>
#include <optional>
#include <pugixml.hpp>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include <variant>
#include <vector>
#include "../Result.hpp"
#include "../always_false.hpp"
@@ -49,49 +42,13 @@ struct Reader {
template <class T>
static constexpr bool has_custom_constructor = false;
/// XML-only helper function. This is needed because XML distinguishes between
/// nodes and attributes.
static rfl::Result<pugi::xml_node> cast_as_node(
const std::variant<pugi::xml_node, pugi::xml_attribute>&
_node_or_attribute) {
const auto cast = [](const auto& _n) -> Result<pugi::xml_node> {
using Type = std::remove_cvref_t<decltype(_n)>;
if constexpr (std::is_same<Type, pugi::xml_node>()) {
return _n;
} else {
return Error("Field '" + std::string(_n.name()) + "' is an attribute.");
}
};
return std::visit(cast, _node_or_attribute);
}
rfl::Result<InputVarType> get_field_from_array(
const size_t _idx, const InputArrayType& _arr) const noexcept {
const auto name = _arr.node_.name();
size_t i = 0;
for (auto node = _arr.node_; node; node = node.next_sibling(name)) {
if (i == _idx) {
return InputVarType(node);
}
}
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
}
const size_t _idx, const InputArrayType& _arr) const noexcept;
rfl::Result<InputVarType> get_field_from_object(
const std::string& _name, const InputObjectType _obj) const noexcept {
const auto node = _obj.node_.child(_name.c_str());
if (!node) {
return rfl::Error("Object contains no field named '" + _name + "'.");
}
return InputVarType(node);
}
const std::string& _name, const InputObjectType _obj) const noexcept;
bool is_empty(const InputVarType _var) const noexcept {
const auto wrap = [](const auto& _node) { return !_node; };
return std::visit(cast_as_node, _var.node_or_attribute_)
.transform(wrap)
.value_or(false);
}
bool is_empty(const InputVarType _var) const noexcept;
template <class T>
rfl::Result<T> to_basic_type(const InputVarType _var) const noexcept {
@@ -128,10 +85,7 @@ struct Reader {
}
}
rfl::Result<InputArrayType> to_array(const InputVarType _var) const noexcept {
const auto wrap = [](const auto& _node) { return InputArrayType(_node); };
return std::visit(cast_as_node, _var.node_or_attribute_).transform(wrap);
}
rfl::Result<InputArrayType> to_array(const InputVarType _var) const noexcept;
template <class ArrayReader>
std::optional<Error> read_array(const ArrayReader& _array_reader,
@@ -168,10 +122,7 @@ struct Reader {
}
rfl::Result<InputObjectType> to_object(
const InputVarType _var) const noexcept {
const auto wrap = [](const auto& _node) { return InputObjectType(_node); };
return std::visit(cast_as_node, _var.node_or_attribute_).transform(wrap);
}
const InputVarType _var) const noexcept;
template <class T>
rfl::Result<T> use_custom_constructor(
+30 -85
View File
@@ -1,26 +1,17 @@
#ifndef RFL_XML_WRITER_HPP_
#define RFL_XML_WRITER_HPP_
#include <exception>
#include <map>
#include <pugixml.hpp>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
namespace rfl {
namespace xml {
struct Writer {
static constexpr const char* XML_CONTENT = "xml_content";
struct XMLOutputArray {
XMLOutputArray(const std::string_view& _name,
const Ref<pugi::xml_node>& _node)
@@ -43,72 +34,41 @@ struct Writer {
using OutputObjectType = XMLOutputObject;
using OutputVarType = XMLOutputVar;
Writer(const Ref<pugi::xml_node>& _root, const std::string& _root_name)
: root_(_root), root_name_(_root_name) {}
Writer(const Ref<pugi::xml_node>& _root, const std::string& _root_name);
~Writer() = default;
~Writer();
OutputArrayType array_as_root(const size_t _size) const noexcept {
auto node_child =
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
return OutputArrayType(root_name_, node_child);
}
OutputArrayType array_as_root(const size_t _size) const noexcept;
OutputObjectType object_as_root(const size_t _size) const noexcept {
auto node_child =
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
return OutputObjectType(node_child);
}
OutputObjectType object_as_root(const size_t _size) const noexcept;
OutputVarType null_as_root() const noexcept {
auto node_child =
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
return OutputVarType(node_child);
}
OutputVarType null_as_root() const noexcept;
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {
const auto str = to_string(_var);
auto node_child =
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
node_child->append_child(pugi::node_pcdata).set_value(str.c_str());
return OutputVarType(node_child);
return value_as_root_impl(str);
}
OutputArrayType add_array_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept {
return *_parent;
}
OutputArrayType* _parent) const noexcept;
OutputArrayType add_array_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
return OutputArrayType(_name, _parent->node_);
}
OutputArrayType add_array_to_object(const std::string_view& _name,
const size_t _size,
OutputObjectType* _parent) const noexcept;
OutputObjectType add_object_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
auto node_child = Ref<pugi::xml_node>::make(
_parent->node_->append_child(_parent->name_.data()));
return OutputObjectType(node_child);
}
OutputObjectType add_object_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept;
OutputObjectType add_object_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
auto node_child =
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.data()));
return OutputObjectType(node_child);
}
OutputObjectType* _parent) const noexcept;
template <class T>
OutputVarType add_value_to_array(const T& _var,
OutputArrayType* _parent) const noexcept {
const auto str = to_string(_var);
auto node_child = Ref<pugi::xml_node>::make(
_parent->node_->append_child(_parent->name_.data()));
node_child->append_child(pugi::node_pcdata).set_value(str.c_str());
return OutputVarType(node_child);
return add_value_to_array_impl(str, _parent);
}
template <class T>
@@ -116,43 +76,18 @@ struct Writer {
const std::string_view& _name, const T& _var, OutputObjectType* _parent,
const bool _is_attribute = false) const noexcept {
const auto str = to_string(_var);
if (_is_attribute) {
_parent->node_->append_attribute(_name.data()) = str.c_str();
return OutputVarType(_parent->node_);
} else if (_name == XML_CONTENT) {
_parent->node_->append_child(pugi::node_pcdata).set_value(str.c_str());
return OutputVarType(_parent->node_);
} else {
auto node_child =
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.data()));
node_child->append_child(pugi::node_pcdata).set_value(str.c_str());
return OutputVarType(node_child);
}
return add_value_to_object_impl(_name, str, _parent, _is_attribute);
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
auto node_child = Ref<pugi::xml_node>::make(
_parent->node_->append_child(_parent->name_.data()));
return OutputVarType(node_child);
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
OutputVarType add_null_to_object(
const std::string_view& _name, OutputObjectType* _parent,
const bool _is_attribute = false) const noexcept {
if (_is_attribute) {
return OutputVarType(_parent->node_);
} else if (_name == XML_CONTENT) {
return OutputVarType(_parent->node_);
} else {
auto node_child =
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.data()));
return OutputVarType(node_child);
}
}
const bool _is_attribute = false) const noexcept;
void end_array(OutputArrayType* _arr) const noexcept {}
void end_array(OutputArrayType* _arr) const noexcept;
void end_object(OutputObjectType* _obj) const noexcept {}
void end_object(OutputObjectType* _obj) const noexcept;
private:
template <class T>
@@ -169,6 +104,16 @@ struct Writer {
}
}
OutputVarType value_as_root_impl(const std::string& _str) const noexcept;
OutputVarType add_value_to_array_impl(
const std::string& _str, OutputArrayType* _parent) const noexcept;
OutputVarType add_value_to_object_impl(
const std::string_view& _name, const std::string& _str,
OutputObjectType* _parent,
const bool _is_attribute = false) const noexcept;
public:
Ref<pugi::xml_node> root_;
@@ -178,4 +123,4 @@ struct Writer {
} // namespace xml
} // namespace rfl
#endif // XML_PARSER_HPP_
#endif // RFL_XML_WRITER_HPP_
+1 -1
View File
@@ -75,4 +75,4 @@ std::string write(const auto& _obj, const std::string& _indent = " ") {
} // namespace xml
} // namespace rfl
#endif // XML_PARSER_HPP_
#endif
+7 -32
View File
@@ -45,30 +45,16 @@ struct Reader {
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);
});
static constexpr bool has_custom_constructor =
(requires(InputVarType var) { T::from_yaml_obj(var); });
rfl::Result<InputVarType> get_field_from_array(
const size_t _idx, const InputArrayType& _arr) const noexcept {
if (_idx >= _arr.node_.size()) {
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
}
return InputVarType(_arr.node_[_idx]);
}
const size_t _idx, const InputArrayType& _arr) const noexcept;
rfl::Result<InputVarType> get_field_from_object(
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;
}
const std::string& _name, const InputObjectType& _obj) const noexcept;
bool is_empty(const InputVarType& _var) const noexcept {
return !_var.node_ && true;
}
bool is_empty(const InputVarType& _var) const noexcept;
template <class T>
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
@@ -86,13 +72,7 @@ struct Reader {
}
}
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_);
}
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept;
template <class ArrayReader>
std::optional<Error> read_array(const ArrayReader& _array_reader,
@@ -121,12 +101,7 @@ struct Reader {
}
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_);
}
const InputVarType& _var) const noexcept;
template <class T>
rfl::Result<T> use_custom_constructor(
+21 -55
View File
@@ -31,21 +31,15 @@ class Writer {
using OutputObjectType = YAMLObject;
using OutputVarType = YAMLVar;
Writer(const Ref<YAML::Emitter>& _out) : out_(_out) {}
Writer(const Ref<YAML::Emitter>& _out);
~Writer() = default;
~Writer();
OutputArrayType array_as_root(const size_t _size) const noexcept {
return new_array();
}
OutputArrayType array_as_root(const size_t _size) const noexcept;
OutputObjectType object_as_root(const size_t _size) const noexcept {
return new_object();
}
OutputObjectType object_as_root(const size_t _size) const noexcept;
OutputVarType null_as_root() const noexcept {
return insert_value(YAML::Null);
}
OutputVarType null_as_root() const noexcept;
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {
@@ -53,26 +47,18 @@ class Writer {
}
OutputArrayType add_array_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept {
return new_array();
}
OutputArrayType* _parent) const noexcept;
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;
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;
OutputObjectType add_object_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
return new_object(_name);
}
OutputObjectType* _parent) const noexcept;
template <class T>
OutputVarType add_value_to_array(const T& _var,
@@ -87,22 +73,14 @@ class Writer {
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;
OutputVarType add_null_to_object(const std::string_view& _name,
OutputObjectType* _parent) const noexcept {
return insert_value(_name, YAML::Null);
}
OutputObjectType* _parent) const noexcept;
void end_array(OutputArrayType* _arr) const noexcept {
(*out_) << YAML::EndSeq;
}
void end_array(OutputArrayType* _arr) const noexcept;
void end_object(OutputObjectType* _obj) const noexcept {
(*out_) << YAML::EndMap;
}
void end_object(OutputObjectType* _obj) const noexcept;
private:
template <class T>
@@ -118,25 +96,13 @@ class Writer {
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;
OutputArrayType new_array() const noexcept {
(*out_) << YAML::BeginSeq;
return OutputArrayType{};
}
OutputArrayType new_array() const noexcept;
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;
OutputObjectType new_object() const noexcept {
(*out_) << YAML::BeginMap;
return OutputObjectType{};
}
OutputObjectType new_object() const noexcept;
public:
const Ref<YAML::Emitter> out_;
@@ -145,4 +111,4 @@ class Writer {
} // namespace yaml
} // namespace rfl
#endif // JSON_PARSER_HPP_
#endif