:3
This commit is contained in:
parent
a743cdabe5
commit
bd402f57f5
276 changed files with 37936 additions and 22932 deletions
|
@ -6,40 +6,46 @@
|
|||
#include "Writer.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
namespace parsing {
|
||||
|
||||
/// CBOR requires us to explicitly set the number of fields in advance. Because
|
||||
/// of that, we require all of the fields and then set them to nullptr, if
|
||||
/// necessary.
|
||||
template <class ProcessorsType, class... FieldTypes>
|
||||
requires AreReaderAndWriter<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...> {
|
||||
};
|
||||
/// CBOR requires us to explicitly set the number of fields in advance.
|
||||
/// Because of that, we require all of the fields and then set them to
|
||||
/// nullptr, if necessary.
|
||||
template <class ProcessorsType, class... FieldTypes>
|
||||
requires AreReaderAndWriter<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...> {};
|
||||
|
||||
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...> {
|
||||
};
|
||||
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...> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
namespace rfl {
|
||||
namespace cbor {
|
||||
namespace cbor {
|
||||
|
||||
template <class T, class ProcessorsType>
|
||||
using Parser = parsing::Parser<Reader, Writer, T, ProcessorsType>;
|
||||
template <class T, class ProcessorsType>
|
||||
using Parser = parsing::Parser<Reader, Writer, T, ProcessorsType>;
|
||||
|
||||
}
|
||||
} // namespace rfl
|
||||
}
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
#ifndef RFL_CBOR_READER_HPP_
|
||||
#define RFL_CBOR_READER_HPP_
|
||||
|
||||
#include <cbor.h>
|
||||
|
||||
#include <array>
|
||||
#include <cbor.h>
|
||||
#include <concepts>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
|
@ -22,238 +21,206 @@
|
|||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace cbor {
|
||||
namespace cbor {
|
||||
|
||||
/// Please refer to https://intel.github.io/tinycbor/current/index.html
|
||||
struct Reader {
|
||||
struct CBORInputArray {
|
||||
CborValue* val_;
|
||||
};
|
||||
/// Please refer to https://intel.github.io/tinycbor/current/index.html
|
||||
struct Reader {
|
||||
struct CBORInputArray {
|
||||
CborValue* val_;
|
||||
};
|
||||
|
||||
struct CBORInputObject {
|
||||
CborValue* val_;
|
||||
};
|
||||
struct CBORInputObject {
|
||||
CborValue* val_;
|
||||
};
|
||||
|
||||
struct CBORInputVar {
|
||||
CborValue* val_;
|
||||
};
|
||||
struct CBORInputVar {
|
||||
CborValue* val_;
|
||||
};
|
||||
|
||||
using InputArrayType = CBORInputArray;
|
||||
using InputObjectType = CBORInputObject;
|
||||
using InputVarType = CBORInputVar;
|
||||
using InputArrayType = CBORInputArray;
|
||||
using InputObjectType = CBORInputObject;
|
||||
using InputVarType = CBORInputVar;
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
|
||||
T::from_cbor_obj(var);
|
||||
});
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor =
|
||||
(requires(InputVarType var) { T::from_cbor_obj(var); });
|
||||
|
||||
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);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
size_t length = 0;
|
||||
err = cbor_value_get_map_length(_obj.val_, &length);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
if (!cbor_value_is_text_string(&val)) {
|
||||
return Error("Expected the key to be a string value.");
|
||||
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);
|
||||
if (err != CborNoError) { return Error(cbor_error_string(err)); }
|
||||
size_t length = 0;
|
||||
err = cbor_value_get_map_length(_obj.val_, &length);
|
||||
if (err != CborNoError) { return Error(cbor_error_string(err)); }
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
if (!cbor_value_is_text_string(&val)) {
|
||||
return Error("Expected the key to be a string value.");
|
||||
}
|
||||
err = get_string(&val, &buffer);
|
||||
if (err != CborNoError) { return Error(cbor_error_string(err)); }
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError) { return Error(cbor_error_string(err)); }
|
||||
if (_name == buffer.data()) { return to_input_var(&val); }
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError) { return Error(cbor_error_string(err)); }
|
||||
}
|
||||
return Error("No field named '" + _name + "' was found.");
|
||||
}
|
||||
err = get_string(&val, &buffer);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
if (_name == buffer.data()) {
|
||||
return to_input_var(&val);
|
||||
}
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
}
|
||||
return Error("No field named '" + _name + "' was found.");
|
||||
}
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return cbor_value_is_null(_var.val_);
|
||||
}
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return cbor_value_is_null(_var.val_);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
if (!cbor_value_is_text_string(_var.val_)) {
|
||||
return Error("Could not cast to string.");
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
if (!cbor_value_is_text_string(_var.val_)) {
|
||||
return Error("Could not cast to string.");
|
||||
}
|
||||
std::vector<char> buffer;
|
||||
const auto err = get_string(_var.val_, &buffer);
|
||||
if (err != CborNoError) { return Error(cbor_error_string(err)); }
|
||||
return std::string(buffer.data());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!cbor_value_is_boolean(_var.val_)) {
|
||||
return rfl::Error("Could not cast to boolean.");
|
||||
}
|
||||
bool result = false;
|
||||
const auto err = cbor_value_get_boolean(_var.val_, &result);
|
||||
if (err != CborNoError) { return Error(cbor_error_string(err)); }
|
||||
return result;
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
if (cbor_value_is_integer(_var.val_)) {
|
||||
std::int64_t result = 0;
|
||||
const auto err = cbor_value_get_int64(_var.val_, &result);
|
||||
if (err != CborNoError) { return Error(cbor_error_string(err)); }
|
||||
return static_cast<T>(result);
|
||||
} else if (cbor_value_is_float(_var.val_)) {
|
||||
float result = 0.0;
|
||||
const auto err = cbor_value_get_float(_var.val_, &result);
|
||||
if (err != CborNoError) { return Error(cbor_error_string(err)); }
|
||||
return static_cast<T>(result);
|
||||
} else if (cbor_value_is_double(_var.val_)) {
|
||||
double result = 0.0;
|
||||
const auto err = cbor_value_get_double(_var.val_, &result);
|
||||
if (err != CborNoError) { return Error(cbor_error_string(err)); }
|
||||
return static_cast<T>(result);
|
||||
}
|
||||
return rfl::Error(
|
||||
"Could not cast to numeric value. The type must be integral, "
|
||||
"float "
|
||||
"or double.");
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
std::vector<char> buffer;
|
||||
const auto err = get_string(_var.val_, &buffer);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
|
||||
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 std::string(buffer.data());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!cbor_value_is_boolean(_var.val_)) {
|
||||
return rfl::Error("Could not cast to boolean.");
|
||||
|
||||
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_};
|
||||
}
|
||||
bool result = false;
|
||||
const auto err = cbor_value_get_boolean(_var.val_, &result);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
return result;
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
if (cbor_value_is_integer(_var.val_)) {
|
||||
std::int64_t result = 0;
|
||||
const auto err = cbor_value_get_int64(_var.val_, &result);
|
||||
if (err != CborNoError) {
|
||||
|
||||
template <class ArrayReader>
|
||||
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);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
return static_cast<T>(result);
|
||||
} else if (cbor_value_is_float(_var.val_)) {
|
||||
float result = 0.0;
|
||||
const auto err = cbor_value_get_float(_var.val_, &result);
|
||||
if (err != CborNoError) {
|
||||
size_t length = 0;
|
||||
err = cbor_value_get_array_length(_arr.val_, &length);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
return static_cast<T>(result);
|
||||
} else if (cbor_value_is_double(_var.val_)) {
|
||||
double result = 0.0;
|
||||
const auto err = cbor_value_get_double(_var.val_, &result);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
const auto err2 = _array_reader.read(to_input_var(&val));
|
||||
if (err2) { return err2; }
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
}
|
||||
return static_cast<T>(result);
|
||||
return std::nullopt;
|
||||
}
|
||||
return rfl::Error(
|
||||
"Could not cast to numeric value. The type must be integral, float "
|
||||
"or double.");
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
template <class ObjectReader>
|
||||
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) { return Error(cbor_error_string(err)); }
|
||||
|
||||
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_};
|
||||
}
|
||||
CborValue val;
|
||||
err = cbor_value_enter_container(_obj.val_, &val);
|
||||
if (err != CborNoError) { return Error(cbor_error_string(err)); }
|
||||
|
||||
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_};
|
||||
}
|
||||
auto buffer = std::vector<char>();
|
||||
|
||||
template <class ArrayReader>
|
||||
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);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
size_t length = 0;
|
||||
err = cbor_value_get_array_length(_arr.val_, &length);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
const auto err2 = _array_reader.read(to_input_var(&val));
|
||||
if (err2) {
|
||||
return err2;
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
err = get_string(&val, &buffer);
|
||||
if (err != CborNoError) { return Error(cbor_error_string(err)); }
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError) { return Error(cbor_error_string(err)); }
|
||||
const auto name = std::string_view(buffer.data(), buffer.size() - 1);
|
||||
_object_reader.read(name, InputVarType {&val});
|
||||
cbor_value_advance(&val);
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return Error(cbor_error_string(err));
|
||||
|
||||
template <class T>
|
||||
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()); }
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
template <class ObjectReader>
|
||||
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) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
|
||||
CborValue val;
|
||||
err = cbor_value_enter_container(_obj.val_, &val);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
|
||||
auto buffer = std::vector<char>();
|
||||
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
err = get_string(&val, &buffer);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
private:
|
||||
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) { return err; }
|
||||
_buffer->resize(length + 1);
|
||||
(*_buffer)[length] = '\0';
|
||||
return cbor_value_copy_text_string(_ptr, _buffer->data(), &length,
|
||||
NULL);
|
||||
}
|
||||
err = cbor_value_advance(&val);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
|
||||
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};
|
||||
}
|
||||
const auto name = std::string_view(buffer.data(), buffer.size() - 1);
|
||||
_object_reader.read(name, InputVarType{&val});
|
||||
cbor_value_advance(&val);
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
private:
|
||||
/// Contains the values inside the object.
|
||||
rfl::Box<std::vector<rfl::Box<CborValue>>> values_;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
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());
|
||||
}
|
||||
}
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
private:
|
||||
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) {
|
||||
return err;
|
||||
}
|
||||
_buffer->resize(length + 1);
|
||||
(*_buffer)[length] = '\0';
|
||||
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};
|
||||
}
|
||||
|
||||
private:
|
||||
/// Contains the values inside the object.
|
||||
rfl::Box<std::vector<rfl::Box<CborValue>>> values_;
|
||||
};
|
||||
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
#endif // JSON_PARSER_HPP_
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
#define RFL_CBOR_WRITER_HPP_
|
||||
|
||||
#include <cbor.h>
|
||||
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
|
@ -19,146 +18,153 @@
|
|||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace cbor {
|
||||
namespace cbor {
|
||||
|
||||
class Writer {
|
||||
public:
|
||||
struct CBOROutputArray {
|
||||
CborEncoder* encoder_;
|
||||
CborEncoder* parent_;
|
||||
};
|
||||
class Writer {
|
||||
public:
|
||||
struct CBOROutputArray {
|
||||
CborEncoder* encoder_;
|
||||
CborEncoder* parent_;
|
||||
};
|
||||
|
||||
struct CBOROutputObject {
|
||||
CborEncoder* encoder_;
|
||||
CborEncoder* parent_;
|
||||
};
|
||||
struct CBOROutputObject {
|
||||
CborEncoder* encoder_;
|
||||
CborEncoder* parent_;
|
||||
};
|
||||
|
||||
struct CBOROutputVar {};
|
||||
struct CBOROutputVar {};
|
||||
|
||||
using OutputArrayType = CBOROutputArray;
|
||||
using OutputObjectType = CBOROutputObject;
|
||||
using OutputVarType = CBOROutputVar;
|
||||
using OutputArrayType = CBOROutputArray;
|
||||
using OutputObjectType = CBOROutputObject;
|
||||
using OutputVarType = CBOROutputVar;
|
||||
|
||||
Writer(CborEncoder* _encoder) : encoder_(_encoder) {}
|
||||
Writer(CborEncoder* _encoder) : encoder_(_encoder) {}
|
||||
|
||||
~Writer() = default;
|
||||
~Writer() = default;
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array(_size, encoder_);
|
||||
}
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array(_size, encoder_);
|
||||
}
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object(_size, encoder_);
|
||||
}
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object(_size, encoder_);
|
||||
}
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
cbor_encode_null(encoder_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
cbor_encode_null(encoder_);
|
||||
return OutputVarType {};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
return new_value(_var, encoder_);
|
||||
}
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
return new_value(_var, encoder_);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array(_size, _parent->encoder_);
|
||||
}
|
||||
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 noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
return new_array(_size, _parent->encoder_);
|
||||
}
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
return new_array(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object(_size, _parent->encoder_);
|
||||
}
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_object(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
return new_object(_size, _parent->encoder_);
|
||||
}
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
return new_object(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
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_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 noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
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 noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
return new_value(_var, _parent->encoder_);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
cbor_encode_null(_parent->encoder_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
cbor_encode_null(_parent->encoder_);
|
||||
return OutputVarType {};
|
||||
}
|
||||
|
||||
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{};
|
||||
}
|
||||
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 {};
|
||||
}
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {
|
||||
cbor_encoder_close_container(_arr->parent_, _arr->encoder_);
|
||||
}
|
||||
void end_array(OutputArrayType* _arr) const noexcept {
|
||||
cbor_encoder_close_container(_arr->parent_, _arr->encoder_);
|
||||
}
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {
|
||||
cbor_encoder_close_container(_obj->parent_, _obj->encoder_);
|
||||
}
|
||||
void end_object(OutputObjectType* _obj) const noexcept {
|
||||
cbor_encoder_close_container(_obj->parent_, _obj->encoder_);
|
||||
}
|
||||
|
||||
private:
|
||||
OutputArrayType new_array(const size_t _size,
|
||||
CborEncoder* _parent) const noexcept {
|
||||
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
|
||||
cbor_encoder_create_array(_parent, subencoders_->back().get(), _size);
|
||||
return OutputArrayType{subencoders_->back().get(), _parent};
|
||||
}
|
||||
private:
|
||||
OutputArrayType new_array(const size_t _size,
|
||||
CborEncoder* _parent) const noexcept {
|
||||
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
|
||||
cbor_encoder_create_array(_parent, subencoders_->back().get(), _size);
|
||||
return OutputArrayType {subencoders_->back().get(), _parent};
|
||||
}
|
||||
|
||||
OutputObjectType new_object(const size_t _size,
|
||||
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};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType new_value(const T& _var,
|
||||
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};
|
||||
}
|
||||
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>()) {
|
||||
cbor_encode_boolean(_parent, _var);
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
cbor_encode_double(_parent, static_cast<double>(_var));
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
cbor_encode_int(_parent, static_cast<std::int64_t>(_var));
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
return OutputVarType {};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
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>()) {
|
||||
cbor_encode_boolean(_parent, _var);
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
cbor_encode_double(_parent, static_cast<double>(_var));
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
cbor_encode_int(_parent, static_cast<std::int64_t>(_var));
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
return OutputVarType{};
|
||||
}
|
||||
private:
|
||||
/// The underlying TinyCBOR encoder.
|
||||
CborEncoder* const encoder_;
|
||||
|
||||
private:
|
||||
/// The underlying TinyCBOR encoder.
|
||||
CborEncoder* const encoder_;
|
||||
/// Contain all of the subobjects and subarrays.
|
||||
const rfl::Box<std::vector<rfl::Box<CborEncoder>>> subencoders_;
|
||||
};
|
||||
|
||||
/// Contain all of the subobjects and subarrays.
|
||||
const rfl::Box<std::vector<rfl::Box<CborEncoder>>> subencoders_;
|
||||
};
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
#endif // CBOR_PARSER_HPP_
|
||||
#endif // CBOR_PARSER_HPP_
|
||||
|
|
|
@ -7,17 +7,17 @@
|
|||
#include "read.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace cbor {
|
||||
namespace cbor {
|
||||
|
||||
template <class T, class... Ps>
|
||||
Result<T> load(const std::string& _fname) {
|
||||
const auto read_bytes = [](const auto& _bytes) {
|
||||
return read<T, Ps...>(_bytes);
|
||||
};
|
||||
return rfl::io::load_bytes(_fname).and_then(read_bytes);
|
||||
}
|
||||
template <class T, class... Ps>
|
||||
Result<T> load(const std::string& _fname) {
|
||||
const auto read_bytes = [](const auto& _bytes) {
|
||||
return read<T, Ps...>(_bytes);
|
||||
};
|
||||
return rfl::io::load_bytes(_fname).and_then(read_bytes);
|
||||
}
|
||||
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
#define RFL_CBOR_READ_HPP_
|
||||
|
||||
#include <cbor.h>
|
||||
|
||||
#include <istream>
|
||||
#include <string>
|
||||
|
||||
|
@ -12,46 +11,46 @@
|
|||
#include "Reader.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace cbor {
|
||||
namespace cbor {
|
||||
|
||||
using InputObjectType = typename Reader::InputObjectType;
|
||||
using InputVarType = typename Reader::InputVarType;
|
||||
using InputObjectType = typename Reader::InputObjectType;
|
||||
using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from a CBOR var.
|
||||
template <class T, class... Ps>
|
||||
auto read(const InputVarType& _obj) {
|
||||
const auto r = Reader();
|
||||
return Parser<T, Processors<Ps...>>::read(r, _obj);
|
||||
}
|
||||
/// Parses an object from a CBOR var.
|
||||
template <class T, class... Ps>
|
||||
auto read(const InputVarType& _obj) {
|
||||
const auto r = Reader();
|
||||
return Parser<T, Processors<Ps...>>::read(r, _obj);
|
||||
}
|
||||
|
||||
/// 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) {
|
||||
CborParser parser;
|
||||
CborValue 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;
|
||||
}
|
||||
/// 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) {
|
||||
CborParser parser;
|
||||
CborValue 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;
|
||||
}
|
||||
|
||||
/// Parses an object from CBOR using reflection.
|
||||
template <class T, class... Ps>
|
||||
auto read(const std::vector<char>& _bytes) {
|
||||
return read<T, Ps...>(_bytes.data(), _bytes.size());
|
||||
}
|
||||
/// Parses an object from CBOR using reflection.
|
||||
template <class T, class... Ps>
|
||||
auto read(const std::vector<char>& _bytes) {
|
||||
return read<T, Ps...>(_bytes.data(), _bytes.size());
|
||||
}
|
||||
|
||||
/// Parses an object from a stream.
|
||||
template <class T, class... Ps>
|
||||
auto read(std::istream& _stream) {
|
||||
std::istreambuf_iterator<char> begin(_stream), end;
|
||||
auto bytes = std::vector<char>(begin, end);
|
||||
return read<T, Ps...>(bytes.data(), bytes.size());
|
||||
}
|
||||
/// Parses an object from a stream.
|
||||
template <class T, class... Ps>
|
||||
auto read(std::istream& _stream) {
|
||||
std::istreambuf_iterator<char> begin(_stream), end;
|
||||
auto bytes = std::vector<char>(begin, end);
|
||||
return read<T, Ps...>(bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
|
|
@ -10,17 +10,17 @@
|
|||
#include "write.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace cbor {
|
||||
namespace cbor {
|
||||
|
||||
template <class... Ps>
|
||||
Result<Nothing> save(const std::string& _fname, const auto& _obj) {
|
||||
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
|
||||
return write<Ps...>(_obj, _stream);
|
||||
};
|
||||
return rfl::io::save_bytes(_fname, _obj, write_func);
|
||||
}
|
||||
template <class... Ps>
|
||||
Result<Nothing> save(const std::string& _fname, const auto& _obj) {
|
||||
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
|
||||
return write<Ps...>(_obj, _stream);
|
||||
};
|
||||
return rfl::io::save_bytes(_fname, _obj, write_func);
|
||||
}
|
||||
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
#define RFL_CBOR_WRITE_HPP_
|
||||
|
||||
#include <cbor.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
|
@ -13,47 +12,48 @@
|
|||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace cbor {
|
||||
namespace cbor {
|
||||
|
||||
template <class... Ps>
|
||||
void write_into_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);
|
||||
const auto writer = Writer(_encoder);
|
||||
Parser<T, Processors<Ps...>>::write(writer, _obj,
|
||||
typename ParentType::Root{});
|
||||
}
|
||||
template <class... Ps>
|
||||
void write_into_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);
|
||||
const auto writer = Writer(_encoder);
|
||||
Parser<T, Processors<Ps...>>::write(writer, _obj,
|
||||
typename ParentType::Root {});
|
||||
}
|
||||
|
||||
/// Returns CBOR bytes.
|
||||
template <class... Ps>
|
||||
std::vector<char> write(const auto& _obj) noexcept {
|
||||
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);
|
||||
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()));
|
||||
buffer.resize(length);
|
||||
return buffer;
|
||||
}
|
||||
/// Returns CBOR bytes.
|
||||
template <class... Ps>
|
||||
std::vector<char> write(const auto& _obj) noexcept {
|
||||
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);
|
||||
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()));
|
||||
buffer.resize(length);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// Writes a CBOR into an ostream.
|
||||
template <class... Ps>
|
||||
std::ostream& write(const auto& _obj, std::ostream& _stream) noexcept {
|
||||
auto buffer = write<Ps...>(_obj);
|
||||
_stream.write(buffer.data(), buffer.size());
|
||||
return _stream;
|
||||
}
|
||||
/// Writes a CBOR into an ostream.
|
||||
template <class... Ps>
|
||||
std::ostream& write(const auto& _obj, std::ostream& _stream) noexcept {
|
||||
auto buffer = write<Ps...>(_obj);
|
||||
_stream.write(buffer.data(), buffer.size());
|
||||
return _stream;
|
||||
}
|
||||
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue