blwegh
This commit is contained in:
parent
f668a2eb4c
commit
50083503cf
224 changed files with 16150 additions and 19488 deletions
|
@ -12,33 +12,26 @@ namespace rfl {
|
|||
/// Because of that, we require all of the fields and then set them to
|
||||
/// nullptr, if necessary.
|
||||
template <class ProcessorsType, class... FieldTypes>
|
||||
requires AreReaderAndWriter<
|
||||
requires AreReaderAndWriter<cbor::Reader, cbor::Writer, NamedTuple<FieldTypes...>>
|
||||
struct Parser<cbor::Reader, cbor::Writer, NamedTuple<FieldTypes...>, ProcessorsType>
|
||||
: public NamedTupleParser<
|
||||
cbor::Reader,
|
||||
cbor::Writer,
|
||||
NamedTuple<FieldTypes...>>
|
||||
struct Parser<
|
||||
cbor::Reader,
|
||||
cbor::Writer,
|
||||
NamedTuple<FieldTypes...>,
|
||||
ProcessorsType>
|
||||
: public NamedTupleParser<
|
||||
cbor::Reader,
|
||||
cbor::Writer,
|
||||
/*_ignore_empty_containers=*/false,
|
||||
/*_all_required=*/true,
|
||||
ProcessorsType,
|
||||
FieldTypes...> {};
|
||||
/*_ignore_empty_containers=*/false,
|
||||
/*_all_required=*/true,
|
||||
ProcessorsType,
|
||||
FieldTypes...> {};
|
||||
|
||||
template <class ProcessorsType, class... Ts>
|
||||
requires AreReaderAndWriter<cbor::Reader, cbor::Writer, std::tuple<Ts...>>
|
||||
struct Parser<cbor::Reader, cbor::Writer, std::tuple<Ts...>, ProcessorsType>
|
||||
: public TupleParser<
|
||||
cbor::Reader,
|
||||
cbor::Writer,
|
||||
/*_ignore_empty_containers=*/false,
|
||||
/*_all_required=*/true,
|
||||
ProcessorsType,
|
||||
Ts...> {};
|
||||
: public TupleParser<
|
||||
cbor::Reader,
|
||||
cbor::Writer,
|
||||
/*_ignore_empty_containers=*/false,
|
||||
/*_all_required=*/true,
|
||||
ProcessorsType,
|
||||
Ts...> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
|
|
@ -43,12 +43,10 @@ namespace rfl {
|
|||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor =
|
||||
(requires(InputVarType var) { T::from_cbor_obj(var); });
|
||||
(requires(InputVarType var) { T::from_cbor_obj(var); });
|
||||
|
||||
rfl::Result<InputVarType> get_field(
|
||||
const std::string& _name,
|
||||
const InputObjectType& _obj
|
||||
) const noexcept {
|
||||
rfl::Result<InputVarType> get_field(const std::string& _name, const InputObjectType& _obj)
|
||||
const noexcept {
|
||||
CborValue val;
|
||||
auto buffer = std::vector<char>();
|
||||
auto err = cbor_value_enter_container(_obj.val_, &val);
|
||||
|
@ -134,9 +132,9 @@ namespace rfl {
|
|||
return static_cast<T>(result);
|
||||
}
|
||||
return rfl::Error(
|
||||
"Could not cast to numeric value. The type must be integral, "
|
||||
"float "
|
||||
"or double."
|
||||
"Could not cast to numeric value. The type must be integral, "
|
||||
"float "
|
||||
"or double."
|
||||
);
|
||||
|
||||
} else {
|
||||
|
@ -144,27 +142,23 @@ namespace rfl {
|
|||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType& _var
|
||||
) const noexcept {
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept {
|
||||
if (!cbor_value_is_array(_var.val_)) {
|
||||
return Error("Could not cast to an array.");
|
||||
}
|
||||
return InputArrayType {_var.val_};
|
||||
return InputArrayType { _var.val_ };
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(const InputVarType& _var
|
||||
) const noexcept {
|
||||
rfl::Result<InputObjectType> to_object(const InputVarType& _var) const noexcept {
|
||||
if (!cbor_value_is_map(_var.val_)) {
|
||||
return Error("Could not cast to an object.");
|
||||
}
|
||||
return InputObjectType {_var.val_};
|
||||
return InputObjectType { _var.val_ };
|
||||
}
|
||||
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(
|
||||
const ArrayReader& _array_reader,
|
||||
const InputArrayType& _arr
|
||||
) const noexcept {
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader, const InputArrayType& _arr)
|
||||
const noexcept {
|
||||
CborValue val;
|
||||
auto buffer = std::vector<char>();
|
||||
auto err = cbor_value_enter_container(_arr.val_, &val);
|
||||
|
@ -190,10 +184,8 @@ namespace rfl {
|
|||
}
|
||||
|
||||
template <class ObjectReader>
|
||||
std::optional<Error> read_object(
|
||||
const ObjectReader& _object_reader,
|
||||
const InputObjectType& _obj
|
||||
) const noexcept {
|
||||
std::optional<Error>
|
||||
read_object(const ObjectReader& _object_reader, const InputObjectType& _obj) const noexcept {
|
||||
size_t length = 0;
|
||||
auto err = cbor_value_get_map_length(_obj.val_, &length);
|
||||
if (err != CborNoError) {
|
||||
|
@ -218,7 +210,7 @@ namespace rfl {
|
|||
return Error(cbor_error_string(err));
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -226,16 +218,14 @@ namespace rfl {
|
|||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(const InputVarType& _var
|
||||
) const noexcept {
|
||||
rfl::Result<T> use_custom_constructor(const InputVarType& _var) const noexcept {
|
||||
try {
|
||||
return T::from_cbor_obj(_var);
|
||||
} catch (std::exception& e) { return rfl::Error(e.what()); }
|
||||
}
|
||||
|
||||
private:
|
||||
CborError get_string(const CborValue* _ptr, std::vector<char>* _buffer)
|
||||
const noexcept {
|
||||
CborError get_string(const CborValue* _ptr, std::vector<char>* _buffer) const noexcept {
|
||||
size_t length = 0;
|
||||
auto err = cbor_value_get_string_length(_ptr, &length);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
|
@ -243,15 +233,13 @@ namespace rfl {
|
|||
}
|
||||
_buffer->resize(length + 1);
|
||||
(*_buffer)[length] = '\0';
|
||||
return cbor_value_copy_text_string(
|
||||
_ptr, _buffer->data(), &length, NULL
|
||||
);
|
||||
return cbor_value_copy_text_string(_ptr, _buffer->data(), &length, NULL);
|
||||
}
|
||||
|
||||
InputVarType to_input_var(CborValue* _ptr) const noexcept {
|
||||
values_->emplace_back(rfl::Box<CborValue>::make(*_ptr));
|
||||
auto* last_value = values_->back().get();
|
||||
return InputVarType {last_value};
|
||||
return InputVarType { last_value };
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
|
@ -60,49 +60,44 @@ namespace rfl {
|
|||
return new_value(_var, encoder_);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent
|
||||
) const noexcept {
|
||||
OutputArrayType add_array_to_array(const size_t _size, OutputArrayType* _parent)
|
||||
const noexcept {
|
||||
return new_array(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
return new_array(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent
|
||||
) const noexcept {
|
||||
OutputObjectType add_object_to_array(const size_t _size, OutputArrayType* _parent)
|
||||
const noexcept {
|
||||
return new_object(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
return new_object(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var, OutputArrayType* _parent)
|
||||
const noexcept {
|
||||
OutputVarType add_value_to_array(const T& _var, OutputArrayType* _parent) const noexcept {
|
||||
return new_value(_var, _parent->encoder_);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(
|
||||
const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent
|
||||
const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
return new_value(_var, _parent->encoder_);
|
||||
|
@ -113,10 +108,8 @@ namespace rfl {
|
|||
return OutputVarType {};
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_object(
|
||||
const std::string_view& _name,
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
OutputVarType add_null_to_object(const std::string_view& _name, OutputObjectType* _parent)
|
||||
const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
cbor_encode_null(_parent->encoder_);
|
||||
return OutputVarType {};
|
||||
|
@ -131,23 +124,20 @@ namespace rfl {
|
|||
}
|
||||
|
||||
private:
|
||||
OutputArrayType new_array(const size_t _size, CborEncoder* _parent)
|
||||
const noexcept {
|
||||
OutputArrayType new_array(const size_t _size, CborEncoder* _parent) const noexcept {
|
||||
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
|
||||
cbor_encoder_create_array(_parent, subencoders_->back().get(), _size);
|
||||
return OutputArrayType {subencoders_->back().get(), _parent};
|
||||
return OutputArrayType { subencoders_->back().get(), _parent };
|
||||
}
|
||||
|
||||
OutputObjectType new_object(const size_t _size, CborEncoder* _parent)
|
||||
const noexcept {
|
||||
OutputObjectType new_object(const size_t _size, CborEncoder* _parent) const noexcept {
|
||||
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
|
||||
cbor_encoder_create_map(_parent, subencoders_->back().get(), _size);
|
||||
return OutputObjectType {subencoders_->back().get(), _parent};
|
||||
return OutputObjectType { subencoders_->back().get(), _parent };
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType new_value(const T& _var, CborEncoder* _parent)
|
||||
const noexcept {
|
||||
OutputVarType new_value(const T& _var, CborEncoder* _parent) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
cbor_encode_text_string(_parent, _var.c_str(), _var.size());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
|
|
|
@ -11,9 +11,7 @@ namespace rfl {
|
|||
|
||||
template <class T, class... Ps>
|
||||
Result<T> load(const std::string& _fname) {
|
||||
const auto read_bytes = [](const auto& _bytes) {
|
||||
return read<T, Ps...>(_bytes);
|
||||
};
|
||||
const auto read_bytes = [](const auto& _bytes) { return read<T, Ps...>(_bytes); };
|
||||
return rfl::io::load_bytes(_fname).and_then(read_bytes);
|
||||
}
|
||||
|
||||
|
|
|
@ -25,14 +25,11 @@ namespace rfl {
|
|||
|
||||
/// Parses an object from CBOR using reflection.
|
||||
template <class T, class... Ps>
|
||||
Result<internal::wrap_in_rfl_array_t<T>>
|
||||
read(const char* _bytes, const size_t _size) {
|
||||
Result<internal::wrap_in_rfl_array_t<T>> read(const char* _bytes, const size_t _size) {
|
||||
CborParser parser;
|
||||
CborValue value;
|
||||
cbor_parser_init(
|
||||
reinterpret_cast<const uint8_t*>(_bytes), _size, 0, &parser, &value
|
||||
);
|
||||
auto doc = InputVarType {&value};
|
||||
cbor_parser_init(reinterpret_cast<const uint8_t*>(_bytes), _size, 0, &parser, &value);
|
||||
auto doc = InputVarType { &value };
|
||||
auto result = read<T, Ps...>(doc);
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -16,22 +16,15 @@ namespace rfl {
|
|||
|
||||
template <class... Ps>
|
||||
void write_into_buffer(
|
||||
const auto& _obj,
|
||||
CborEncoder* _encoder,
|
||||
std::vector<char>* _buffer
|
||||
const auto& _obj,
|
||||
CborEncoder* _encoder,
|
||||
std::vector<char>* _buffer
|
||||
) noexcept {
|
||||
using T = std::remove_cvref_t<decltype(_obj)>;
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
cbor_encoder_init(
|
||||
_encoder,
|
||||
reinterpret_cast<uint8_t*>(_buffer->data()),
|
||||
_buffer->size(),
|
||||
0
|
||||
);
|
||||
cbor_encoder_init(_encoder, reinterpret_cast<uint8_t*>(_buffer->data()), _buffer->size(), 0);
|
||||
const auto writer = Writer(_encoder);
|
||||
Parser<T, Processors<Ps...>>::write(
|
||||
writer, _obj, typename ParentType::Root {}
|
||||
);
|
||||
Parser<T, Processors<Ps...>>::write(writer, _obj, typename ParentType::Root {});
|
||||
}
|
||||
|
||||
/// Returns CBOR bytes.
|
||||
|
@ -40,15 +33,13 @@ namespace rfl {
|
|||
std::vector<char> buffer(4096);
|
||||
CborEncoder encoder;
|
||||
write_into_buffer<Ps...>(_obj, &encoder, &buffer);
|
||||
const auto total_bytes_needed =
|
||||
buffer.size() + cbor_encoder_get_extra_bytes_needed(&encoder);
|
||||
const auto total_bytes_needed = buffer.size() + cbor_encoder_get_extra_bytes_needed(&encoder);
|
||||
if (total_bytes_needed != buffer.size()) {
|
||||
buffer.resize(total_bytes_needed);
|
||||
write_into_buffer<Ps...>(_obj, &encoder, &buffer);
|
||||
}
|
||||
const auto length = cbor_encoder_get_buffer_size(
|
||||
&encoder, reinterpret_cast<uint8_t*>(buffer.data())
|
||||
);
|
||||
const auto length =
|
||||
cbor_encoder_get_buffer_size(&encoder, reinterpret_cast<uint8_t*>(buffer.data()));
|
||||
buffer.resize(length);
|
||||
return buffer;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue