This commit is contained in:
Mars 2024-06-09 18:55:00 -04:00
parent 05451841e8
commit 94d08a02d6
Signed by: pupbrained
GPG key ID: 874E22DF2F9DFCB5
60 changed files with 610 additions and 299 deletions

View file

@ -6,7 +6,6 @@ AllowShortBlocksOnASingleLine: Always
AllowShortCompoundRequirementOnASingleLine: true AllowShortCompoundRequirementOnASingleLine: true
AllowShortEnumsOnASingleLine: true AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: true AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true AllowShortLoopsOnASingleLine: true
BasedOnStyle: Chromium BasedOnStyle: Chromium
BinPackArguments: false BinPackArguments: false

View file

@ -15,7 +15,6 @@ Checks: >
-llvm-include-order, -llvm-include-order,
-llvmlibc-*, -llvmlibc-*,
-misc-non-private-member-variables-in-classes, -misc-non-private-member-variables-in-classes,
-modernize-use-trailing-return-type,
-readability-braces-around-statements, -readability-braces-around-statements,
-readability-implicit-bool-conversion, -readability-implicit-bool-conversion,
-readability-isolate-declaration, -readability-isolate-declaration,

View file

@ -25,7 +25,9 @@ namespace rfl {
/// You can generate them from unique_ptrs as well, in which case it will /// You can generate them from unique_ptrs as well, in which case it will
/// return an Error, if the unique_ptr is not set. /// return an Error, if the unique_ptr is not set.
static Result<Box<T>> make(std::unique_ptr<T>&& _ptr) { static Result<Box<T>> make(std::unique_ptr<T>&& _ptr) {
if (!_ptr) { return Error("std::unique_ptr was a nullptr."); } if (!_ptr) {
return Error("std::unique_ptr was a nullptr.");
}
return Box<T>(std::move(_ptr)); return Box<T>(std::move(_ptr));
} }

View file

@ -297,7 +297,9 @@ namespace rfl {
template <int _i = 0> template <int _i = 0>
static Result<int> find_value(const std::string& _str) { static Result<int> find_value(const std::string& _str) {
using FieldType = typename std::tuple_element<_i, FieldsType>::type; using FieldType = typename std::tuple_element<_i, FieldsType>::type;
if (FieldType::field_.str() == _str) { return _i; } if (FieldType::field_.str() == _str) {
return _i;
}
if constexpr (_i + 1 == num_fields_) { if constexpr (_i + 1 == num_fields_) {
return Error( return Error(
"Literal does not support string '" + _str + "Literal does not support string '" + _str +

View file

@ -487,13 +487,16 @@ namespace rfl {
if constexpr (size == _index) { if constexpr (size == _index) {
return make_replaced<_index, V, T>( return make_replaced<_index, V, T>(
std::forward<V>(_values), std::forward<T>(_val), std::forward<V>(_values),
std::forward<Args>(_args)..., FieldType(std::forward<T>(_val)) std::forward<T>(_val),
std::forward<Args>(_args)...,
FieldType(std::forward<T>(_val))
); );
} else { } else {
using U = typename FieldType::Type; using U = typename FieldType::Type;
return make_replaced<_index, V, T>( return make_replaced<_index, V, T>(
std::forward<V>(_values), std::forward<T>(_val), std::forward<V>(_values),
std::forward<T>(_val),
std::forward<Args>(_args)..., std::forward<Args>(_args)...,
FieldType(std::forward<U>(std::get<size>(_values))) FieldType(std::forward<U>(std::get<size>(_values)))
); );

View file

@ -52,7 +52,9 @@ namespace rfl {
_r.or_else(push_back); _r.or_else(push_back);
if constexpr (sizeof...(Tail) == 0) { if constexpr (sizeof...(Tail) == 0) {
if (_errors.size() == sizeof...(Cs)) { return _value; } if (_errors.size() == sizeof...(Cs)) {
return _value;
}
return make_error_message(_errors); return make_error_message(_errors);
} else { } else {
return validate_impl<T, Tail...>( return validate_impl<T, Tail...>(

View file

@ -25,14 +25,18 @@ namespace rfl {
/// You can generate them from shared_ptrs as well, in which case it will /// You can generate them from shared_ptrs as well, in which case it will
/// return an Error, if the shared_ptr is not set. /// return an Error, if the shared_ptr is not set.
static Result<Ref<T>> make(std::shared_ptr<T>&& _ptr) { static Result<Ref<T>> make(std::shared_ptr<T>&& _ptr) {
if (!_ptr) { return Error("std::shared_ptr was a nullptr."); } if (!_ptr) {
return Error("std::shared_ptr was a nullptr.");
}
return Ref<T>(std::move(_ptr)); return Ref<T>(std::move(_ptr));
} }
/// You can generate them from shared_ptrs as well, in which case it will /// You can generate them from shared_ptrs as well, in which case it will
/// return an Error, if the shared_ptr is not set. /// return an Error, if the shared_ptr is not set.
static Result<Ref<T>> make(const std::shared_ptr<T>& _ptr) { static Result<Ref<T>> make(const std::shared_ptr<T>& _ptr) {
if (!_ptr) { return Error("std::shared_ptr was a nullptr."); } if (!_ptr) {
return Error("std::shared_ptr was a nullptr.");
}
return Ref<T>(_ptr); return Ref<T>(_ptr);
} }

View file

@ -188,7 +188,9 @@ namespace rfl {
/// Assigns the underlying object. /// Assigns the underlying object.
Result<T>& operator=(const Result<T>& _other) { Result<T>& operator=(const Result<T>& _other) {
if (this == &_other) { return *this; } if (this == &_other) {
return *this;
}
destroy(); destroy();
success_ = _other.success_; success_ = _other.success_;
copy_from_other(_other); copy_from_other(_other);
@ -197,7 +199,9 @@ namespace rfl {
/// Assigns the underlying object. /// Assigns the underlying object.
Result<T>& operator=(Result<T>&& _other) noexcept { Result<T>& operator=(Result<T>&& _other) noexcept {
if (this == &_other) { return *this; } if (this == &_other) {
return *this;
}
destroy(); destroy();
success_ = _other.success_; success_ = _other.success_;
move_from_other(_other); move_from_other(_other);

View file

@ -78,7 +78,9 @@ namespace rfl {
std::istringstream input(_s); std::istringstream input(_s);
input.imbue(std::locale(setlocale(LC_ALL, nullptr))); input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
input >> std::get_time(_tm, _f); input >> std::get_time(_tm, _f);
if (input.fail()) { return NULL; } if (input.fail()) {
return NULL;
}
return (char*)(_s + input.tellg()); return (char*)(_s + input.tellg());
} }
#endif #endif

View file

@ -60,7 +60,9 @@ namespace rfl {
if (bson_iter_init(&iter, &b)) { if (bson_iter_init(&iter, &b)) {
while (bson_iter_next(&iter)) { while (bson_iter_next(&iter)) {
auto key = std::string(bson_iter_key(&iter)); auto key = std::string(bson_iter_key(&iter));
if (key == _name) { return to_input_var(&iter); } if (key == _name) {
return to_input_var(&iter);
}
} }
} }
} }
@ -146,7 +148,9 @@ namespace rfl {
if (bson_iter_init(&iter, &b)) { if (bson_iter_init(&iter, &b)) {
while (bson_iter_next(&iter)) { while (bson_iter_next(&iter)) {
const auto err = _array_reader.read(to_input_var(&iter)); const auto err = _array_reader.read(to_input_var(&iter));
if (err) { return err; } if (err) {
return err;
}
} }
} }
} }

View file

@ -127,7 +127,9 @@ namespace rfl {
) const noexcept { ) const noexcept {
subdocs_->emplace_back(rfl::Box<BSONType>()); subdocs_->emplace_back(rfl::Box<BSONType>());
bson_append_document_begin( bson_append_document_begin(
_parent->val_, _name.data(), static_cast<int>(_name.size()), _parent->val_,
_name.data(),
static_cast<int>(_name.size()),
&(subdocs_->back()->val_) &(subdocs_->back()->val_)
); );
return OutputObjectType( return OutputObjectType(
@ -169,8 +171,11 @@ namespace rfl {
) const noexcept { ) const noexcept {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) { if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
bson_append_utf8( bson_append_utf8(
_parent->val_, _name.data(), static_cast<int>(_name.size()), _parent->val_,
_var.c_str(), static_cast<int>(_var.size()) _name.data(),
static_cast<int>(_name.size()),
_var.c_str(),
static_cast<int>(_var.size())
); );
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) { } else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
bson_append_bool( bson_append_bool(
@ -178,12 +183,16 @@ namespace rfl {
); );
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) { } else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
bson_append_double( bson_append_double(
_parent->val_, _name.data(), static_cast<int>(_name.size()), _parent->val_,
_name.data(),
static_cast<int>(_name.size()),
static_cast<double>(_var) static_cast<double>(_var)
); );
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) { } else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
bson_append_int64( bson_append_int64(
_parent->val_, _name.data(), static_cast<int>(_name.size()), _parent->val_,
_name.data(),
static_cast<int>(_name.size()),
static_cast<std::int64_t>(_var) static_cast<std::int64_t>(_var)
); );
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>( } else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>(

View file

@ -52,21 +52,33 @@ namespace rfl {
CborValue val; CborValue val;
auto buffer = std::vector<char>(); auto buffer = std::vector<char>();
auto err = cbor_value_enter_container(_obj.val_, &val); auto err = cbor_value_enter_container(_obj.val_, &val);
if (err != CborNoError) { return Error(cbor_error_string(err)); } if (err != CborNoError) {
return Error(cbor_error_string(err));
}
size_t length = 0; size_t length = 0;
err = cbor_value_get_map_length(_obj.val_, &length); err = cbor_value_get_map_length(_obj.val_, &length);
if (err != CborNoError) { return Error(cbor_error_string(err)); } if (err != CborNoError) {
return Error(cbor_error_string(err));
}
for (size_t i = 0; i < length; ++i) { for (size_t i = 0; i < length; ++i) {
if (!cbor_value_is_text_string(&val)) { if (!cbor_value_is_text_string(&val)) {
return Error("Expected the key to be a string value."); return Error("Expected the key to be a string value.");
} }
err = get_string(&val, &buffer); err = get_string(&val, &buffer);
if (err != CborNoError) { return Error(cbor_error_string(err)); } if (err != CborNoError) {
return Error(cbor_error_string(err));
}
err = cbor_value_advance(&val); err = cbor_value_advance(&val);
if (err != CborNoError) { return Error(cbor_error_string(err)); } if (err != CborNoError) {
if (_name == buffer.data()) { return to_input_var(&val); } return Error(cbor_error_string(err));
}
if (_name == buffer.data()) {
return to_input_var(&val);
}
err = cbor_value_advance(&val); err = cbor_value_advance(&val);
if (err != CborNoError) { return Error(cbor_error_string(err)); } if (err != CborNoError) {
return Error(cbor_error_string(err));
}
} }
return Error("No field named '" + _name + "' was found."); return Error("No field named '" + _name + "' was found.");
} }
@ -83,7 +95,9 @@ namespace rfl {
} }
std::vector<char> buffer; std::vector<char> buffer;
const auto err = get_string(_var.val_, &buffer); const auto err = get_string(_var.val_, &buffer);
if (err != CborNoError) { return Error(cbor_error_string(err)); } if (err != CborNoError) {
return Error(cbor_error_string(err));
}
return std::string(buffer.data()); return std::string(buffer.data());
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) { } else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!cbor_value_is_boolean(_var.val_)) { if (!cbor_value_is_boolean(_var.val_)) {
@ -91,24 +105,32 @@ namespace rfl {
} }
bool result = false; bool result = false;
const auto err = cbor_value_get_boolean(_var.val_, &result); const auto err = cbor_value_get_boolean(_var.val_, &result);
if (err != CborNoError) { return Error(cbor_error_string(err)); } if (err != CborNoError) {
return Error(cbor_error_string(err));
}
return result; return result;
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() || } else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_integral<std::remove_cvref_t<T>>()) { std::is_integral<std::remove_cvref_t<T>>()) {
if (cbor_value_is_integer(_var.val_)) { if (cbor_value_is_integer(_var.val_)) {
std::int64_t result = 0; std::int64_t result = 0;
const auto err = cbor_value_get_int64(_var.val_, &result); const auto err = cbor_value_get_int64(_var.val_, &result);
if (err != CborNoError) { return Error(cbor_error_string(err)); } if (err != CborNoError) {
return Error(cbor_error_string(err));
}
return static_cast<T>(result); return static_cast<T>(result);
} else if (cbor_value_is_float(_var.val_)) { } else if (cbor_value_is_float(_var.val_)) {
float result = 0.0; float result = 0.0;
const auto err = cbor_value_get_float(_var.val_, &result); const auto err = cbor_value_get_float(_var.val_, &result);
if (err != CborNoError) { return Error(cbor_error_string(err)); } if (err != CborNoError) {
return Error(cbor_error_string(err));
}
return static_cast<T>(result); return static_cast<T>(result);
} else if (cbor_value_is_double(_var.val_)) { } else if (cbor_value_is_double(_var.val_)) {
double result = 0.0; double result = 0.0;
const auto err = cbor_value_get_double(_var.val_, &result); const auto err = cbor_value_get_double(_var.val_, &result);
if (err != CborNoError) { return Error(cbor_error_string(err)); } if (err != CborNoError) {
return Error(cbor_error_string(err));
}
return static_cast<T>(result); return static_cast<T>(result);
} }
return rfl::Error( return rfl::Error(
@ -156,7 +178,9 @@ namespace rfl {
} }
for (size_t i = 0; i < length; ++i) { for (size_t i = 0; i < length; ++i) {
const auto err2 = _array_reader.read(to_input_var(&val)); const auto err2 = _array_reader.read(to_input_var(&val));
if (err2) { return err2; } if (err2) {
return err2;
}
err = cbor_value_advance(&val); err = cbor_value_advance(&val);
if (err != CborNoError && err != CborErrorOutOfMemory) { if (err != CborNoError && err != CborErrorOutOfMemory) {
return Error(cbor_error_string(err)); return Error(cbor_error_string(err));
@ -172,19 +196,27 @@ namespace rfl {
) const noexcept { ) const noexcept {
size_t length = 0; size_t length = 0;
auto err = cbor_value_get_map_length(_obj.val_, &length); auto err = cbor_value_get_map_length(_obj.val_, &length);
if (err != CborNoError) { return Error(cbor_error_string(err)); } if (err != CborNoError) {
return Error(cbor_error_string(err));
}
CborValue val; CborValue val;
err = cbor_value_enter_container(_obj.val_, &val); err = cbor_value_enter_container(_obj.val_, &val);
if (err != CborNoError) { return Error(cbor_error_string(err)); } if (err != CborNoError) {
return Error(cbor_error_string(err));
}
auto buffer = std::vector<char>(); auto buffer = std::vector<char>();
for (size_t i = 0; i < length; ++i) { for (size_t i = 0; i < length; ++i) {
err = get_string(&val, &buffer); err = get_string(&val, &buffer);
if (err != CborNoError) { return Error(cbor_error_string(err)); } if (err != CborNoError) {
return Error(cbor_error_string(err));
}
err = cbor_value_advance(&val); err = cbor_value_advance(&val);
if (err != CborNoError) { return Error(cbor_error_string(err)); } if (err != CborNoError) {
return Error(cbor_error_string(err));
}
const auto name = std::string_view(buffer.data(), buffer.size() - 1); const auto name = std::string_view(buffer.data(), buffer.size() - 1);
_object_reader.read(name, InputVarType {&val}); _object_reader.read(name, InputVarType {&val});
cbor_value_advance(&val); cbor_value_advance(&val);
@ -206,7 +238,9 @@ namespace rfl {
const noexcept { const noexcept {
size_t length = 0; size_t length = 0;
auto err = cbor_value_get_string_length(_ptr, &length); auto err = cbor_value_get_string_length(_ptr, &length);
if (err != CborNoError && err != CborErrorOutOfMemory) { return err; } if (err != CborNoError && err != CborErrorOutOfMemory) {
return err;
}
_buffer->resize(length + 1); _buffer->resize(length + 1);
(*_buffer)[length] = '\0'; (*_buffer)[length] = '\0';
return cbor_value_copy_text_string( return cbor_value_copy_text_string(

View file

@ -23,8 +23,10 @@ namespace rfl {
using T = std::remove_cvref_t<decltype(_obj)>; using T = std::remove_cvref_t<decltype(_obj)>;
using ParentType = parsing::Parent<Writer>; using ParentType = parsing::Parent<Writer>;
cbor_encoder_init( cbor_encoder_init(
_encoder, reinterpret_cast<uint8_t*>(_buffer->data()), _encoder,
_buffer->size(), 0 reinterpret_cast<uint8_t*>(_buffer->data()),
_buffer->size(),
0
); );
const auto writer = Writer(_encoder); const auto writer = Writer(_encoder);
Parser<T, Processors<Ps...>>::write( Parser<T, Processors<Ps...>>::write(

View file

@ -29,8 +29,8 @@ namespace rfl {
// to their values. // to their values.
template <internal::enums::is_scoped_enum EnumType> template <internal::enums::is_scoped_enum EnumType>
auto get_enumerators() { auto get_enumerators() {
constexpr auto names = internal::enums::get_enum_names< constexpr auto names = internal::enums::
EnumType, internal::enums::is_flag_enum<EnumType>>(); get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
return internal::enums::names_to_enumerator_named_tuple(names); return internal::enums::names_to_enumerator_named_tuple(names);
} }
@ -38,8 +38,8 @@ namespace rfl {
// to their underlying values. // to their underlying values.
template <internal::enums::is_scoped_enum EnumType> template <internal::enums::is_scoped_enum EnumType>
auto get_underlying_enumerators() { auto get_underlying_enumerators() {
constexpr auto names = internal::enums::get_enum_names< constexpr auto names = internal::enums::
EnumType, internal::enums::is_flag_enum<EnumType>>(); get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
return internal::enums::names_to_underlying_enumerator_named_tuple(names); return internal::enums::names_to_underlying_enumerator_named_tuple(names);
} }
@ -47,8 +47,8 @@ namespace rfl {
// std::string_view) and values. // std::string_view) and values.
template <internal::enums::is_scoped_enum EnumType> template <internal::enums::is_scoped_enum EnumType>
constexpr auto get_enumerator_array() { constexpr auto get_enumerator_array() {
constexpr auto names = internal::enums::get_enum_names< constexpr auto names = internal::enums::
EnumType, internal::enums::is_flag_enum<EnumType>>(); get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
return internal::enums::names_to_enumerator_array(names); return internal::enums::names_to_enumerator_array(names);
} }
@ -56,8 +56,8 @@ namespace rfl {
// std::string_view) and underlying values. // std::string_view) and underlying values.
template <internal::enums::is_scoped_enum EnumType> template <internal::enums::is_scoped_enum EnumType>
constexpr auto get_underlying_enumerator_array() { constexpr auto get_underlying_enumerator_array() {
constexpr auto names = internal::enums::get_enum_names< constexpr auto names = internal::enums::
EnumType, internal::enums::is_flag_enum<EnumType>>(); get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
return internal::enums::names_to_underlying_enumerator_array(names); return internal::enums::names_to_underlying_enumerator_array(names);
} }

View file

@ -49,7 +49,9 @@ namespace rfl {
) const noexcept { ) const noexcept {
const auto keys = _obj.Keys(); const auto keys = _obj.Keys();
for (size_t i = 0; i < keys.size(); ++i) { for (size_t i = 0; i < keys.size(); ++i) {
if (_name == keys[i].AsString().c_str()) { return _obj.Values()[i]; } if (_name == keys[i].AsString().c_str()) {
return _obj.Values()[i];
}
} }
return rfl::Error( return rfl::Error(
"Map does not contain any element called '" + _name + "'." "Map does not contain any element called '" + _name + "'."
@ -95,7 +97,9 @@ namespace rfl {
const auto size = _arr.size(); const auto size = _arr.size();
for (size_t i = 0; i < size; ++i) { for (size_t i = 0; i < size; ++i) {
const auto err = _array_reader.read(InputVarType(_arr[i])); const auto err = _array_reader.read(InputVarType(_arr[i]));
if (err) { return err; } if (err) {
return err;
}
} }
return std::nullopt; return std::nullopt;
} }
@ -128,7 +132,9 @@ namespace rfl {
rfl::Result<InputObjectType> to_object(const InputVarType& _var rfl::Result<InputObjectType> to_object(const InputVarType& _var
) const noexcept { ) const noexcept {
if (!_var.IsMap()) { return rfl::Error("Could not cast to Map!"); } if (!_var.IsMap()) {
return rfl::Error("Could not cast to Map!");
}
return _var.AsMap(); return _var.AsMap();
} }

View file

@ -17,7 +17,8 @@ namespace rfl {
auto from_named_tuple(NamedTupleType&& _n) { auto from_named_tuple(NamedTupleType&& _n) {
using RequiredType = std::remove_cvref_t<rfl::named_tuple_t<T>>; using RequiredType = std::remove_cvref_t<rfl::named_tuple_t<T>>;
if constexpr (!std::is_same< if constexpr (!std::is_same<
std::remove_cvref_t<NamedTupleType>, RequiredType>()) { std::remove_cvref_t<NamedTupleType>,
RequiredType>()) {
return from_named_tuple<T>(RequiredType(std::forward<NamedTupleType>(_n)) return from_named_tuple<T>(RequiredType(std::forward<NamedTupleType>(_n))
); );
} else if constexpr (internal::has_fields<T>()) { } else if constexpr (internal::has_fields<T>()) {
@ -40,7 +41,8 @@ namespace rfl {
auto from_named_tuple(const NamedTupleType& _n) { auto from_named_tuple(const NamedTupleType& _n) {
using RequiredType = std::remove_cvref_t<rfl::named_tuple_t<T>>; using RequiredType = std::remove_cvref_t<rfl::named_tuple_t<T>>;
if constexpr (!std::is_same< if constexpr (!std::is_same<
std::remove_cvref_t<NamedTupleType>, RequiredType>()) { std::remove_cvref_t<NamedTupleType>,
RequiredType>()) {
return from_named_tuple<T>(RequiredType(_n)); return from_named_tuple<T>(RequiredType(_n));
} else if constexpr (internal::has_fields<T>()) { } else if constexpr (internal::has_fields<T>()) {
return internal::copy_from_named_tuple<T>(_n); return internal::copy_from_named_tuple<T>(_n);

View file

@ -42,7 +42,8 @@ namespace rfl::internal {
static_assert( static_assert(
std::is_same< std::is_same<
typename std::tuple_element< typename std::tuple_element<
index, typename NamedTupleType::Fields>::type::Type, index,
typename NamedTupleType::Fields>::type::Type,
typename Field::Type>(), typename Field::Type>(),
"If two fields have the same name, " "If two fields have the same name, "
"their type must be the same as " "their type must be the same as "
@ -73,7 +74,8 @@ namespace rfl::internal {
static_assert( static_assert(
std::is_same< std::is_same<
typename std::tuple_element< typename std::tuple_element<
index, typename NamedTupleType::Fields>::type::Type, index,
typename NamedTupleType::Fields>::type::Type,
typename Field::Type>(), typename Field::Type>(),
"If two fields have the same name, " "If two fields have the same name, "
"their type must be the same as " "their type must be the same as "

View file

@ -38,7 +38,9 @@ namespace rfl {
const StringLiteral<N1>& _first, const StringLiteral<N1>& _first,
const StringLiteral<N2>& _second const StringLiteral<N2>& _second
) { ) {
if constexpr (N1 != N2) { return false; } if constexpr (N1 != N2) {
return false;
}
return _first.string_view() == _second.string_view(); return _first.string_view() == _second.string_view();
} }

View file

@ -124,17 +124,26 @@ namespace rfl {
return NamesType {}; return NamesType {};
} else { } else {
return get_enum_names_impl< return get_enum_names_impl<
EnumType, NamesType, _max, _is_flag, _i + 1>(); EnumType,
NamesType,
_max,
_is_flag,
_i + 1>();
} }
} else { } else {
using NewNames = typename NamesType::template AddOneType< using NewNames = typename NamesType::template AddOneType<
Literal<remove_namespaces<name>()>, static_cast<EnumType>(j)>; Literal<remove_namespaces<name>()>,
static_cast<EnumType>(j)>;
if constexpr (j == _max) { if constexpr (j == _max) {
return NewNames {}; return NewNames {};
} else { } else {
return get_enum_names_impl< return get_enum_names_impl<
EnumType, NewNames, _max, _is_flag, _i + 1>(); EnumType,
NewNames,
_max,
_is_flag,
_i + 1>();
} }
} }
} }

View file

@ -139,7 +139,8 @@ namespace rfl::internal {
const auto get = []<std::size_t... Is>(std::index_sequence<Is...>) { const auto get = []<std::size_t... Is>(std::index_sequence<Is...>) {
return concat_literals( return concat_literals(
get_field_name< get_field_name<
Type, wrap(std::get<Is>(bind_fake_object_to_tuple<T>()))>()... Type,
wrap(std::get<Is>(bind_fake_object_to_tuple<T>()))>()...
); );
}; };
#else #else

View file

@ -29,7 +29,9 @@ namespace rfl {
auto split = func_name.substr(0, func_name.size() - 7); auto split = func_name.substr(0, func_name.size() - 7);
split = split.substr(split.find("get_type_name_str_view<") + 23); split = split.substr(split.find("get_type_name_str_view<") + 23);
auto pos = split.find(" "); auto pos = split.find(" ");
if (pos != std::string_view::npos) { return split.substr(pos + 1); } if (pos != std::string_view::npos) {
return split.substr(pos + 1);
}
return split; return split;
#else #else
static_assert( static_assert(

View file

@ -18,14 +18,16 @@ namespace rfl {
using T = std::tuple_element_t<i, std::remove_cvref_t<FieldTuple>>; using T = std::tuple_element_t<i, std::remove_cvref_t<FieldTuple>>;
if constexpr (is_flatten_field<T>::value) { if constexpr (is_flatten_field<T>::value) {
return move_and_flatten_field_tuple( return move_and_flatten_field_tuple(
std::move(_t), std::move(_args)..., std::move(_t),
std::move(_args)...,
move_and_flatten_field_tuple( move_and_flatten_field_tuple(
move_to_field_tuple(std::move(std::get<i>(_t).value_)) move_to_field_tuple(std::move(std::get<i>(_t).value_))
) )
); );
} else { } else {
return move_and_flatten_field_tuple( return move_and_flatten_field_tuple(
std::move(_t), std::move(_args)..., std::move(_t),
std::move(_args)...,
std::make_tuple(std::move(std::get<i>(_t))) std::make_tuple(std::move(std::get<i>(_t)))
); );
} }

View file

@ -29,8 +29,8 @@ namespace rfl {
std::remove_cvref_t<std::remove_pointer_t<typename Field::Type>>; std::remove_cvref_t<std::remove_pointer_t<typename Field::Type>>;
if constexpr (is_named_tuple_v<T>) { if constexpr (is_named_tuple_v<T>) {
using SubPtrNamedTupleType = typename std::invoke_result< using SubPtrNamedTupleType = typename std::
decltype(nt_to_ptr_named_tuple<T>), T>::type; invoke_result<decltype(nt_to_ptr_named_tuple<T>), T>::type;
return make_ptr_fields<PtrFieldTupleType>( return make_ptr_fields<PtrFieldTupleType>(
_n, _args..., SubPtrNamedTupleType(_n).fields() _n, _args..., SubPtrNamedTupleType(_n).fields()
@ -63,7 +63,8 @@ namespace rfl {
if constexpr (is_field_v<FieldType>) { if constexpr (is_field_v<FieldType>) {
return move_from_ptr_fields<T>( return move_from_ptr_fields<T>(
_ptrs, std::move(_args)..., _ptrs,
std::move(_args)...,
rfl::make_field<FieldType::name_>( rfl::make_field<FieldType::name_>(
std::move(*std::get<i>(_ptrs).value()) std::move(*std::get<i>(_ptrs).value())
) )
@ -76,7 +77,8 @@ namespace rfl {
typename std::tuple_element_t<i, PtrFieldTupleType>::Type>>; typename std::tuple_element_t<i, PtrFieldTupleType>::Type>>;
return move_from_ptr_fields<T>( return move_from_ptr_fields<T>(
_ptrs, std::move(_args)..., _ptrs,
std::move(_args)...,
move_from_ptr_fields<U>(std::get<i>(_ptrs)) move_from_ptr_fields<U>(std::get<i>(_ptrs))
); );
} }

View file

@ -54,8 +54,11 @@ namespace rfl {
calc_flattened_size<SubTargetTupleType>(); calc_flattened_size<SubTargetTupleType>();
return unflatten_ptr_tuple< return unflatten_ptr_tuple<
TargetTupleType, PtrTupleType, _j + flattened_size>( TargetTupleType,
_t, _args..., PtrTupleType,
_j + flattened_size>(
_t,
_args...,
unflatten_ptr_tuple<SubTargetTupleType, PtrTupleType, _j>(_t) unflatten_ptr_tuple<SubTargetTupleType, PtrTupleType, _j>(_t)
); );
@ -88,7 +91,8 @@ namespace rfl {
typename std::tuple_element_t<i, PtrTupleType>>::Type>; typename std::tuple_element_t<i, PtrTupleType>>::Type>;
return move_from_pointers<T>( return move_from_pointers<T>(
_ptrs, std::move(_args)..., _ptrs,
std::move(_args)...,
move_from_pointers<U>(std::get<i>(_ptrs)) move_from_pointers<U>(std::get<i>(_ptrs))
); );
} }

View file

@ -45,7 +45,8 @@ namespace rfl {
using FieldType = typename std::tuple_element<i, Fields>::type; using FieldType = typename std::tuple_element<i, Fields>::type;
using T = std::remove_cvref_t<typename FieldType::Type>; using T = std::remove_cvref_t<typename FieldType::Type>;
return nt_to_ptr_named_tuple( return nt_to_ptr_named_tuple(
_nt, _a..., _nt,
_a...,
Field<FieldType::name_, const T*>(&std::get<i>(_nt.values())) Field<FieldType::name_, const T*>(&std::get<i>(_nt.values()))
); );
} }

View file

@ -83,11 +83,13 @@ namespace rfl {
template <std::size_t l, std::size_t nested, std::size_t r> template <std::size_t l, std::size_t nested, std::size_t r>
static consteval bool constructible_with_nested() { static consteval bool constructible_with_nested() {
return return
[]<std::size_t... i, std::size_t... j, []<std::size_t... i,
std::size_t... j,
std:: std::
size_t... k>(std::index_sequence<i...>, std::index_sequence<j...>, std::index_sequence<k...>) { size_t... k>(std::index_sequence<i...>, std::index_sequence<j...>, std::index_sequence<k...>) {
return requires { T {any(i)..., {any(j)...}, any(k)...}; }; return requires { T {any(i)..., {any(j)...}, any(k)...}; };
}(std::make_index_sequence<l>(), std::make_index_sequence<nested>(), }(std::make_index_sequence<l>(),
std::make_index_sequence<nested>(),
std::make_index_sequence<r>()); std::make_index_sequence<r>());
} }
@ -122,7 +124,8 @@ namespace rfl {
std:: std::
size_t... r>(std::index_sequence<l...>, std::index_sequence<r...>) { size_t... r>(std::index_sequence<l...>, std::index_sequence<r...>) {
return requires { return requires {
T {any_empty_base<T>(l)..., any_base<T>(0), T {any_empty_base<T>(l)...,
any_base<T>(0),
any_empty_base<T>(r)...}; any_empty_base<T>(r)...};
}; };
}; };

View file

@ -14,7 +14,9 @@ namespace rfl {
consteval auto remove_namespaces() { consteval auto remove_namespaces() {
constexpr auto name = _name.string_view(); constexpr auto name = _name.string_view();
constexpr size_t pos = name.find_last_of(":"); constexpr size_t pos = name.find_last_of(":");
if constexpr (pos == std::string_view::npos) { return _name; } if constexpr (pos == std::string_view::npos) {
return _name;
}
constexpr auto substr = name.substr(pos + 1); constexpr auto substr = name.substr(pos + 1);
const auto to_str_lit = [&]<auto... Ns>(std::index_sequence<Ns...>) { const auto to_str_lit = [&]<auto... Ns>(std::index_sequence<Ns...>) {
return StringLiteral<sizeof...(Ns) + 1> {substr[Ns]...}; return StringLiteral<sizeof...(Ns) + 1> {substr[Ns]...};

View file

@ -13,7 +13,9 @@ namespace rfl {
const std::string& _delimiter, const std::string& _delimiter,
const std::vector<std::string>& _strings const std::vector<std::string>& _strings
) { ) {
if (_strings.size() == 0) { return ""; } if (_strings.size() == 0) {
return "";
}
auto res = _strings[0]; auto res = _strings[0];
for (size_t i = 1; i < _strings.size(); ++i) { for (size_t i = 1; i < _strings.size(); ++i) {
res += _delimiter + _strings[i]; res += _delimiter + _strings[i];

View file

@ -22,12 +22,14 @@ namespace rfl {
using T = std::tuple_element_t<i, std::remove_cvref_t<PtrTuple>>; using T = std::tuple_element_t<i, std::remove_cvref_t<PtrTuple>>;
if constexpr (is_flatten_field_v<T>) { if constexpr (is_flatten_field_v<T>) {
return flatten_ptr_tuple( return flatten_ptr_tuple(
std::forward<PtrTuple>(_t), std::forward<Args>(_args)..., std::forward<PtrTuple>(_t),
std::forward<Args>(_args)...,
flatten_ptr_tuple(to_ptr_tuple(std::get<i>(_t)->get())) flatten_ptr_tuple(to_ptr_tuple(std::get<i>(_t)->get()))
); );
} else { } else {
return flatten_ptr_tuple( return flatten_ptr_tuple(
std::forward<PtrTuple>(_t), std::forward<Args>(_args)..., std::forward<PtrTuple>(_t),
std::forward<Args>(_args)...,
std::make_tuple(std::get<i>(_t)) std::make_tuple(std::get<i>(_t))
); );
} }

View file

@ -30,7 +30,8 @@ namespace rfl {
if constexpr (internal::is_flatten_field<T>::value) { if constexpr (internal::is_flatten_field<T>::value) {
auto subtuple = internal::to_ptr_field_tuple(*std::get<i>(_t).get()); auto subtuple = internal::to_ptr_field_tuple(*std::get<i>(_t).get());
return flatten_ptr_field_tuple( return flatten_ptr_field_tuple(
_t, std::forward<Args>(_args)..., _t,
std::forward<Args>(_args)...,
flatten_ptr_field_tuple(subtuple) flatten_ptr_field_tuple(subtuple)
); );
} else { } else {

View file

@ -30,10 +30,18 @@ namespace rfl::internal {
return transform_snake_case<_name, false, _name.arr_.size(), chars...>(); return transform_snake_case<_name, false, _name.arr_.size(), chars...>();
} else if constexpr (_capitalize) { } else if constexpr (_capitalize) {
return transform_snake_case< return transform_snake_case<
_name, false, _i + 1, chars..., to_upper<_name.arr_[_i]>()>(); _name,
false,
_i + 1,
chars...,
to_upper<_name.arr_[_i]>()>();
} else { } else {
return transform_snake_case< return transform_snake_case<
_name, false, _i + 1, chars..., _name.arr_[_i]>(); _name,
false,
_i + 1,
chars...,
_name.arr_[_i]>();
} }
} }
} // namespace rfl::internal } // namespace rfl::internal

View file

@ -71,7 +71,9 @@ namespace rfl {
yyjson_arr_iter_init(_arr.val_, &iter); yyjson_arr_iter_init(_arr.val_, &iter);
while ((val = yyjson_arr_iter_next(&iter))) { while ((val = yyjson_arr_iter_next(&iter))) {
const auto err = _array_reader.read(InputVarType(val)); const auto err = _array_reader.read(InputVarType(val));
if (err) { return err; } if (err) {
return err;
}
} }
return std::nullopt; return std::nullopt;
} }
@ -95,7 +97,9 @@ namespace rfl {
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>()) { if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
const auto r = yyjson_get_str(_var.val_); const auto r = yyjson_get_str(_var.val_);
if (r == NULL) { return rfl::Error("Could not cast to string."); } if (r == NULL) {
return rfl::Error("Could not cast to string.");
}
return std::string(r); return std::string(r);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) { } else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!yyjson_is_bool(_var.val_)) { if (!yyjson_is_bool(_var.val_)) {

View file

@ -28,7 +28,9 @@ namespace rfl {
Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _json_str Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _json_str
) { ) {
yyjson_doc* doc = yyjson_read(_json_str.c_str(), _json_str.size(), 0); yyjson_doc* doc = yyjson_read(_json_str.c_str(), _json_str.size(), 0);
if (!doc) { return Error("Could not parse document"); } if (!doc) {
return Error("Could not parse document");
}
yyjson_val* root = yyjson_doc_get_root(doc); yyjson_val* root = yyjson_doc_get_root(doc);
const auto r = Reader(); const auto r = Reader();
auto res = Parser<T, Processors<Ps...>>::read(r, InputVarType(root)); auto res = Parser<T, Processors<Ps...>>::read(r, InputVarType(root));

View file

@ -225,7 +225,9 @@ namespace rfl::json {
auto required = std::vector<std::string>(); auto required = std::vector<std::string>();
for (const auto& [k, v] : _t.types_) { for (const auto& [k, v] : _t.types_) {
properties[k] = type_to_json_schema_type(v); properties[k] = type_to_json_schema_type(v);
if (!is_optional(v)) { required.push_back(k); } if (!is_optional(v)) {
required.push_back(k);
}
} }
return schema::Type { return schema::Type {
.value = .value =

View file

@ -45,7 +45,9 @@ namespace rfl {
} }
const auto current_name = const auto current_name =
std::string_view(key.via.str.ptr, key.via.str.size); std::string_view(key.via.str.ptr, key.via.str.size);
if (_name == current_name) { return _obj.ptr[i].val; } if (_name == current_name) {
return _obj.ptr[i].val;
}
} }
return Error("No field named '" + _name + "' was found."); return Error("No field named '" + _name + "' was found.");
} }
@ -111,7 +113,9 @@ namespace rfl {
) const noexcept { ) const noexcept {
for (uint32_t i = 0; i < _arr.size; ++i) { for (uint32_t i = 0; i < _arr.size; ++i) {
const auto err = _array_reader.read(_arr.ptr[i]); const auto err = _array_reader.read(_arr.ptr[i]);
if (err) { return err; } if (err) {
return err;
}
} }
return std::nullopt; return std::nullopt;
} }

View file

@ -45,7 +45,9 @@ namespace rfl {
&_r, &field_variant &_r, &field_variant
); );
auto err = _r.read_object(reader, _obj); auto err = _r.read_object(reader, _obj);
if (err) { return *err; } if (err) {
return *err;
}
if (!field_variant) { if (!field_variant) {
return Error( return Error(
"Could not parse: Expected the object to have " "Could not parse: Expected the object to have "

View file

@ -97,8 +97,12 @@ namespace rfl {
const auto map_reader = const auto map_reader =
MapReader<R, W, MapType, ProcessorsType>(&_r, &map, &errors); MapReader<R, W, MapType, ProcessorsType>(&_r, &map, &errors);
const auto err = _r.read_object(map_reader, _obj); const auto err = _r.read_object(map_reader, _obj);
if (err) { return *err; } if (err) {
if (errors.size() != 0) { return to_single_error_message(errors); } return *err;
}
if (errors.size() != 0) {
return to_single_error_message(errors);
}
return map; return map;
} }
}; };

View file

@ -65,7 +65,9 @@ namespace rfl {
using ViewType = std::remove_cvref_t<decltype(view)>; using ViewType = std::remove_cvref_t<decltype(view)>;
const auto err = const auto err =
Parser<R, W, ViewType, ProcessorsType>::read_view(_r, _var, &view); Parser<R, W, ViewType, ProcessorsType>::read_view(_r, _var, &view);
if (err) [[unlikely]] { return *err; } if (err) [[unlikely]] {
return *err;
}
return *ptr; return *ptr;
} }
@ -76,7 +78,9 @@ namespace rfl {
NamedTuple<FieldTypes...>* _view NamedTuple<FieldTypes...>* _view
) noexcept { ) noexcept {
auto obj = _r.to_object(_var); auto obj = _r.to_object(_var);
if (!obj) [[unlikely]] { return obj.error(); } if (!obj) [[unlikely]] {
return obj.error();
}
return read_object(_r, *obj, _view); return read_object(_r, *obj, _view);
} }
@ -106,8 +110,8 @@ namespace rfl {
if constexpr (_i == size) { if constexpr (_i == size) {
return Type {Type::Object {_values}}; return Type {Type::Object {_values}};
} else { } else {
using F = std::tuple_element_t< using F = std::
_i, typename NamedTuple<FieldTypes...>::Fields>; tuple_element_t<_i, typename NamedTuple<FieldTypes...>::Fields>;
using U = typename F::Type; using U = typename F::Type;
if constexpr (!internal::is_skip_v<U>) { if constexpr (!internal::is_skip_v<U>) {
_values[std::string(F::name())] = _values[std::string(F::name())] =
@ -217,9 +221,14 @@ namespace rfl {
&_r, _view, &found, &set, &errors &_r, _view, &found, &set, &errors
); );
const auto err = _r.read_object(object_reader, _obj); const auto err = _r.read_object(object_reader, _obj);
if (err) { return *err; } if (err) {
return *err;
}
handle_missing_fields( handle_missing_fields(
found, *_view, &set, &errors, found,
*_view,
&set,
&errors,
std::make_integer_sequence<int, size_>() std::make_integer_sequence<int, size_>()
); );
if (errors.size() != 0) { if (errors.size() != 0) {

View file

@ -165,7 +165,8 @@ namespace rfl {
.description_ = typename U::Content().str(), .description_ = typename U::Content().str(),
.type_ = .type_ =
Ref<Type>::make(Parser< Ref<Type>::make(Parser<
R, W, std::remove_cvref_t<typename U::Type>, R, W,
std::remove_cvref_t<typename U::Type>,
ProcessorsType>::to_schema(_definitions)) ProcessorsType>::to_schema(_definitions))
} }
}; };
@ -244,7 +245,9 @@ namespace rfl {
using ViewType = std::remove_cvref_t<decltype(view)>; using ViewType = std::remove_cvref_t<decltype(view)>;
const auto err = const auto err =
Parser<R, W, ViewType, ProcessorsType>::read_view(_r, _var, &view); Parser<R, W, ViewType, ProcessorsType>::read_view(_r, _var, &view);
if (err) [[unlikely]] { return *err; } if (err) [[unlikely]] {
return *err;
}
return std::move(*ptr); return std::move(*ptr);
} }

View file

@ -25,7 +25,9 @@ namespace rfl {
static Result<std::optional<T>> static Result<std::optional<T>>
read(const R& _r, const InputVarType& _var) noexcept { read(const R& _r, const InputVarType& _var) noexcept {
if (_r.is_empty(_var)) { return std::optional<T>(); } if (_r.is_empty(_var)) {
return std::optional<T>();
}
const auto to_opt = [](auto&& _t) { return std::make_optional<T>(_t); }; const auto to_opt = [](auto&& _t) { return std::make_optional<T>(_t); };
return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::read( return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::read(
_r, _var _r, _var

View file

@ -46,7 +46,9 @@ namespace rfl {
) noexcept { ) noexcept {
const auto tup = std::make_tuple(&_p.first, &_p.second); const auto tup = std::make_tuple(&_p.first, &_p.second);
Parser< Parser<
R, W, std::tuple<const FirstType*, const SecondType*>, R,
W,
std::tuple<const FirstType*, const SecondType*>,
ProcessorsType>::write(_w, tup, _parent); ProcessorsType>::write(_w, tup, _parent);
} }

View file

@ -24,7 +24,9 @@ namespace rfl::parsing {
static Result<std::shared_ptr<T>> static Result<std::shared_ptr<T>>
read(const R& _r, const InputVarType& _var) noexcept { read(const R& _r, const InputVarType& _var) noexcept {
if (_r.is_empty(_var)) { return std::shared_ptr<T>(); } if (_r.is_empty(_var)) {
return std::shared_ptr<T>();
}
const auto to_ptr = [](auto&& _t) { const auto to_ptr = [](auto&& _t) {
return std::make_shared<T>(std::move(_t)); return std::make_shared<T>(std::move(_t));
}; };

View file

@ -41,8 +41,9 @@ namespace rfl {
); );
} else { } else {
const auto to_skip = [&](auto&& _t) { const auto to_skip = [&](auto&& _t) {
return internal::Skip< return internal::
T, _skip_serialization, _skip_deserialization>(std::move(_t)); Skip<T, _skip_serialization, _skip_deserialization>(std::move(_t
));
}; };
return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::read( return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::read(
_r, _var _r, _var
@ -60,7 +61,9 @@ namespace rfl {
) noexcept { ) noexcept {
if constexpr (_skip_serialization) { if constexpr (_skip_serialization) {
using ReflectionType = std::remove_cvref_t<typename internal::Skip< using ReflectionType = std::remove_cvref_t<typename internal::Skip<
T, _skip_serialization, _skip_deserialization>::ReflectionType>; T,
_skip_serialization,
_skip_deserialization>::ReflectionType>;
Parser<R, W, ReflectionType, ProcessorsType>::write( Parser<R, W, ReflectionType, ProcessorsType>::write(
_w, ReflectionType(), _parent _w, ReflectionType(), _parent
); );

View file

@ -82,8 +82,8 @@ namespace rfl {
const InputVarType& _var const InputVarType& _var
) noexcept { ) noexcept {
if constexpr (_i == sizeof...(AlternativeTypes)) { if constexpr (_i == sizeof...(AlternativeTypes)) {
const auto names = TaggedUnion< const auto names = TaggedUnion<_discriminator, AlternativeTypes...>::
_discriminator, AlternativeTypes...>::PossibleTags::names(); PossibleTags::names();
return Error( return Error(
"Could not parse tagged union, could not match " + "Could not parse tagged union, could not match " +
_discriminator.str() + " '" + _disc_value + _discriminator.str() + " '" + _disc_value +
@ -91,9 +91,9 @@ namespace rfl {
internal::strings::join(",", names) internal::strings::join(",", names)
); );
} else { } else {
using AlternativeType = using AlternativeType = std::remove_cvref_t<
std::remove_cvref_t<std::variant_alternative_t< std::
_i, std::variant<AlternativeTypes...>>>; variant_alternative_t<_i, std::variant<AlternativeTypes...>>>;
if (contains_disc_value<AlternativeType>(_disc_value)) { if (contains_disc_value<AlternativeType>(_disc_value)) {
const auto to_tagged_union = [](auto&& _val) { const auto to_tagged_union = [](auto&& _val) {

View file

@ -25,7 +25,9 @@ namespace rfl {
static Result<std::unique_ptr<T>> static Result<std::unique_ptr<T>>
read(const R& _r, const InputVarType& _var) noexcept { read(const R& _r, const InputVarType& _var) noexcept {
if (_r.is_empty(_var)) { return std::unique_ptr<T>(); } if (_r.is_empty(_var)) {
return std::unique_ptr<T>();
}
const auto to_ptr = [](auto&& _t) { const auto to_ptr = [](auto&& _t) {
return std::make_unique<T>(std::move(_t)); return std::make_unique<T>(std::move(_t));
}; };

View file

@ -73,8 +73,8 @@ namespace rfl {
std::vector<schema::Type> _types = {} std::vector<schema::Type> _types = {}
) { ) {
if constexpr (internal::all_fields<std::tuple<FieldTypes...>>()) { if constexpr (internal::all_fields<std::tuple<FieldTypes...>>()) {
return FieldVariantParser< return FieldVariantParser<R, W, ProcessorsType, FieldTypes...>::
R, W, ProcessorsType, FieldTypes...>::to_schema(_definitions); to_schema(_definitions);
} else { } else {
using Type = schema::Type; using Type = schema::Type;
constexpr size_t size = sizeof...(FieldTypes); constexpr size_t size = sizeof...(FieldTypes);

View file

@ -24,7 +24,9 @@ namespace rfl {
static Result<std::wstring> static Result<std::wstring>
read(const R& _r, const InputVarType& _var) noexcept { read(const R& _r, const InputVarType& _var) noexcept {
if (_r.is_empty(_var)) { return std::wstring(); } if (_r.is_empty(_var)) {
return std::wstring();
}
auto inStr = Parser<R, W, std::string, ProcessorsType>::read(_r, _var); auto inStr = Parser<R, W, std::string, ProcessorsType>::read(_r, _var);
if (auto err = inStr.error(); err.has_value()) { if (auto err = inStr.error(); err.has_value()) {

View file

@ -38,7 +38,11 @@ namespace rfl::parsing {
alignas(std::tuple<Ts...>) unsigned char buf[sizeof(std::tuple<Ts...>)]; alignas(std::tuple<Ts...>) unsigned char buf[sizeof(std::tuple<Ts...>)];
auto ptr = reinterpret_cast<std::tuple<Ts...>*>(buf); auto ptr = reinterpret_cast<std::tuple<Ts...>*>(buf);
const auto tuple_reader = TupleReader< const auto tuple_reader = TupleReader<
R, W, std::tuple<Ts...>, _ignore_empty_containers, _all_required, R,
W,
std::tuple<Ts...>,
_ignore_empty_containers,
_all_required,
ProcessorsType>(&_r, ptr); ProcessorsType>(&_r, ptr);
auto err = _r.read_array(tuple_reader, _arr); auto err = _r.read_array(tuple_reader, _arr);
if (err) { if (err) {

View file

@ -31,7 +31,9 @@ namespace rfl::parsing {
std::optional<Error> handle_missing_fields() const { std::optional<Error> handle_missing_fields() const {
std::optional<Error> err; std::optional<Error> err;
if (i_ < size_) { handle_missing_fields_impl(&err); } if (i_ < size_) {
handle_missing_fields_impl(&err);
}
return err; return err;
} }
@ -46,7 +48,9 @@ namespace rfl::parsing {
template <size_t _i = 0> template <size_t _i = 0>
void call_destructors_where_necessary() const { void call_destructors_where_necessary() const {
if constexpr (_i < size_) { if constexpr (_i < size_) {
if (_i >= i_) { return; } if (_i >= i_) {
return;
}
using CurrentType = using CurrentType =
std::remove_cvref_t<std::tuple_element_t<_i, TupleType>>; std::remove_cvref_t<std::tuple_element_t<_i, TupleType>>;
if constexpr (!std::is_array_v<CurrentType> && if constexpr (!std::is_array_v<CurrentType> &&

View file

@ -61,7 +61,9 @@ namespace rfl {
auto vector_reader = auto vector_reader =
VectorReader<R, W, VecType, ProcessorsType>(&_r, &vec); VectorReader<R, W, VecType, ProcessorsType>(&_r, &vec);
const auto err = _r.read_array(vector_reader, _arr); const auto err = _r.read_array(vector_reader, _arr);
if (err) { return *err; } if (err) {
return *err;
}
return vec; return vec;
}; };
return _r.to_array(_var).and_then(parse); return _r.to_array(_var).and_then(parse);

View file

@ -48,7 +48,9 @@ namespace rfl::parsing {
using K = std::remove_cvref_t<typename T::first_type>; using K = std::remove_cvref_t<typename T::first_type>;
using V = std::remove_cvref_t<typename T::second_type>; using V = std::remove_cvref_t<typename T::second_type>;
return Parser< return Parser<
R, W, std::remove_cvref_t<std::pair<K, V>>, R,
W,
std::remove_cvref_t<std::pair<K, V>>,
ProcessorsType>::read(*r_, _var); ProcessorsType>::read(*r_, _var);
} }

View file

@ -34,7 +34,13 @@ namespace rfl::parsing {
/// such a field exists in the underlying view. /// such a field exists in the underlying view.
void read(const std::string_view& _name, const InputVarType& _var) const { void read(const std::string_view& _name, const InputVarType& _var) const {
assign_to_matching_field( assign_to_matching_field(
*r_, _name, _var, view_, errors_, found_, set_, *r_,
_name,
_var,
view_,
errors_,
found_,
set_,
std::make_integer_sequence<int, size_>() std::make_integer_sequence<int, size_>()
); );
} }
@ -90,7 +96,13 @@ namespace rfl::parsing {
assign_to_matching_field(const R& _r, const std::string_view& _current_name, const auto& _var, auto* _view, auto* _errors, auto* _found, auto* _set, std::integer_sequence<int, is...>) { assign_to_matching_field(const R& _r, const std::string_view& _current_name, const auto& _var, auto* _view, auto* _errors, auto* _found, auto* _set, std::integer_sequence<int, is...>) {
bool already_assigned = false; bool already_assigned = false;
(assign_if_field_matches<is>( (assign_if_field_matches<is>(
_r, _current_name, _var, _view, _errors, _found, _set, _r,
_current_name,
_var,
_view,
_errors,
_found,
_set,
&already_assigned &already_assigned
), ),
...); ...);
@ -116,7 +128,9 @@ namespace rfl::parsing {
if constexpr (!std::is_array_v<ValueType> && if constexpr (!std::is_array_v<ValueType> &&
std::is_pointer_v<OriginalType> && std::is_pointer_v<OriginalType> &&
std::is_destructible_v<ValueType>) { std::is_destructible_v<ValueType>) {
if (std::get<_i>(*set_)) { rfl::get<_i>(*view_)->~ValueType(); } if (std::get<_i>(*set_)) {
rfl::get<_i>(*view_)->~ValueType();
}
} else if constexpr (std::is_array_v<ValueType>) { } else if constexpr (std::is_array_v<ValueType>) {
if (std::get<_i>(*set_)) { if (std::get<_i>(*set_)) {
auto ptr = rfl::get<_i>(*view_); auto ptr = rfl::get<_i>(*view_);

View file

@ -39,7 +39,8 @@ namespace rfl {
using Type = std::remove_cvref_t<std::remove_pointer_t<T>>; using Type = std::remove_cvref_t<std::remove_pointer_t<T>>;
if constexpr (internal::has_reflection_type_v<Type>) { if constexpr (internal::has_reflection_type_v<Type>) {
return is_required< return is_required<
typename Type::ReflectionType, _ignore_empty_containers>(); typename Type::ReflectionType,
_ignore_empty_containers>();
} else if constexpr (internal::is_rename_v<Type>) { } else if constexpr (internal::is_rename_v<Type>) {
return is_required<typename Type::Type, _ignore_empty_containers>(); return is_required<typename Type::Type, _ignore_empty_containers>();
} else { } else {

View file

@ -47,19 +47,27 @@ namespace rfl::toml {
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>()) { if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
const auto ptr = _var->as<std::string>(); const auto ptr = _var->as<std::string>();
if (!ptr) { return Error("Could not cast the node to std::string!"); } if (!ptr) {
return Error("Could not cast the node to std::string!");
}
return **ptr; return **ptr;
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) { } else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
const auto ptr = _var->as<bool>(); const auto ptr = _var->as<bool>();
if (!ptr) { return Error("Could not cast the node to bool!"); } if (!ptr) {
return Error("Could not cast the node to bool!");
}
return **ptr; return **ptr;
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) { } else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
const auto ptr = _var->as<double>(); const auto ptr = _var->as<double>();
if (!ptr) { return Error("Could not cast the node to double!"); } if (!ptr) {
return Error("Could not cast the node to double!");
}
return static_cast<std::remove_cvref_t<T>>(**ptr); return static_cast<std::remove_cvref_t<T>>(**ptr);
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) { } else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
const auto ptr = _var->as<int64_t>(); const auto ptr = _var->as<int64_t>();
if (!ptr) { return Error("Could not cast the node to int64_t!"); } if (!ptr) {
return Error("Could not cast the node to int64_t!");
}
return static_cast<std::remove_cvref_t<T>>(**ptr); return static_cast<std::remove_cvref_t<T>>(**ptr);
} else { } else {
static_assert(rfl::always_false_v<T>, "Unsupported type."); static_assert(rfl::always_false_v<T>, "Unsupported type.");
@ -69,7 +77,9 @@ namespace rfl::toml {
rfl::Result<InputArrayType> to_array(const InputVarType& _var rfl::Result<InputArrayType> to_array(const InputVarType& _var
) const noexcept { ) const noexcept {
const auto ptr = _var->as_array(); const auto ptr = _var->as_array();
if (!ptr) { return rfl::Error("Could not cast to an array!"); } if (!ptr) {
return rfl::Error("Could not cast to an array!");
}
return ptr; return ptr;
} }
@ -80,7 +90,9 @@ namespace rfl::toml {
) const noexcept { ) const noexcept {
for (auto& node : *_arr) { for (auto& node : *_arr) {
const auto err = _array_reader.read(&node); const auto err = _array_reader.read(&node);
if (err) { return err; } if (err) {
return err;
}
} }
return std::nullopt; return std::nullopt;
} }
@ -99,7 +111,9 @@ namespace rfl::toml {
rfl::Result<InputObjectType> to_object(const InputVarType& _var rfl::Result<InputObjectType> to_object(const InputVarType& _var
) const noexcept { ) const noexcept {
const auto ptr = _var->as_table(); const auto ptr = _var->as_table();
if (!ptr) { return rfl::Error("Could not cast to a table!"); } if (!ptr) {
return rfl::Error("Could not cast to a table!");
}
return ptr; return ptr;
} }

View file

@ -142,7 +142,9 @@ namespace rfl {
const auto name = _arr.node_.name(); const auto name = _arr.node_.name();
for (auto node = _arr.node_; node; node = node.next_sibling(name)) { for (auto node = _arr.node_; node; node = node.next_sibling(name)) {
const auto err = _array_reader.read(InputVarType(node)); const auto err = _array_reader.read(InputVarType(node));
if (err) { return err; } if (err) {
return err;
}
} }
return std::nullopt; return std::nullopt;
} }

View file

@ -91,7 +91,9 @@ namespace rfl {
) const noexcept { ) const noexcept {
for (size_t i = 0; i < _arr.node_.size(); ++i) { for (size_t i = 0; i < _arr.node_.size(); ++i) {
const auto err = _array_reader.read(_arr.node_[i]); const auto err = _array_reader.read(_arr.node_[i]);
if (err) { return err; } if (err) {
return err;
}
} }
return std::nullopt; return std::nullopt;
} }

View file

@ -1,7 +1,10 @@
#pragma once #pragma once
#include <cstddef>
#include <cstdint> #include <cstdint>
#define fn auto
// Unsigned integers // Unsigned integers
using u8 = std::uint8_t; using u8 = std::uint8_t;
using u16 = std::uint16_t; using u16 = std::uint16_t;

View file

@ -869,8 +869,10 @@ extern "C" {
size_t mul = (size_t)12 + !(flg & YYJSON_READ_INSITU); size_t mul = (size_t)12 + !(flg & YYJSON_READ_INSITU);
size_t pad = 256; size_t pad = 256;
size_t max = (size_t)(~(size_t)0); size_t max = (size_t)(~(size_t)0);
if (flg & YYJSON_READ_STOP_WHEN_DONE) len = len < 256 ? 256 : len; if (flg & YYJSON_READ_STOP_WHEN_DONE)
if (len >= (max - pad - mul) / mul) return 0; len = len < 256 ? 256 : len;
if (len >= (max - pad - mul) / mul)
return 0;
return len * mul + pad; return len * mul + pad;
} }
@ -1756,10 +1758,12 @@ extern "C" {
} }
@endcode @endcode
*/ */
#define yyjson_arr_foreach(arr, idx, max, val) \ #define yyjson_arr_foreach(arr, idx, max, val) \
for ((idx) = 0, (max) = yyjson_arr_size(arr), \ for ((idx) = 0, \
(val) = yyjson_arr_get_first(arr); \ (max) = yyjson_arr_size(arr), \
(idx) < (max); (idx)++, (val) = unsafe_yyjson_get_next(val)) (val) = yyjson_arr_get_first(arr); \
(idx) < (max); \
(idx)++, (val) = unsafe_yyjson_get_next(val))
/*============================================================================== /*==============================================================================
* JSON Object API * JSON Object API
@ -1922,10 +1926,12 @@ extern "C" {
} }
@endcode @endcode
*/ */
#define yyjson_obj_foreach(obj, idx, max, key, val) \ #define yyjson_obj_foreach(obj, idx, max, key, val) \
for ((idx) = 0, (max) = yyjson_obj_size(obj), \ for ((idx) = 0, \
(key) = (obj) ? unsafe_yyjson_get_first(obj) : NULL, (val) = (key) + 1; \ (max) = yyjson_obj_size(obj), \
(idx) < (max); \ (key) = (obj) ? unsafe_yyjson_get_first(obj) : NULL, \
(val) = (key) + 1; \
(idx) < (max); \
(idx)++, (key) = unsafe_yyjson_get_next(val), (val) = (key) + 1) (idx)++, (key) = unsafe_yyjson_get_next(val), (val) = (key) + 1)
/*============================================================================== /*==============================================================================
@ -2443,10 +2449,12 @@ extern "C" {
} }
@endcode @endcode
*/ */
#define yyjson_mut_arr_foreach(arr, idx, max, val) \ #define yyjson_mut_arr_foreach(arr, idx, max, val) \
for ((idx) = 0, (max) = yyjson_mut_arr_size(arr), \ for ((idx) = 0, \
(val) = yyjson_mut_arr_get_first(arr); \ (max) = yyjson_mut_arr_size(arr), \
(idx) < (max); (idx)++, (val) = (val)->next) (val) = yyjson_mut_arr_get_first(arr); \
(idx) < (max); \
(idx)++, (val) = (val)->next)
/*============================================================================== /*==============================================================================
* Mutable JSON Array Creation API * Mutable JSON Array Creation API
@ -3357,11 +3365,13 @@ extern "C" {
} }
@endcode @endcode
*/ */
#define yyjson_mut_obj_foreach(obj, idx, max, key, val) \ #define yyjson_mut_obj_foreach(obj, idx, max, key, val) \
for ((idx) = 0, (max) = yyjson_mut_obj_size(obj), \ for ((idx) = 0, \
(key) = (max) ? ((yyjson_mut_val*)(obj)->uni.ptr)->next->next : NULL, \ (max) = yyjson_mut_obj_size(obj), \
(val) = (key) ? (key)->next : NULL; \ (key) = (max) ? ((yyjson_mut_val*)(obj)->uni.ptr)->next->next : NULL, \
(idx) < (max); (idx)++, (key) = (val)->next, (val) = (key)->next) (val) = (key) ? (key)->next : NULL; \
(idx) < (max); \
(idx)++, (key) = (val)->next, (val) = (key)->next)
/*============================================================================== /*==============================================================================
* Mutable JSON Object Creation API * Mutable JSON Object Creation API
@ -4823,7 +4833,8 @@ extern "C" {
yyjson_api_inline void yyjson_doc_free(yyjson_doc* doc) { yyjson_api_inline void yyjson_doc_free(yyjson_doc* doc) {
if (doc) { if (doc) {
yyjson_alc alc = doc->alc; yyjson_alc alc = doc->alc;
if (doc->str_pool) alc.free(alc.ctx, doc->str_pool); if (doc->str_pool)
alc.free(alc.ctx, doc->str_pool);
alc.free(alc.ctx, doc); alc.free(alc.ctx, doc);
} }
} }
@ -4985,64 +4996,76 @@ extern "C" {
yyjson_api bool unsafe_yyjson_equals(yyjson_val* lhs, yyjson_val* rhs); yyjson_api bool unsafe_yyjson_equals(yyjson_val* lhs, yyjson_val* rhs);
yyjson_api_inline bool yyjson_equals(yyjson_val* lhs, yyjson_val* rhs) { yyjson_api_inline bool yyjson_equals(yyjson_val* lhs, yyjson_val* rhs) {
if (yyjson_unlikely(!lhs || !rhs)) return false; if (yyjson_unlikely(!lhs || !rhs))
return false;
return unsafe_yyjson_equals(lhs, rhs); return unsafe_yyjson_equals(lhs, rhs);
} }
yyjson_api_inline bool yyjson_api_inline bool
yyjson_set_raw(yyjson_val* val, const char* raw, size_t len) { yyjson_set_raw(yyjson_val* val, const char* raw, size_t len) {
if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val)))
return false;
unsafe_yyjson_set_raw(val, raw, len); unsafe_yyjson_set_raw(val, raw, len);
return true; return true;
} }
yyjson_api_inline bool yyjson_set_null(yyjson_val* val) { yyjson_api_inline bool yyjson_set_null(yyjson_val* val) {
if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val)))
return false;
unsafe_yyjson_set_null(val); unsafe_yyjson_set_null(val);
return true; return true;
} }
yyjson_api_inline bool yyjson_set_bool(yyjson_val* val, bool num) { yyjson_api_inline bool yyjson_set_bool(yyjson_val* val, bool num) {
if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val)))
return false;
unsafe_yyjson_set_bool(val, num); unsafe_yyjson_set_bool(val, num);
return true; return true;
} }
yyjson_api_inline bool yyjson_set_uint(yyjson_val* val, uint64_t num) { yyjson_api_inline bool yyjson_set_uint(yyjson_val* val, uint64_t num) {
if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val)))
return false;
unsafe_yyjson_set_uint(val, num); unsafe_yyjson_set_uint(val, num);
return true; return true;
} }
yyjson_api_inline bool yyjson_set_sint(yyjson_val* val, int64_t num) { yyjson_api_inline bool yyjson_set_sint(yyjson_val* val, int64_t num) {
if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val)))
return false;
unsafe_yyjson_set_sint(val, num); unsafe_yyjson_set_sint(val, num);
return true; return true;
} }
yyjson_api_inline bool yyjson_set_int(yyjson_val* val, int num) { yyjson_api_inline bool yyjson_set_int(yyjson_val* val, int num) {
if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val)))
return false;
unsafe_yyjson_set_sint(val, (int64_t)num); unsafe_yyjson_set_sint(val, (int64_t)num);
return true; return true;
} }
yyjson_api_inline bool yyjson_set_real(yyjson_val* val, double num) { yyjson_api_inline bool yyjson_set_real(yyjson_val* val, double num) {
if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val)))
return false;
unsafe_yyjson_set_real(val, num); unsafe_yyjson_set_real(val, num);
return true; return true;
} }
yyjson_api_inline bool yyjson_set_str(yyjson_val* val, const char* str) { yyjson_api_inline bool yyjson_set_str(yyjson_val* val, const char* str) {
if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val)))
if (yyjson_unlikely(!str)) return false; return false;
if (yyjson_unlikely(!str))
return false;
unsafe_yyjson_set_str(val, str); unsafe_yyjson_set_str(val, str);
return true; return true;
} }
yyjson_api_inline bool yyjson_api_inline bool
yyjson_set_strn(yyjson_val* val, const char* str, size_t len) { yyjson_set_strn(yyjson_val* val, const char* str, size_t len) {
if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val)))
if (yyjson_unlikely(!str)) return false; return false;
if (yyjson_unlikely(!str))
return false;
unsafe_yyjson_set_strn(val, str, len); unsafe_yyjson_set_strn(val, str, len);
return true; return true;
} }
@ -5107,7 +5130,8 @@ extern "C" {
iter->cur = unsafe_yyjson_get_first(arr); iter->cur = unsafe_yyjson_get_first(arr);
return true; return true;
} }
if (iter) memset(iter, 0, sizeof(yyjson_arr_iter)); if (iter)
memset(iter, 0, sizeof(yyjson_arr_iter));
return false; return false;
} }
@ -5174,7 +5198,8 @@ extern "C" {
iter->obj = obj; iter->obj = obj;
return true; return true;
} }
if (iter) memset(iter, 0, sizeof(yyjson_obj_iter)); if (iter)
memset(iter, 0, sizeof(yyjson_obj_iter));
return false; return false;
} }
@ -5333,7 +5358,8 @@ extern "C" {
yyjson_api_inline char* yyjson_api_inline char*
unsafe_yyjson_mut_strncpy(yyjson_mut_doc* doc, const char* str, size_t len) { unsafe_yyjson_mut_strncpy(yyjson_mut_doc* doc, const char* str, size_t len) {
char* mem = unsafe_yyjson_mut_str_alc(doc, len); char* mem = unsafe_yyjson_mut_str_alc(doc, len);
if (yyjson_unlikely(!mem)) return NULL; if (yyjson_unlikely(!mem))
return NULL;
memcpy((void*)mem, (const void*)str, len); memcpy((void*)mem, (const void*)str, len);
mem[len] = '\0'; mem[len] = '\0';
return mem; return mem;
@ -5365,7 +5391,8 @@ extern "C" {
yyjson_api_inline void yyjson_api_inline void
yyjson_mut_doc_set_root(yyjson_mut_doc* doc, yyjson_mut_val* root) { yyjson_mut_doc_set_root(yyjson_mut_doc* doc, yyjson_mut_val* root) {
if (doc) doc->root = root; if (doc)
doc->root = root;
} }
/*============================================================================== /*==============================================================================
@ -5499,76 +5526,88 @@ extern "C" {
yyjson_api_inline bool yyjson_api_inline bool
yyjson_mut_equals(yyjson_mut_val* lhs, yyjson_mut_val* rhs) { yyjson_mut_equals(yyjson_mut_val* lhs, yyjson_mut_val* rhs) {
if (yyjson_unlikely(!lhs || !rhs)) return false; if (yyjson_unlikely(!lhs || !rhs))
return false;
return unsafe_yyjson_mut_equals(lhs, rhs); return unsafe_yyjson_mut_equals(lhs, rhs);
} }
yyjson_api_inline bool yyjson_api_inline bool
yyjson_mut_set_raw(yyjson_mut_val* val, const char* raw, size_t len) { yyjson_mut_set_raw(yyjson_mut_val* val, const char* raw, size_t len) {
if (yyjson_unlikely(!val || !raw)) return false; if (yyjson_unlikely(!val || !raw))
return false;
unsafe_yyjson_set_raw(val, raw, len); unsafe_yyjson_set_raw(val, raw, len);
return true; return true;
} }
yyjson_api_inline bool yyjson_mut_set_null(yyjson_mut_val* val) { yyjson_api_inline bool yyjson_mut_set_null(yyjson_mut_val* val) {
if (yyjson_unlikely(!val)) return false; if (yyjson_unlikely(!val))
return false;
unsafe_yyjson_set_null(val); unsafe_yyjson_set_null(val);
return true; return true;
} }
yyjson_api_inline bool yyjson_mut_set_bool(yyjson_mut_val* val, bool num) { yyjson_api_inline bool yyjson_mut_set_bool(yyjson_mut_val* val, bool num) {
if (yyjson_unlikely(!val)) return false; if (yyjson_unlikely(!val))
return false;
unsafe_yyjson_set_bool(val, num); unsafe_yyjson_set_bool(val, num);
return true; return true;
} }
yyjson_api_inline bool yyjson_api_inline bool
yyjson_mut_set_uint(yyjson_mut_val* val, uint64_t num) { yyjson_mut_set_uint(yyjson_mut_val* val, uint64_t num) {
if (yyjson_unlikely(!val)) return false; if (yyjson_unlikely(!val))
return false;
unsafe_yyjson_set_uint(val, num); unsafe_yyjson_set_uint(val, num);
return true; return true;
} }
yyjson_api_inline bool yyjson_mut_set_sint(yyjson_mut_val* val, int64_t num) { yyjson_api_inline bool yyjson_mut_set_sint(yyjson_mut_val* val, int64_t num) {
if (yyjson_unlikely(!val)) return false; if (yyjson_unlikely(!val))
return false;
unsafe_yyjson_set_sint(val, num); unsafe_yyjson_set_sint(val, num);
return true; return true;
} }
yyjson_api_inline bool yyjson_mut_set_int(yyjson_mut_val* val, int num) { yyjson_api_inline bool yyjson_mut_set_int(yyjson_mut_val* val, int num) {
if (yyjson_unlikely(!val)) return false; if (yyjson_unlikely(!val))
return false;
unsafe_yyjson_set_sint(val, (int64_t)num); unsafe_yyjson_set_sint(val, (int64_t)num);
return true; return true;
} }
yyjson_api_inline bool yyjson_mut_set_real(yyjson_mut_val* val, double num) { yyjson_api_inline bool yyjson_mut_set_real(yyjson_mut_val* val, double num) {
if (yyjson_unlikely(!val)) return false; if (yyjson_unlikely(!val))
return false;
unsafe_yyjson_set_real(val, num); unsafe_yyjson_set_real(val, num);
return true; return true;
} }
yyjson_api_inline bool yyjson_api_inline bool
yyjson_mut_set_str(yyjson_mut_val* val, const char* str) { yyjson_mut_set_str(yyjson_mut_val* val, const char* str) {
if (yyjson_unlikely(!val || !str)) return false; if (yyjson_unlikely(!val || !str))
return false;
unsafe_yyjson_set_str(val, str); unsafe_yyjson_set_str(val, str);
return true; return true;
} }
yyjson_api_inline bool yyjson_api_inline bool
yyjson_mut_set_strn(yyjson_mut_val* val, const char* str, size_t len) { yyjson_mut_set_strn(yyjson_mut_val* val, const char* str, size_t len) {
if (yyjson_unlikely(!val || !str)) return false; if (yyjson_unlikely(!val || !str))
return false;
unsafe_yyjson_set_strn(val, str, len); unsafe_yyjson_set_strn(val, str, len);
return true; return true;
} }
yyjson_api_inline bool yyjson_mut_set_arr(yyjson_mut_val* val) { yyjson_api_inline bool yyjson_mut_set_arr(yyjson_mut_val* val) {
if (yyjson_unlikely(!val)) return false; if (yyjson_unlikely(!val))
return false;
unsafe_yyjson_set_arr(val, 0); unsafe_yyjson_set_arr(val, 0);
return true; return true;
} }
yyjson_api_inline bool yyjson_mut_set_obj(yyjson_mut_val* val) { yyjson_api_inline bool yyjson_mut_set_obj(yyjson_mut_val* val) {
if (yyjson_unlikely(!val)) return false; if (yyjson_unlikely(!val))
return false;
unsafe_yyjson_set_obj(val, 0); unsafe_yyjson_set_obj(val, 0);
return true; return true;
} }
@ -5579,7 +5618,8 @@ extern "C" {
yyjson_api_inline yyjson_mut_val* yyjson_api_inline yyjson_mut_val*
yyjson_mut_raw(yyjson_mut_doc* doc, const char* str) { yyjson_mut_raw(yyjson_mut_doc* doc, const char* str) {
if (yyjson_likely(str)) return yyjson_mut_rawn(doc, str, strlen(str)); if (yyjson_likely(str))
return yyjson_mut_rawn(doc, str, strlen(str));
return NULL; return NULL;
} }
@ -5598,7 +5638,8 @@ extern "C" {
yyjson_api_inline yyjson_mut_val* yyjson_api_inline yyjson_mut_val*
yyjson_mut_rawcpy(yyjson_mut_doc* doc, const char* str) { yyjson_mut_rawcpy(yyjson_mut_doc* doc, const char* str) {
if (yyjson_likely(str)) return yyjson_mut_rawncpy(doc, str, strlen(str)); if (yyjson_likely(str))
return yyjson_mut_rawncpy(doc, str, strlen(str));
return NULL; return NULL;
} }
@ -5707,7 +5748,8 @@ extern "C" {
yyjson_api_inline yyjson_mut_val* yyjson_api_inline yyjson_mut_val*
yyjson_mut_str(yyjson_mut_doc* doc, const char* str) { yyjson_mut_str(yyjson_mut_doc* doc, const char* str) {
if (yyjson_likely(str)) return yyjson_mut_strn(doc, str, strlen(str)); if (yyjson_likely(str))
return yyjson_mut_strn(doc, str, strlen(str));
return NULL; return NULL;
} }
@ -5726,7 +5768,8 @@ extern "C" {
yyjson_api_inline yyjson_mut_val* yyjson_api_inline yyjson_mut_val*
yyjson_mut_strcpy(yyjson_mut_doc* doc, const char* str) { yyjson_mut_strcpy(yyjson_mut_doc* doc, const char* str) {
if (yyjson_likely(str)) return yyjson_mut_strncpy(doc, str, strlen(str)); if (yyjson_likely(str))
return yyjson_mut_strncpy(doc, str, strlen(str));
return NULL; return NULL;
} }
@ -5792,7 +5835,8 @@ extern "C" {
iter->arr = arr; iter->arr = arr;
return true; return true;
} }
if (iter) memset(iter, 0, sizeof(yyjson_mut_arr_iter)); if (iter)
memset(iter, 0, sizeof(yyjson_mut_arr_iter));
return false; return false;
} }
@ -5828,7 +5872,8 @@ extern "C" {
yyjson_mut_val* prev = iter->pre; yyjson_mut_val* prev = iter->pre;
yyjson_mut_val* cur = iter->cur; yyjson_mut_val* cur = iter->cur;
yyjson_mut_val* next = cur->next; yyjson_mut_val* next = cur->next;
if (yyjson_unlikely(iter->idx == iter->max)) iter->arr->uni.ptr = prev; if (yyjson_unlikely(iter->idx == iter->max))
iter->arr->uni.ptr = prev;
iter->idx--; iter->idx--;
iter->max--; iter->max--;
unsafe_yyjson_set_len(iter->arr, iter->max); unsafe_yyjson_set_len(iter->arr, iter->max);
@ -6030,7 +6075,8 @@ extern "C" {
uint64_t len = (uint64_t)strlen(vals[i]); uint64_t len = (uint64_t)strlen(vals[i]);
val->tag = (len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->tag = (len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->uni.str = vals[i]; val->uni.str = vals[i];
if (yyjson_unlikely(!val->uni.str)) return NULL; if (yyjson_unlikely(!val->uni.str))
return NULL;
}); });
} }
@ -6040,11 +6086,13 @@ extern "C" {
const size_t* lens, const size_t* lens,
size_t count size_t count
) { ) {
if (yyjson_unlikely(count > 0 && !lens)) return NULL; if (yyjson_unlikely(count > 0 && !lens))
return NULL;
yyjson_mut_arr_with_func({ yyjson_mut_arr_with_func({
val->tag = ((uint64_t)lens[i] << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->tag = ((uint64_t)lens[i] << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->uni.str = vals[i]; val->uni.str = vals[i];
if (yyjson_unlikely(!val->uni.str)) return NULL; if (yyjson_unlikely(!val->uni.str))
return NULL;
}); });
} }
@ -6057,11 +6105,13 @@ extern "C" {
const char* str; const char* str;
yyjson_mut_arr_with_func({ yyjson_mut_arr_with_func({
str = vals[i]; str = vals[i];
if (!str) return NULL; if (!str)
return NULL;
len = strlen(str); len = strlen(str);
val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->uni.str = unsafe_yyjson_mut_strncpy(doc, str, len); val->uni.str = unsafe_yyjson_mut_strncpy(doc, str, len);
if (yyjson_unlikely(!val->uni.str)) return NULL; if (yyjson_unlikely(!val->uni.str))
return NULL;
}); });
} }
@ -6073,13 +6123,15 @@ extern "C" {
) { ) {
size_t len; size_t len;
const char* str; const char* str;
if (yyjson_unlikely(count > 0 && !lens)) return NULL; if (yyjson_unlikely(count > 0 && !lens))
return NULL;
yyjson_mut_arr_with_func({ yyjson_mut_arr_with_func({
str = vals[i]; str = vals[i];
len = lens[i]; len = lens[i];
val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->uni.str = unsafe_yyjson_mut_strncpy(doc, str, len); val->uni.str = unsafe_yyjson_mut_strncpy(doc, str, len);
if (yyjson_unlikely(!val->uni.str)) return NULL; if (yyjson_unlikely(!val->uni.str))
return NULL;
}); });
} }
@ -6172,7 +6224,8 @@ extern "C" {
} }
prev->next = val; prev->next = val;
val->next = next->next; val->next = next->next;
if ((void*)next == arr->uni.ptr) arr->uni.ptr = val; if ((void*)next == arr->uni.ptr)
arr->uni.ptr = val;
return next; return next;
} else { } else {
yyjson_mut_val* prev = ((yyjson_mut_val*)arr->uni.ptr); yyjson_mut_val* prev = ((yyjson_mut_val*)arr->uni.ptr);
@ -6199,7 +6252,8 @@ extern "C" {
next = next->next; next = next->next;
} }
prev->next = next->next; prev->next = next->next;
if ((void*)next == arr->uni.ptr) arr->uni.ptr = prev; if ((void*)next == arr->uni.ptr)
arr->uni.ptr = prev;
return next; return next;
} else { } else {
return ((yyjson_mut_val*)arr->uni.ptr); return ((yyjson_mut_val*)arr->uni.ptr);
@ -6258,17 +6312,21 @@ extern "C" {
yyjson_mut_val *prev, *next; yyjson_mut_val *prev, *next;
bool tail_removed; bool tail_removed;
size_t len = unsafe_yyjson_get_len(arr); size_t len = unsafe_yyjson_get_len(arr);
if (yyjson_unlikely(_idx + _len > len)) return false; if (yyjson_unlikely(_idx + _len > len))
if (yyjson_unlikely(_len == 0)) return true; return false;
if (yyjson_unlikely(_len == 0))
return true;
unsafe_yyjson_set_len(arr, len - _len); unsafe_yyjson_set_len(arr, len - _len);
if (yyjson_unlikely(len == _len)) return true; if (yyjson_unlikely(len == _len))
return true;
tail_removed = (_idx + _len == len); tail_removed = (_idx + _len == len);
prev = ((yyjson_mut_val*)arr->uni.ptr); prev = ((yyjson_mut_val*)arr->uni.ptr);
while (_idx-- > 0) prev = prev->next; while (_idx-- > 0) prev = prev->next;
next = prev->next; next = prev->next;
while (_len-- > 0) next = next->next; while (_len-- > 0) next = next->next;
prev->next = next; prev->next = next;
if (yyjson_unlikely(tail_removed)) arr->uni.ptr = prev; if (yyjson_unlikely(tail_removed))
arr->uni.ptr = prev;
return true; return true;
} }
return false; return false;
@ -6499,7 +6557,8 @@ extern "C" {
iter->obj = obj; iter->obj = obj;
return true; return true;
} }
if (iter) memset(iter, 0, sizeof(yyjson_mut_obj_iter)); if (iter)
memset(iter, 0, sizeof(yyjson_mut_obj_iter));
return false; return false;
} }
@ -6541,7 +6600,8 @@ extern "C" {
yyjson_mut_val* prev = iter->pre; yyjson_mut_val* prev = iter->pre;
yyjson_mut_val* cur = iter->cur; yyjson_mut_val* cur = iter->cur;
yyjson_mut_val* next = cur->next->next; yyjson_mut_val* next = cur->next->next;
if (yyjson_unlikely(iter->idx == iter->max)) iter->obj->uni.ptr = prev; if (yyjson_unlikely(iter->idx == iter->max))
iter->obj->uni.ptr = prev;
iter->idx--; iter->idx--;
iter->max--; iter->max--;
unsafe_yyjson_set_len(iter->obj, iter->max); unsafe_yyjson_set_len(iter->obj, iter->max);
@ -6572,7 +6632,8 @@ extern "C" {
if (unsafe_yyjson_get_len(cur) == key_len && if (unsafe_yyjson_get_len(cur) == key_len &&
memcmp(cur->uni.str, key, key_len) == 0) { memcmp(cur->uni.str, key, key_len) == 0) {
iter->idx += idx; iter->idx += idx;
if (iter->idx > max) iter->idx -= max + 1; if (iter->idx > max)
iter->idx -= max + 1;
iter->pre = pre; iter->pre = pre;
iter->cur = cur; iter->cur = cur;
return cur->next; return cur->next;
@ -6702,10 +6763,12 @@ extern "C" {
for (i = 0; i < obj_len; i++) { for (i = 0; i < obj_len; i++) {
if (key_tag == cur_key->tag && if (key_tag == cur_key->tag &&
memcmp(key, cur_key->uni.ptr, key_len) == 0) { memcmp(key, cur_key->uni.ptr, key_len) == 0) {
if (!removed_item) removed_item = cur_key->next; if (!removed_item)
removed_item = cur_key->next;
cur_key = cur_key->next->next; cur_key = cur_key->next->next;
pre_key->next->next = cur_key; pre_key->next->next = cur_key;
if (i + 1 == obj_len) obj->uni.ptr = pre_key; if (i + 1 == obj_len)
obj->uni.ptr = pre_key;
i--; i--;
obj_len--; obj_len--;
} else { } else {
@ -6791,7 +6854,8 @@ extern "C" {
} }
} }
} }
if (!replaced && val) unsafe_yyjson_mut_obj_add(obj, key, val, iter.max); if (!replaced && val)
unsafe_yyjson_mut_obj_add(obj, key, val, iter.max);
return true; return true;
} }
@ -6998,7 +7062,8 @@ extern "C" {
const char* _key, const char* _key,
const char* _val const char* _val
) { ) {
if (yyjson_unlikely(!_val)) return false; if (yyjson_unlikely(!_val))
return false;
yyjson_mut_obj_add_func({ yyjson_mut_obj_add_func({
val->tag = ((uint64_t)strlen(_val) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->tag = ((uint64_t)strlen(_val) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->uni.str = _val; val->uni.str = _val;
@ -7012,7 +7077,8 @@ extern "C" {
const char* _val, const char* _val,
size_t _len size_t _len
) { ) {
if (yyjson_unlikely(!_val)) return false; if (yyjson_unlikely(!_val))
return false;
yyjson_mut_obj_add_func({ yyjson_mut_obj_add_func({
val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->uni.str = _val; val->uni.str = _val;
@ -7025,11 +7091,13 @@ extern "C" {
const char* _key, const char* _key,
const char* _val const char* _val
) { ) {
if (yyjson_unlikely(!_val)) return false; if (yyjson_unlikely(!_val))
return false;
yyjson_mut_obj_add_func({ yyjson_mut_obj_add_func({
size_t _len = strlen(_val); size_t _len = strlen(_val);
val->uni.str = unsafe_yyjson_mut_strncpy(doc, _val, _len); val->uni.str = unsafe_yyjson_mut_strncpy(doc, _val, _len);
if (yyjson_unlikely(!val->uni.str)) return false; if (yyjson_unlikely(!val->uni.str))
return false;
val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
}); });
} }
@ -7041,10 +7109,12 @@ extern "C" {
const char* _val, const char* _val,
size_t _len size_t _len
) { ) {
if (yyjson_unlikely(!_val)) return false; if (yyjson_unlikely(!_val))
return false;
yyjson_mut_obj_add_func({ yyjson_mut_obj_add_func({
val->uni.str = unsafe_yyjson_mut_strncpy(doc, _val, _len); val->uni.str = unsafe_yyjson_mut_strncpy(doc, _val, _len);
if (yyjson_unlikely(!val->uni.str)) return false; if (yyjson_unlikely(!val->uni.str))
return false;
val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
}); });
} }
@ -7055,7 +7125,8 @@ extern "C" {
const char* _key, const char* _key,
yyjson_mut_val* _val yyjson_mut_val* _val
) { ) {
if (yyjson_unlikely(!_val)) return false; if (yyjson_unlikely(!_val))
return false;
yyjson_mut_obj_add_func({ val = _val; }); yyjson_mut_obj_add_func({ val = _val; });
} }
@ -7077,7 +7148,8 @@ extern "C" {
while ((key = yyjson_mut_obj_iter_next(&iter)) != NULL) { while ((key = yyjson_mut_obj_iter_next(&iter)) != NULL) {
if (unsafe_yyjson_get_len(key) == _len && if (unsafe_yyjson_get_len(key) == _len &&
memcmp(key->uni.str, _key, _len) == 0) { memcmp(key->uni.str, _key, _len) == 0) {
if (!val_removed) val_removed = key->next; if (!val_removed)
val_removed = key->next;
yyjson_mut_obj_iter_remove(&iter); yyjson_mut_obj_iter_remove(&iter);
} }
} }
@ -7092,7 +7164,8 @@ extern "C" {
const char* key, const char* key,
const char* new_key const char* new_key
) { ) {
if (!key || !new_key) return false; if (!key || !new_key)
return false;
return yyjson_mut_obj_rename_keyn( return yyjson_mut_obj_rename_keyn(
doc, obj, key, strlen(key), new_key, strlen(new_key) doc, obj, key, strlen(key), new_key, strlen(new_key)
); );
@ -7109,13 +7182,15 @@ extern "C" {
char* cpy_key = NULL; char* cpy_key = NULL;
yyjson_mut_val* old_key; yyjson_mut_val* old_key;
yyjson_mut_obj_iter iter; yyjson_mut_obj_iter iter;
if (!doc || !obj || !key || !new_key) return false; if (!doc || !obj || !key || !new_key)
return false;
yyjson_mut_obj_iter_init(obj, &iter); yyjson_mut_obj_iter_init(obj, &iter);
while ((old_key = yyjson_mut_obj_iter_next(&iter))) { while ((old_key = yyjson_mut_obj_iter_next(&iter))) {
if (unsafe_yyjson_equals_strn((void*)old_key, key, len)) { if (unsafe_yyjson_equals_strn((void*)old_key, key, len)) {
if (!cpy_key) { if (!cpy_key) {
cpy_key = unsafe_yyjson_mut_strncpy(doc, new_key, new_len); cpy_key = unsafe_yyjson_mut_strncpy(doc, new_key, new_len);
if (!cpy_key) return false; if (!cpy_key)
return false;
} }
yyjson_mut_set_strn(old_key, cpy_key, new_len); yyjson_mut_set_strn(old_key, cpy_key, new_len);
} }
@ -7187,7 +7262,8 @@ extern "C" {
yyjson_api_inline yyjson_val* yyjson_api_inline yyjson_val*
yyjson_doc_ptr_get(yyjson_doc* doc, const char* ptr) { yyjson_doc_ptr_get(yyjson_doc* doc, const char* ptr) {
if (yyjson_unlikely(!ptr)) return NULL; if (yyjson_unlikely(!ptr))
return NULL;
return yyjson_doc_ptr_getn(doc, ptr, strlen(ptr)); return yyjson_doc_ptr_getn(doc, ptr, strlen(ptr));
} }
@ -7211,7 +7287,9 @@ extern "C" {
yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL");
return NULL; return NULL;
} }
if (yyjson_unlikely(len == 0)) { return doc->root; } if (yyjson_unlikely(len == 0)) {
return doc->root;
}
if (yyjson_unlikely(*ptr != '/')) { if (yyjson_unlikely(*ptr != '/')) {
yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); yyjson_ptr_set_err(SYNTAX, "no prefix '/'");
return NULL; return NULL;
@ -7221,7 +7299,8 @@ extern "C" {
yyjson_api_inline yyjson_val* yyjson_api_inline yyjson_val*
yyjson_ptr_get(yyjson_val* val, const char* ptr) { yyjson_ptr_get(yyjson_val* val, const char* ptr) {
if (yyjson_unlikely(!ptr)) return NULL; if (yyjson_unlikely(!ptr))
return NULL;
return yyjson_ptr_getn(val, ptr, strlen(ptr)); return yyjson_ptr_getn(val, ptr, strlen(ptr));
} }
@ -7241,7 +7320,9 @@ extern "C" {
yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); yyjson_ptr_set_err(PARAMETER, "input parameter is NULL");
return NULL; return NULL;
} }
if (yyjson_unlikely(len == 0)) { return val; } if (yyjson_unlikely(len == 0)) {
return val;
}
if (yyjson_unlikely(*ptr != '/')) { if (yyjson_unlikely(*ptr != '/')) {
yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); yyjson_ptr_set_err(SYNTAX, "no prefix '/'");
return NULL; return NULL;
@ -7251,7 +7332,8 @@ extern "C" {
yyjson_api_inline yyjson_mut_val* yyjson_api_inline yyjson_mut_val*
yyjson_mut_doc_ptr_get(yyjson_mut_doc* doc, const char* ptr) { yyjson_mut_doc_ptr_get(yyjson_mut_doc* doc, const char* ptr) {
if (!ptr) return NULL; if (!ptr)
return NULL;
return yyjson_mut_doc_ptr_getn(doc, ptr, strlen(ptr)); return yyjson_mut_doc_ptr_getn(doc, ptr, strlen(ptr));
} }
@ -7268,7 +7350,8 @@ extern "C" {
yyjson_ptr_err* err yyjson_ptr_err* err
) { ) {
yyjson_ptr_set_err(NONE, NULL); yyjson_ptr_set_err(NONE, NULL);
if (ctx) memset(ctx, 0, sizeof(*ctx)); if (ctx)
memset(ctx, 0, sizeof(*ctx));
if (yyjson_unlikely(!doc || !ptr)) { if (yyjson_unlikely(!doc || !ptr)) {
yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); yyjson_ptr_set_err(PARAMETER, "input parameter is NULL");
@ -7278,7 +7361,9 @@ extern "C" {
yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL");
return NULL; return NULL;
} }
if (yyjson_unlikely(len == 0)) { return doc->root; } if (yyjson_unlikely(len == 0)) {
return doc->root;
}
if (yyjson_unlikely(*ptr != '/')) { if (yyjson_unlikely(*ptr != '/')) {
yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); yyjson_ptr_set_err(SYNTAX, "no prefix '/'");
return NULL; return NULL;
@ -7288,7 +7373,8 @@ extern "C" {
yyjson_api_inline yyjson_mut_val* yyjson_api_inline yyjson_mut_val*
yyjson_mut_ptr_get(yyjson_mut_val* val, const char* ptr) { yyjson_mut_ptr_get(yyjson_mut_val* val, const char* ptr) {
if (!ptr) return NULL; if (!ptr)
return NULL;
return yyjson_mut_ptr_getn(val, ptr, strlen(ptr)); return yyjson_mut_ptr_getn(val, ptr, strlen(ptr));
} }
@ -7305,13 +7391,16 @@ extern "C" {
yyjson_ptr_err* err yyjson_ptr_err* err
) { ) {
yyjson_ptr_set_err(NONE, NULL); yyjson_ptr_set_err(NONE, NULL);
if (ctx) memset(ctx, 0, sizeof(*ctx)); if (ctx)
memset(ctx, 0, sizeof(*ctx));
if (yyjson_unlikely(!val || !ptr)) { if (yyjson_unlikely(!val || !ptr)) {
yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); yyjson_ptr_set_err(PARAMETER, "input parameter is NULL");
return NULL; return NULL;
} }
if (yyjson_unlikely(len == 0)) { return val; } if (yyjson_unlikely(len == 0)) {
return val;
}
if (yyjson_unlikely(*ptr != '/')) { if (yyjson_unlikely(*ptr != '/')) {
yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); yyjson_ptr_set_err(SYNTAX, "no prefix '/'");
return NULL; return NULL;
@ -7324,7 +7413,8 @@ extern "C" {
const char* ptr, const char* ptr,
yyjson_mut_val* new_val yyjson_mut_val* new_val
) { ) {
if (yyjson_unlikely(!ptr)) return false; if (yyjson_unlikely(!ptr))
return false;
return yyjson_mut_doc_ptr_addn(doc, ptr, strlen(ptr), new_val); return yyjson_mut_doc_ptr_addn(doc, ptr, strlen(ptr), new_val);
} }
@ -7347,7 +7437,8 @@ extern "C" {
yyjson_ptr_err* err yyjson_ptr_err* err
) { ) {
yyjson_ptr_set_err(NONE, NULL); yyjson_ptr_set_err(NONE, NULL);
if (ctx) memset(ctx, 0, sizeof(*ctx)); if (ctx)
memset(ctx, 0, sizeof(*ctx));
if (yyjson_unlikely(!doc || !ptr || !new_val)) { if (yyjson_unlikely(!doc || !ptr || !new_val)) {
yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); yyjson_ptr_set_err(PARAMETER, "input parameter is NULL");
@ -7395,7 +7486,8 @@ extern "C" {
yyjson_mut_val* new_val, yyjson_mut_val* new_val,
yyjson_mut_doc* doc yyjson_mut_doc* doc
) { ) {
if (yyjson_unlikely(!ptr)) return false; if (yyjson_unlikely(!ptr))
return false;
return yyjson_mut_ptr_addn(val, ptr, strlen(ptr), new_val, doc); return yyjson_mut_ptr_addn(val, ptr, strlen(ptr), new_val, doc);
} }
@ -7420,7 +7512,8 @@ extern "C" {
yyjson_ptr_err* err yyjson_ptr_err* err
) { ) {
yyjson_ptr_set_err(NONE, NULL); yyjson_ptr_set_err(NONE, NULL);
if (ctx) memset(ctx, 0, sizeof(*ctx)); if (ctx)
memset(ctx, 0, sizeof(*ctx));
if (yyjson_unlikely(!val || !ptr || !new_val || !doc)) { if (yyjson_unlikely(!val || !ptr || !new_val || !doc)) {
yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); yyjson_ptr_set_err(PARAMETER, "input parameter is NULL");
@ -7444,7 +7537,8 @@ extern "C" {
const char* ptr, const char* ptr,
yyjson_mut_val* new_val yyjson_mut_val* new_val
) { ) {
if (yyjson_unlikely(!ptr)) return false; if (yyjson_unlikely(!ptr))
return false;
return yyjson_mut_doc_ptr_setn(doc, ptr, strlen(ptr), new_val); return yyjson_mut_doc_ptr_setn(doc, ptr, strlen(ptr), new_val);
} }
@ -7467,14 +7561,16 @@ extern "C" {
yyjson_ptr_err* err yyjson_ptr_err* err
) { ) {
yyjson_ptr_set_err(NONE, NULL); yyjson_ptr_set_err(NONE, NULL);
if (ctx) memset(ctx, 0, sizeof(*ctx)); if (ctx)
memset(ctx, 0, sizeof(*ctx));
if (yyjson_unlikely(!doc || !ptr)) { if (yyjson_unlikely(!doc || !ptr)) {
yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); yyjson_ptr_set_err(PARAMETER, "input parameter is NULL");
return false; return false;
} }
if (yyjson_unlikely(len == 0)) { if (yyjson_unlikely(len == 0)) {
if (ctx) ctx->old = doc->root; if (ctx)
ctx->old = doc->root;
doc->root = new_val; doc->root = new_val;
return true; return true;
} }
@ -7518,7 +7614,8 @@ extern "C" {
yyjson_mut_val* new_val, yyjson_mut_val* new_val,
yyjson_mut_doc* doc yyjson_mut_doc* doc
) { ) {
if (yyjson_unlikely(!ptr)) return false; if (yyjson_unlikely(!ptr))
return false;
return yyjson_mut_ptr_setn(val, ptr, strlen(ptr), new_val, doc); return yyjson_mut_ptr_setn(val, ptr, strlen(ptr), new_val, doc);
} }
@ -7543,7 +7640,8 @@ extern "C" {
yyjson_ptr_err* err yyjson_ptr_err* err
) { ) {
yyjson_ptr_set_err(NONE, NULL); yyjson_ptr_set_err(NONE, NULL);
if (ctx) memset(ctx, 0, sizeof(*ctx)); if (ctx)
memset(ctx, 0, sizeof(*ctx));
if (yyjson_unlikely(!val || !ptr || !doc)) { if (yyjson_unlikely(!val || !ptr || !doc)) {
yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); yyjson_ptr_set_err(PARAMETER, "input parameter is NULL");
@ -7570,7 +7668,8 @@ extern "C" {
const char* ptr, const char* ptr,
yyjson_mut_val* new_val yyjson_mut_val* new_val
) { ) {
if (!ptr) return NULL; if (!ptr)
return NULL;
return yyjson_mut_doc_ptr_replacen(doc, ptr, strlen(ptr), new_val); return yyjson_mut_doc_ptr_replacen(doc, ptr, strlen(ptr), new_val);
} }
@ -7592,7 +7691,8 @@ extern "C" {
yyjson_ptr_err* err yyjson_ptr_err* err
) { ) {
yyjson_ptr_set_err(NONE, NULL); yyjson_ptr_set_err(NONE, NULL);
if (ctx) memset(ctx, 0, sizeof(*ctx)); if (ctx)
memset(ctx, 0, sizeof(*ctx));
if (yyjson_unlikely(!doc || !ptr || !new_val)) { if (yyjson_unlikely(!doc || !ptr || !new_val)) {
yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); yyjson_ptr_set_err(PARAMETER, "input parameter is NULL");
@ -7604,7 +7704,8 @@ extern "C" {
yyjson_ptr_set_err(RESOLVE, "JSON pointer cannot be resolved"); yyjson_ptr_set_err(RESOLVE, "JSON pointer cannot be resolved");
return NULL; return NULL;
} }
if (ctx) ctx->old = root; if (ctx)
ctx->old = root;
doc->root = new_val; doc->root = new_val;
return root; return root;
} }
@ -7626,7 +7727,8 @@ extern "C" {
const char* ptr, const char* ptr,
yyjson_mut_val* new_val yyjson_mut_val* new_val
) { ) {
if (!ptr) return NULL; if (!ptr)
return NULL;
return yyjson_mut_ptr_replacen(val, ptr, strlen(ptr), new_val); return yyjson_mut_ptr_replacen(val, ptr, strlen(ptr), new_val);
} }
@ -7648,7 +7750,8 @@ extern "C" {
yyjson_ptr_err* err yyjson_ptr_err* err
) { ) {
yyjson_ptr_set_err(NONE, NULL); yyjson_ptr_set_err(NONE, NULL);
if (ctx) memset(ctx, 0, sizeof(*ctx)); if (ctx)
memset(ctx, 0, sizeof(*ctx));
if (yyjson_unlikely(!val || !ptr || !new_val)) { if (yyjson_unlikely(!val || !ptr || !new_val)) {
yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); yyjson_ptr_set_err(PARAMETER, "input parameter is NULL");
@ -7667,7 +7770,8 @@ extern "C" {
yyjson_api_inline yyjson_mut_val* yyjson_api_inline yyjson_mut_val*
yyjson_mut_doc_ptr_remove(yyjson_mut_doc* doc, const char* ptr) { yyjson_mut_doc_ptr_remove(yyjson_mut_doc* doc, const char* ptr) {
if (!ptr) return NULL; if (!ptr)
return NULL;
return yyjson_mut_doc_ptr_removen(doc, ptr, strlen(ptr)); return yyjson_mut_doc_ptr_removen(doc, ptr, strlen(ptr));
} }
@ -7684,7 +7788,8 @@ extern "C" {
yyjson_ptr_err* err yyjson_ptr_err* err
) { ) {
yyjson_ptr_set_err(NONE, NULL); yyjson_ptr_set_err(NONE, NULL);
if (ctx) memset(ctx, 0, sizeof(*ctx)); if (ctx)
memset(ctx, 0, sizeof(*ctx));
if (yyjson_unlikely(!doc || !ptr)) { if (yyjson_unlikely(!doc || !ptr)) {
yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); yyjson_ptr_set_err(PARAMETER, "input parameter is NULL");
@ -7696,7 +7801,8 @@ extern "C" {
} }
if (yyjson_unlikely(len == 0)) { if (yyjson_unlikely(len == 0)) {
yyjson_mut_val* root = doc->root; yyjson_mut_val* root = doc->root;
if (ctx) ctx->old = root; if (ctx)
ctx->old = root;
doc->root = NULL; doc->root = NULL;
return root; return root;
} }
@ -7709,7 +7815,8 @@ extern "C" {
yyjson_api_inline yyjson_mut_val* yyjson_api_inline yyjson_mut_val*
yyjson_mut_ptr_remove(yyjson_mut_val* val, const char* ptr) { yyjson_mut_ptr_remove(yyjson_mut_val* val, const char* ptr) {
if (!ptr) return NULL; if (!ptr)
return NULL;
return yyjson_mut_ptr_removen(val, ptr, strlen(ptr)); return yyjson_mut_ptr_removen(val, ptr, strlen(ptr));
} }
@ -7726,7 +7833,8 @@ extern "C" {
yyjson_ptr_err* err yyjson_ptr_err* err
) { ) {
yyjson_ptr_set_err(NONE, NULL); yyjson_ptr_set_err(NONE, NULL);
if (ctx) memset(ctx, 0, sizeof(*ctx)); if (ctx)
memset(ctx, 0, sizeof(*ctx));
if (yyjson_unlikely(!val || !ptr)) { if (yyjson_unlikely(!val || !ptr)) {
yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); yyjson_ptr_set_err(PARAMETER, "input parameter is NULL");
@ -7749,11 +7857,13 @@ extern "C" {
yyjson_mut_val* val yyjson_mut_val* val
) { ) {
yyjson_mut_val *ctn, *pre_key, *pre_val, *cur_key, *cur_val; yyjson_mut_val *ctn, *pre_key, *pre_val, *cur_key, *cur_val;
if (!ctx || !ctx->ctn || !val) return false; if (!ctx || !ctx->ctn || !val)
return false;
ctn = ctx->ctn; ctn = ctx->ctn;
if (yyjson_mut_is_obj(ctn)) { if (yyjson_mut_is_obj(ctn)) {
if (!key) return false; if (!key)
return false;
key->next = val; key->next = val;
pre_key = ctx->pre; pre_key = ctx->pre;
if (unsafe_yyjson_get_len(ctn) == 0) { if (unsafe_yyjson_get_len(ctn) == 0) {
@ -7772,7 +7882,8 @@ extern "C" {
cur_val = cur_key->next; cur_val = cur_key->next;
val->next = cur_val->next; val->next = cur_val->next;
cur_val->next = key; cur_val->next = key;
if (ctn->uni.ptr == cur_key) ctn->uni.ptr = key; if (ctn->uni.ptr == cur_key)
ctn->uni.ptr = key;
ctx->pre = cur_key; ctx->pre = cur_key;
} }
} else { } else {
@ -7791,7 +7902,8 @@ extern "C" {
cur_val = pre_val->next; cur_val = pre_val->next;
val->next = cur_val->next; val->next = cur_val->next;
cur_val->next = val; cur_val->next = val;
if (ctn->uni.ptr == cur_val) ctn->uni.ptr = val; if (ctn->uni.ptr == cur_val)
ctn->uni.ptr = val;
ctx->pre = cur_val; ctx->pre = cur_val;
} }
} }
@ -7802,7 +7914,8 @@ extern "C" {
yyjson_api_inline bool yyjson_api_inline bool
yyjson_ptr_ctx_replace(yyjson_ptr_ctx* ctx, yyjson_mut_val* val) { yyjson_ptr_ctx_replace(yyjson_ptr_ctx* ctx, yyjson_mut_val* val) {
yyjson_mut_val *ctn, *pre_key, *cur_key, *pre_val, *cur_val; yyjson_mut_val *ctn, *pre_key, *cur_key, *pre_val, *cur_val;
if (!ctx || !ctx->ctn || !ctx->pre || !val) return false; if (!ctx || !ctx->ctn || !ctx->pre || !val)
return false;
ctn = ctx->ctn; ctn = ctx->ctn;
if (yyjson_mut_is_obj(ctn)) { if (yyjson_mut_is_obj(ctn)) {
pre_key = ctx->pre; pre_key = ctx->pre;
@ -7820,7 +7933,8 @@ extern "C" {
if (pre_val != cur_val) { if (pre_val != cur_val) {
val->next = cur_val->next; val->next = cur_val->next;
pre_val->next = val; pre_val->next = val;
if (ctn->uni.ptr == cur_val) ctn->uni.ptr = val; if (ctn->uni.ptr == cur_val)
ctn->uni.ptr = val;
} else { } else {
val->next = val; val->next = val;
ctn->uni.ptr = val; ctn->uni.ptr = val;
@ -7834,7 +7948,8 @@ extern "C" {
yyjson_api_inline bool yyjson_ptr_ctx_remove(yyjson_ptr_ctx* ctx) { yyjson_api_inline bool yyjson_ptr_ctx_remove(yyjson_ptr_ctx* ctx) {
yyjson_mut_val *ctn, *pre_key, *pre_val, *cur_key, *cur_val; yyjson_mut_val *ctn, *pre_key, *pre_val, *cur_key, *cur_val;
size_t len; size_t len;
if (!ctx || !ctx->ctn || !ctx->pre) return false; if (!ctx || !ctx->ctn || !ctx->pre)
return false;
ctn = ctx->ctn; ctn = ctx->ctn;
if (yyjson_mut_is_obj(ctn)) { if (yyjson_mut_is_obj(ctn)) {
pre_key = ctx->pre; pre_key = ctx->pre;
@ -7843,7 +7958,8 @@ extern "C" {
cur_val = cur_key->next; cur_val = cur_key->next;
/* remove current key-value */ /* remove current key-value */
pre_val->next = cur_val->next; pre_val->next = cur_val->next;
if (ctn->uni.ptr == cur_key) ctn->uni.ptr = pre_key; if (ctn->uni.ptr == cur_key)
ctn->uni.ptr = pre_key;
ctx->pre = NULL; ctx->pre = NULL;
ctx->old = cur_val; ctx->old = cur_val;
} else { } else {
@ -7851,12 +7967,14 @@ extern "C" {
cur_val = pre_val->next; cur_val = pre_val->next;
/* remove current key-value */ /* remove current key-value */
pre_val->next = cur_val->next; pre_val->next = cur_val->next;
if (ctn->uni.ptr == cur_val) ctn->uni.ptr = pre_val; if (ctn->uni.ptr == cur_val)
ctn->uni.ptr = pre_val;
ctx->pre = NULL; ctx->pre = NULL;
ctx->old = cur_val; ctx->old = cur_val;
} }
len = unsafe_yyjson_get_len(ctn) - 1; len = unsafe_yyjson_get_len(ctn) - 1;
if (len == 0) ctn->uni.ptr = NULL; if (len == 0)
ctn->uni.ptr = NULL;
unsafe_yyjson_set_len(ctn, len); unsafe_yyjson_set_len(ctn, len);
return true; return true;
} }

View file

@ -13,10 +13,9 @@ struct BytesToGiB {
template <> template <>
struct fmt::formatter<BytesToGiB> : formatter<double> { struct fmt::formatter<BytesToGiB> : formatter<double> {
template <typename FormatContext> template <typename FmtCtx>
typename FormatContext::iterator fn format(const BytesToGiB BTG, FmtCtx& ctx) -> typename FmtCtx::iterator {
format(const BytesToGiB BTG, FormatContext& ctx) { typename FmtCtx::iterator out = formatter<double>::format(
typename FormatContext::iterator out = formatter<double>::format(
static_cast<double>(BTG.value) / pow(1024, 3), ctx static_cast<double>(BTG.value) / pow(1024, 3), ctx
); );
*out++ = 'G'; *out++ = 'G';
@ -26,53 +25,35 @@ struct fmt::formatter<BytesToGiB> : formatter<double> {
} }
}; };
enum DateNum { Ones, Twos, Threes, Default }; fn GetDate() -> string {
DateNum ParseDate(string const& input) {
if (input == "1" || input == "21" || input == "31") return Ones;
if (input == "2" || input == "22") return Twos;
if (input == "3" || input == "23") return Threes;
return Default;
}
int main() {
const Config& config = Config::getInstance();
if (config.getNowPlaying().getEnabled()) fmt::println("{}", GetNowPlaying());
fmt::println("Hello {}!", config.getGeneral().getName());
const u64 memInfo = GetMemInfo();
fmt::println("{:.2f}", BytesToGiB {memInfo});
const std::tm localTime = fmt::localtime(time(nullptr)); const std::tm localTime = fmt::localtime(time(nullptr));
string date = fmt::format("{:%e}", localTime); string date = fmt::format("{:%e}", localTime);
auto start = date.begin(); if (!date.empty() && std::isspace(date.front()))
while (start != date.end() && std::isspace(*start)) ++start; date.erase(date.begin());
date.erase(date.begin(), start);
switch (ParseDate(date)) { if (date == "1" || date == "21" || date == "31")
case Ones: date += "st";
date += "st"; else if (date == "2" || date == "22")
break; date += "nd";
else if (date == "3" || date == "23")
date += "rd";
else
date += "th";
case Twos: return fmt::format("{:%B} {}, {:%-I:%0M %p}", localTime, date, localTime);
date += "nd"; }
break;
case Threes: fn main() -> int {
date += "rd"; const Config& config = Config::getInstance();
break;
case Default: if (config.getNowPlaying().getEnabled())
date += "th"; fmt::println("{}", GetNowPlaying());
break;
}
fmt::println("{:%B} {}, {:%-I:%0M %p}", localTime, date, localTime); fmt::println("Hello {}!", config.getGeneral().getName());
fmt::println("Installed RAM: {:.2f}", BytesToGiB {GetMemInfo()});
fmt::println("Today is: {}", GetDate());
Weather::WeatherOutput json = config.getWeather().getWeatherInfo(); Weather::WeatherOutput json = config.getWeather().getWeatherInfo();

View file

@ -93,13 +93,15 @@ std::vector<std::string> GetMprisPlayers(sdbus::IConnection& connection) {
std::vector<std::string> mprisPlayers; std::vector<std::string> mprisPlayers;
for (const std::basic_string<char>& name : names) for (const std::basic_string<char>& name : names)
if (name.contains(mprisInterfaceName)) mprisPlayers.push_back(name); if (name.contains(mprisInterfaceName))
mprisPlayers.push_back(name);
return mprisPlayers; return mprisPlayers;
} }
std::string GetActivePlayer(const std::vector<std::string>& mprisPlayers) { std::string GetActivePlayer(const std::vector<std::string>& mprisPlayers) {
if (!mprisPlayers.empty()) return mprisPlayers.front(); if (!mprisPlayers.empty())
return mprisPlayers.front();
return ""; return "";
} }
@ -114,11 +116,13 @@ std::string GetNowPlaying() {
std::vector<std::string> mprisPlayers = GetMprisPlayers(*connection); std::vector<std::string> mprisPlayers = GetMprisPlayers(*connection);
if (mprisPlayers.empty()) return ""; if (mprisPlayers.empty())
return "";
std::string activePlayer = GetActivePlayer(mprisPlayers); std::string activePlayer = GetActivePlayer(mprisPlayers);
if (activePlayer.empty()) return ""; if (activePlayer.empty())
return "";
std::unique_ptr<sdbus::IProxy> playerProxy = std::unique_ptr<sdbus::IProxy> playerProxy =
sdbus::createProxy(*connection, activePlayer, playerObjectPath); sdbus::createProxy(*connection, activePlayer, playerObjectPath);