mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-02 16:16:19 +08:00
Use jsoncons for CBOR instead of TinyCBOR (#325)
This commit is contained in:
+5
-14
@@ -39,7 +39,7 @@ if (REFLECTCPP_BUILD_TESTS OR REFLECTCPP_BUILD_BENCHMARKS OR
|
|||||||
(REFLECTCPP_JSON AND NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES) OR REFLECTCPP_AVRO OR
|
(REFLECTCPP_JSON AND NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES) OR REFLECTCPP_AVRO OR
|
||||||
REFLECTCPP_BSON OR REFLECTCPP_CAPNPROTO OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR
|
REFLECTCPP_BSON OR REFLECTCPP_CAPNPROTO OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR
|
||||||
REFLECTCPP_MSGPACK OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_UBJSON OR REFLECTCPP_YAML)
|
REFLECTCPP_MSGPACK OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_UBJSON OR REFLECTCPP_YAML)
|
||||||
# enable vcpkg per default if require features other than JSON
|
# enable vcpkg per default if features other than JSON are required
|
||||||
set(REFLECTCPP_USE_VCPKG_DEFAULT ON)
|
set(REFLECTCPP_USE_VCPKG_DEFAULT ON)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
@@ -147,20 +147,10 @@ if (REFLECTCPP_CBOR)
|
|||||||
list(APPEND REFLECT_CPP_SOURCES
|
list(APPEND REFLECT_CPP_SOURCES
|
||||||
src/reflectcpp_cbor.cpp
|
src/reflectcpp_cbor.cpp
|
||||||
)
|
)
|
||||||
if(REFLECTCPP_USE_VCPKG)
|
if (NOT TARGET jsoncons)
|
||||||
target_include_directories(reflectcpp SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include")
|
find_package(jsoncons CONFIG REQUIRED)
|
||||||
target_include_directories(reflectcpp SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include/tinycbor")
|
|
||||||
if (MSVC)
|
|
||||||
target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/tinycbor${CMAKE_STATIC_LIBRARY_SUFFIX}")
|
|
||||||
else ()
|
|
||||||
target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/libtinycbor${CMAKE_STATIC_LIBRARY_SUFFIX}")
|
|
||||||
endif ()
|
|
||||||
else()
|
|
||||||
if (NOT TARGET tinycbor)
|
|
||||||
find_package(tinycbor)
|
|
||||||
endif()
|
|
||||||
target_link_libraries(reflectcpp PUBLIC tinycbor::tinycbor)
|
|
||||||
endif()
|
endif()
|
||||||
|
include_directories(PUBLIC ${jsoncons_INCLUDE_DIRS})
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
if (REFLECTCPP_FLEXBUFFERS)
|
if (REFLECTCPP_FLEXBUFFERS)
|
||||||
@@ -212,6 +202,7 @@ if (REFLECTCPP_UBJSON)
|
|||||||
if (NOT TARGET jsoncons)
|
if (NOT TARGET jsoncons)
|
||||||
find_package(jsoncons CONFIG REQUIRED)
|
find_package(jsoncons CONFIG REQUIRED)
|
||||||
endif()
|
endif()
|
||||||
|
include_directories(PUBLIC ${jsoncons_INCLUDE_DIRS})
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
if (REFLECTCPP_XML)
|
if (REFLECTCPP_XML)
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ The following table lists the serialization formats currently supported by refle
|
|||||||
| Avro | [avro-c](https://avro.apache.org/docs/1.11.1/api/c/) | >= 1.11.3 | Apache 2.0 | Schemaful binary format |
|
| Avro | [avro-c](https://avro.apache.org/docs/1.11.1/api/c/) | >= 1.11.3 | Apache 2.0 | Schemaful binary format |
|
||||||
| BSON | [libbson](https://github.com/mongodb/mongo-c-driver) | >= 1.25.1 | Apache 2.0 | JSON-like binary format |
|
| BSON | [libbson](https://github.com/mongodb/mongo-c-driver) | >= 1.25.1 | Apache 2.0 | JSON-like binary format |
|
||||||
| Cap'n Proto | [capnproto](https://capnproto.org) | >= 1.0.2 | MIT | Schemaful binary format |
|
| Cap'n Proto | [capnproto](https://capnproto.org) | >= 1.0.2 | MIT | Schemaful binary format |
|
||||||
| CBOR | [tinycbor](https://github.com/intel/tinycbor) | >= 0.6.0 | MIT | JSON-like binary format |
|
| CBOR | [jsoncons](https://github.com/danielaparker/jsoncons)| >= 0.176.0 | BSL 1.0 | JSON-like binary format |
|
||||||
| flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | Schema-less version of flatbuffers, binary format |
|
| flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | Schema-less version of flatbuffers, binary format |
|
||||||
| msgpack | [msgpack-c](https://github.com/msgpack/msgpack-c) | >= 6.0.0 | BSL 1.0 | JSON-like binary format |
|
| msgpack | [msgpack-c](https://github.com/msgpack/msgpack-c) | >= 6.0.0 | BSL 1.0 | JSON-like binary format |
|
||||||
| TOML | [toml++](https://github.com/marzer/tomlplusplus) | >= 3.4.0 | MIT | Textual format with an emphasis on readability |
|
| TOML | [toml++](https://github.com/marzer/tomlplusplus) | >= 3.4.0 | MIT | Textual format with an emphasis on readability |
|
||||||
|
|||||||
+2
-4
@@ -73,16 +73,14 @@ class ReflectCppConan(ConanFile):
|
|||||||
self.requires("yyjson/0.10.0", transitive_headers=True)
|
self.requires("yyjson/0.10.0", transitive_headers=True)
|
||||||
if self.options.with_capnproto:
|
if self.options.with_capnproto:
|
||||||
self.requires("capnproto/1.1.0", transitive_headers=True)
|
self.requires("capnproto/1.1.0", transitive_headers=True)
|
||||||
if self.options.with_cbor:
|
if self.options.with_cbor or self.options.with_ubjson:
|
||||||
self.requires("tinycbor/0.6.0", transitive_headers=True)
|
self.requires("jsoncons/0.176.0", transitive_headers=True)
|
||||||
if self.options.with_flatbuffers:
|
if self.options.with_flatbuffers:
|
||||||
self.requires("flatbuffers/24.3.25", transitive_headers=True)
|
self.requires("flatbuffers/24.3.25", transitive_headers=True)
|
||||||
if self.options.with_msgpack:
|
if self.options.with_msgpack:
|
||||||
self.requires("msgpack-c/6.0.0", transitive_headers=True)
|
self.requires("msgpack-c/6.0.0", transitive_headers=True)
|
||||||
if self.options.with_toml:
|
if self.options.with_toml:
|
||||||
self.requires("tomlplusplus/3.4.0", transitive_headers=True)
|
self.requires("tomlplusplus/3.4.0", transitive_headers=True)
|
||||||
if self.options.with_ubjson:
|
|
||||||
self.requires("jsoncons/0.176.0", transitive_headers=True)
|
|
||||||
if self.options.with_xml:
|
if self.options.with_xml:
|
||||||
self.requires("pugixml/1.14", transitive_headers=True)
|
self.requires("pugixml/1.14", transitive_headers=True)
|
||||||
if self.options.with_yaml:
|
if self.options.with_yaml:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# CBOR
|
# CBOR
|
||||||
|
|
||||||
For CBOR support, you must also include the header `<rfl/cbor.hpp>` and link to the tinycbor library (https://github.com/intel/tinycbor).
|
For CBOR support, you must also include the header `<rfl/cbor.hpp>` and include the jsoncons library (https://github.com/danielaparker/jsoncons). Note that it is header-only.
|
||||||
|
|
||||||
CBOR or Concise Binary Object Representation, is a JSON-like binary format with an emphasis on small binary sizes.
|
CBOR or Concise Binary Object Representation, is a JSON-like binary format with an emphasis on small binary sizes.
|
||||||
|
|
||||||
|
|||||||
+14
-21
@@ -6,15 +6,14 @@
|
|||||||
#include "Reader.hpp"
|
#include "Reader.hpp"
|
||||||
#include "Writer.hpp"
|
#include "Writer.hpp"
|
||||||
|
|
||||||
namespace rfl {
|
namespace rfl::parsing {
|
||||||
namespace parsing {
|
|
||||||
|
|
||||||
/// CBOR requires us to explicitly set the number of fields in advance. Because
|
/// CBOR requires us to explicitly set the number of fields in advance.
|
||||||
/// of that, we require all of the fields and then set them to nullptr, if
|
/// Because of that, we require all of the fields and then set them to nullptr,
|
||||||
/// necessary.
|
/// if necessary.
|
||||||
template <class ProcessorsType, class... FieldTypes>
|
template <class ProcessorsType, class... FieldTypes>
|
||||||
requires AreReaderAndWriter<cbor::Reader, cbor::Writer,
|
requires AreReaderAndWriter<cbor::Reader, cbor::Writer,
|
||||||
NamedTuple<FieldTypes...>>
|
NamedTuple<FieldTypes...>>
|
||||||
struct Parser<cbor::Reader, cbor::Writer, NamedTuple<FieldTypes...>,
|
struct Parser<cbor::Reader, cbor::Writer, NamedTuple<FieldTypes...>,
|
||||||
ProcessorsType>
|
ProcessorsType>
|
||||||
: public NamedTupleParser<
|
: public NamedTupleParser<
|
||||||
@@ -22,37 +21,31 @@ struct Parser<cbor::Reader, cbor::Writer, NamedTuple<FieldTypes...>,
|
|||||||
/*_ignore_empty_containers=*/false,
|
/*_ignore_empty_containers=*/false,
|
||||||
/*_all_required=*/true,
|
/*_all_required=*/true,
|
||||||
/*_no_field_names=*/ProcessorsType::no_field_names_, ProcessorsType,
|
/*_no_field_names=*/ProcessorsType::no_field_names_, ProcessorsType,
|
||||||
FieldTypes...> {
|
FieldTypes...> {};
|
||||||
};
|
|
||||||
|
|
||||||
template <class ProcessorsType, class... Ts>
|
template <class ProcessorsType, class... Ts>
|
||||||
requires AreReaderAndWriter<cbor::Reader, cbor::Writer, rfl::Tuple<Ts...>>
|
requires AreReaderAndWriter<cbor::Reader, cbor::Writer, rfl::Tuple<Ts...>>
|
||||||
struct Parser<cbor::Reader, cbor::Writer, rfl::Tuple<Ts...>, ProcessorsType>
|
struct Parser<cbor::Reader, cbor::Writer, rfl::Tuple<Ts...>, ProcessorsType>
|
||||||
: public TupleParser<cbor::Reader, cbor::Writer,
|
: public TupleParser<cbor::Reader, cbor::Writer,
|
||||||
/*_ignore_empty_containers=*/false,
|
/*_ignore_empty_containers=*/false,
|
||||||
/*_all_required=*/true, ProcessorsType,
|
/*_all_required=*/true, ProcessorsType,
|
||||||
rfl::Tuple<Ts...>> {
|
rfl::Tuple<Ts...>> {};
|
||||||
};
|
|
||||||
|
|
||||||
template <class ProcessorsType, class... Ts>
|
template <class ProcessorsType, class... Ts>
|
||||||
requires AreReaderAndWriter<cbor::Reader, cbor::Writer, std::tuple<Ts...>>
|
requires AreReaderAndWriter<cbor::Reader, cbor::Writer, std::tuple<Ts...>>
|
||||||
struct Parser<cbor::Reader, cbor::Writer, std::tuple<Ts...>, ProcessorsType>
|
struct Parser<cbor::Reader, cbor::Writer, std::tuple<Ts...>, ProcessorsType>
|
||||||
: public TupleParser<cbor::Reader, cbor::Writer,
|
: public TupleParser<cbor::Reader, cbor::Writer,
|
||||||
/*_ignore_empty_containers=*/false,
|
/*_ignore_empty_containers=*/false,
|
||||||
/*_all_required=*/true, ProcessorsType,
|
/*_all_required=*/true, ProcessorsType,
|
||||||
std::tuple<Ts...>> {
|
std::tuple<Ts...>> {};
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace parsing
|
} // namespace rfl::parsing
|
||||||
} // namespace rfl
|
|
||||||
|
|
||||||
namespace rfl {
|
namespace rfl::cbor {
|
||||||
namespace cbor {
|
|
||||||
|
|
||||||
template <class T, class ProcessorsType>
|
template <class T, class ProcessorsType>
|
||||||
using Parser = parsing::Parser<Reader, Writer, T, ProcessorsType>;
|
using Parser = parsing::Parser<Reader, Writer, T, ProcessorsType>;
|
||||||
|
|
||||||
}
|
} // namespace rfl::cbor
|
||||||
} // namespace rfl
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+40
-114
@@ -1,10 +1,9 @@
|
|||||||
#ifndef RFL_CBOR_READER_HPP_
|
#ifndef RFL_CBOR_READER_HPP_
|
||||||
#define RFL_CBOR_READER_HPP_
|
#define RFL_CBOR_READER_HPP_
|
||||||
|
|
||||||
#include <cbor.h>
|
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <jsoncons/json.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
@@ -13,32 +12,35 @@
|
|||||||
#include "../Bytestring.hpp"
|
#include "../Bytestring.hpp"
|
||||||
#include "../Result.hpp"
|
#include "../Result.hpp"
|
||||||
#include "../always_false.hpp"
|
#include "../always_false.hpp"
|
||||||
|
#include "../internal/ptr_cast.hpp"
|
||||||
|
|
||||||
namespace rfl {
|
namespace rfl::cbor {
|
||||||
namespace cbor {
|
|
||||||
|
|
||||||
/// Please refer to https://intel.github.io/tinycbor/current/index.html
|
class Reader {
|
||||||
struct Reader {
|
public:
|
||||||
struct CBORInputArray {
|
struct CBORInputArray {
|
||||||
CborValue val_;
|
jsoncons::json* val_;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CBORInputObject {
|
struct CBORInputObject {
|
||||||
CborValue val_;
|
jsoncons::json* val_;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CBORInputVar {
|
struct CBORInputVar {
|
||||||
CborValue val_;
|
jsoncons::json* val_;
|
||||||
};
|
};
|
||||||
|
|
||||||
using InputArrayType = CBORInputArray;
|
using InputArrayType = CBORInputArray;
|
||||||
using InputObjectType = CBORInputObject;
|
using InputObjectType = CBORInputObject;
|
||||||
using InputVarType = CBORInputVar;
|
using InputVarType = CBORInputVar;
|
||||||
|
|
||||||
|
Reader() {}
|
||||||
|
|
||||||
|
~Reader() = default;
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
|
static constexpr bool has_custom_constructor =
|
||||||
T::from_cbor_obj(var);
|
(requires(InputVarType var) { T::from_cbor_obj(var); });
|
||||||
});
|
|
||||||
|
|
||||||
rfl::Result<InputVarType> get_field_from_array(
|
rfl::Result<InputVarType> get_field_from_array(
|
||||||
const size_t _idx, const InputArrayType& _arr) const noexcept;
|
const size_t _idx, const InputArrayType& _arr) const noexcept;
|
||||||
@@ -51,71 +53,43 @@ struct Reader {
|
|||||||
template <class T>
|
template <class T>
|
||||||
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>()) {
|
||||||
if (!cbor_value_is_text_string(&_var.val_)) {
|
if (!_var.val_->is_string()) {
|
||||||
return Error("Could not cast to string.");
|
return Error("Could not cast to string.");
|
||||||
}
|
}
|
||||||
std::string str;
|
return _var.val_->as<std::string>();
|
||||||
const auto err = get_string(&_var.val_, &str);
|
|
||||||
if (err != CborNoError) {
|
|
||||||
return Error(cbor_error_string(err));
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||||
rfl::Bytestring>()) {
|
rfl::Bytestring>()) {
|
||||||
if (!cbor_value_is_byte_string(&_var.val_)) {
|
if (!_var.val_->is_byte_string()) {
|
||||||
return Error("Could not cast to bytestring.");
|
return Error("Could not cast to bytestring.");
|
||||||
}
|
}
|
||||||
rfl::Bytestring bstr;
|
const auto vec = _var.val_->as<std::vector<uint8_t>>();
|
||||||
const auto err = get_bytestring(&_var.val_, &bstr);
|
return rfl::Bytestring(internal::ptr_cast<const std::byte*>(vec.data()),
|
||||||
if (err != CborNoError) {
|
vec.size());
|
||||||
return Error(cbor_error_string(err));
|
|
||||||
}
|
|
||||||
return bstr;
|
|
||||||
} 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 (!_var.val_->is_bool()) {
|
||||||
return rfl::Error("Could not cast to boolean.");
|
return rfl::Error("Could not cast to boolean.");
|
||||||
}
|
}
|
||||||
bool result = false;
|
return _var.val_->as<bool>();
|
||||||
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>>() ||
|
} 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 (_var.val_->is_double()) {
|
||||||
std::int64_t result = 0;
|
return static_cast<T>(_var.val_->as<double>());
|
||||||
const auto err = cbor_value_get_int64(&_var.val_, &result);
|
}
|
||||||
if (err != CborNoError) {
|
if (_var.val_->is_int64()) {
|
||||||
return Error(cbor_error_string(err));
|
return static_cast<T>(_var.val_->as<int64_t>());
|
||||||
}
|
}
|
||||||
return static_cast<T>(result);
|
if (_var.val_->is_uint64()) {
|
||||||
} else if (cbor_value_is_float(&_var.val_)) {
|
return static_cast<T>(_var.val_->as<uint64_t>());
|
||||||
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(
|
return rfl::Error(
|
||||||
"Could not cast to numeric value. The type must be integral, float "
|
"Could not cast to numeric value. The type must be integral, "
|
||||||
"or double.");
|
"float or double.");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rfl::Result<InputArrayType> to_array(
|
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept;
|
||||||
const InputVarType& _var) const noexcept;
|
|
||||||
|
|
||||||
rfl::Result<InputObjectType> to_object(
|
rfl::Result<InputObjectType> to_object(
|
||||||
const InputVarType& _var) const noexcept;
|
const InputVarType& _var) const noexcept;
|
||||||
@@ -123,25 +97,10 @@ struct Reader {
|
|||||||
template <class ArrayReader>
|
template <class ArrayReader>
|
||||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||||
const InputArrayType& _arr) const noexcept {
|
const InputArrayType& _arr) const noexcept {
|
||||||
InputVarType var;
|
for (auto& val : _arr.val_->array_range()) {
|
||||||
auto buffer = std::vector<char>();
|
const auto err = _array_reader.read(InputVarType{&val});
|
||||||
auto err = cbor_value_enter_container(&_arr.val_, &var.val_);
|
if (err) {
|
||||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
return err;
|
||||||
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(var);
|
|
||||||
if (err2) {
|
|
||||||
return err2;
|
|
||||||
}
|
|
||||||
err = cbor_value_advance(&var.val_);
|
|
||||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
|
||||||
return Error(cbor_error_string(err));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
@@ -150,34 +109,9 @@ struct Reader {
|
|||||||
template <class ObjectReader>
|
template <class ObjectReader>
|
||||||
std::optional<Error> read_object(const ObjectReader& _object_reader,
|
std::optional<Error> read_object(const ObjectReader& _object_reader,
|
||||||
const InputObjectType& _obj) const noexcept {
|
const InputObjectType& _obj) const noexcept {
|
||||||
size_t length = 0;
|
for (auto& kv : _obj.val_->object_range()) {
|
||||||
auto err = cbor_value_get_map_length(&_obj.val_, &length);
|
_object_reader.read(kv.key(), InputVarType{&kv.value()});
|
||||||
if (err != CborNoError) {
|
|
||||||
return Error(cbor_error_string(err));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
InputVarType var;
|
|
||||||
err = cbor_value_enter_container(&_obj.val_, &var.val_);
|
|
||||||
if (err != CborNoError) {
|
|
||||||
return Error(cbor_error_string(err));
|
|
||||||
}
|
|
||||||
|
|
||||||
auto buffer = std::string();
|
|
||||||
|
|
||||||
for (size_t i = 0; i < length; ++i) {
|
|
||||||
err = get_string(&var.val_, &buffer);
|
|
||||||
if (err != CborNoError) {
|
|
||||||
return Error(cbor_error_string(err));
|
|
||||||
}
|
|
||||||
err = cbor_value_advance(&var.val_);
|
|
||||||
if (err != CborNoError) {
|
|
||||||
return Error(cbor_error_string(err));
|
|
||||||
}
|
|
||||||
const auto name = std::string_view(buffer);
|
|
||||||
_object_reader.read(name, var);
|
|
||||||
cbor_value_advance(&var.val_);
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,16 +124,8 @@ struct Reader {
|
|||||||
return rfl::Error(e.what());
|
return rfl::Error(e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
CborError get_bytestring(const CborValue* _ptr,
|
|
||||||
rfl::Bytestring* _str) const noexcept;
|
|
||||||
|
|
||||||
CborError get_string(const CborValue* _ptr,
|
|
||||||
std::string* _str) const noexcept;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace cbor
|
} // namespace rfl::cbor
|
||||||
} // namespace rfl
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+23
-38
@@ -1,9 +1,9 @@
|
|||||||
#ifndef RFL_CBOR_WRITER_HPP_
|
#ifndef RFL_CBOR_WRITER_HPP_
|
||||||
#define RFL_CBOR_WRITER_HPP_
|
#define RFL_CBOR_WRITER_HPP_
|
||||||
|
|
||||||
#include <cbor.h>
|
#include <bit>
|
||||||
|
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <jsoncons_ext/cbor/cbor_encoder.hpp>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
@@ -18,22 +18,16 @@
|
|||||||
#include "../Ref.hpp"
|
#include "../Ref.hpp"
|
||||||
#include "../Result.hpp"
|
#include "../Result.hpp"
|
||||||
#include "../always_false.hpp"
|
#include "../always_false.hpp"
|
||||||
#include "../internal/ptr_cast.hpp"
|
|
||||||
|
|
||||||
namespace rfl {
|
namespace rfl::cbor {
|
||||||
namespace cbor {
|
|
||||||
|
|
||||||
class Writer {
|
class Writer {
|
||||||
public:
|
using Encoder = jsoncons::cbor::cbor_bytes_encoder;
|
||||||
struct CBOROutputArray {
|
|
||||||
CborEncoder* encoder_;
|
|
||||||
CborEncoder* parent_;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct CBOROutputObject {
|
public:
|
||||||
CborEncoder* encoder_;
|
struct CBOROutputArray {};
|
||||||
CborEncoder* parent_;
|
|
||||||
};
|
struct CBOROutputObject {};
|
||||||
|
|
||||||
struct CBOROutputVar {};
|
struct CBOROutputVar {};
|
||||||
|
|
||||||
@@ -41,7 +35,7 @@ class Writer {
|
|||||||
using OutputObjectType = CBOROutputObject;
|
using OutputObjectType = CBOROutputObject;
|
||||||
using OutputVarType = CBOROutputVar;
|
using OutputVarType = CBOROutputVar;
|
||||||
|
|
||||||
Writer(CborEncoder* _encoder);
|
Writer(Encoder* _encoder);
|
||||||
|
|
||||||
~Writer();
|
~Writer();
|
||||||
|
|
||||||
@@ -53,7 +47,7 @@ class Writer {
|
|||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||||
return new_value(_var, encoder_);
|
return new_value(_var);
|
||||||
}
|
}
|
||||||
|
|
||||||
OutputArrayType add_array_to_array(const size_t _size,
|
OutputArrayType add_array_to_array(const size_t _size,
|
||||||
@@ -73,15 +67,15 @@ class Writer {
|
|||||||
template <class T>
|
template <class T>
|
||||||
OutputVarType add_value_to_array(const T& _var,
|
OutputVarType add_value_to_array(const T& _var,
|
||||||
OutputArrayType* _parent) const noexcept {
|
OutputArrayType* _parent) const noexcept {
|
||||||
return new_value(_var, _parent->encoder_);
|
return new_value(_var);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
OutputVarType add_value_to_object(const std::string_view& _name,
|
OutputVarType add_value_to_object(const std::string_view& _name,
|
||||||
const T& _var,
|
const T& _var,
|
||||||
OutputObjectType* _parent) const noexcept {
|
OutputObjectType* _parent) const noexcept {
|
||||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
encoder_->key(_name);
|
||||||
return new_value(_var, _parent->encoder_);
|
return new_value(_var);
|
||||||
}
|
}
|
||||||
|
|
||||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
|
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
|
||||||
@@ -94,27 +88,23 @@ class Writer {
|
|||||||
void end_object(OutputObjectType* _obj) const noexcept;
|
void end_object(OutputObjectType* _obj) const noexcept;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
OutputArrayType new_array(const size_t _size,
|
OutputArrayType new_array(const size_t _size) const noexcept;
|
||||||
CborEncoder* _parent) const noexcept;
|
|
||||||
|
|
||||||
OutputObjectType new_object(const size_t _size,
|
OutputObjectType new_object(const size_t _size) const noexcept;
|
||||||
CborEncoder* _parent) const noexcept;
|
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
OutputVarType new_value(const T& _var, CborEncoder* _parent) const noexcept {
|
OutputVarType new_value(const T& _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>()) {
|
||||||
cbor_encode_text_string(_parent, _var.c_str(), _var.size());
|
encoder_->string_value(_var);
|
||||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||||
rfl::Bytestring>()) {
|
rfl::Bytestring>()) {
|
||||||
cbor_encode_byte_string(_parent,
|
encoder_->byte_string_value(_var);
|
||||||
internal::ptr_cast<const uint8_t*>(_var.c_str()),
|
|
||||||
_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>()) {
|
||||||
cbor_encode_boolean(_parent, _var);
|
encoder_->bool_value(_var);
|
||||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||||
cbor_encode_double(_parent, static_cast<double>(_var));
|
encoder_->double_value(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>>()) {
|
||||||
cbor_encode_int(_parent, static_cast<std::int64_t>(_var));
|
encoder_->int64_value(static_cast<std::int64_t>(_var));
|
||||||
} else {
|
} else {
|
||||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||||
}
|
}
|
||||||
@@ -123,13 +113,8 @@ class Writer {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
/// The underlying TinyCBOR encoder.
|
/// The underlying TinyCBOR encoder.
|
||||||
CborEncoder* const encoder_;
|
Encoder* const encoder_;
|
||||||
|
|
||||||
/// Contain all of the subobjects and subarrays.
|
|
||||||
const rfl::Box<std::vector<rfl::Box<CborEncoder>>> subencoders_;
|
|
||||||
};
|
};
|
||||||
|
} // namespace rfl::cbor
|
||||||
} // namespace cbor
|
|
||||||
} // namespace rfl
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -6,8 +6,7 @@
|
|||||||
#include "../io/load_bytes.hpp"
|
#include "../io/load_bytes.hpp"
|
||||||
#include "read.hpp"
|
#include "read.hpp"
|
||||||
|
|
||||||
namespace rfl {
|
namespace rfl::cbor {
|
||||||
namespace cbor {
|
|
||||||
|
|
||||||
template <class T, class... Ps>
|
template <class T, class... Ps>
|
||||||
Result<T> load(const std::string& _fname) {
|
Result<T> load(const std::string& _fname) {
|
||||||
@@ -17,7 +16,6 @@ Result<T> load(const std::string& _fname) {
|
|||||||
return rfl::io::load_bytes(_fname).and_then(read_bytes);
|
return rfl::io::load_bytes(_fname).and_then(read_bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace cbor
|
} // namespace rfl::cbor
|
||||||
} // namespace rfl
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+13
-32
@@ -1,57 +1,38 @@
|
|||||||
#ifndef RFL_CBOR_READ_HPP_
|
#ifndef RFL_CBOR_READ_HPP_
|
||||||
#define RFL_CBOR_READ_HPP_
|
#define RFL_CBOR_READ_HPP_
|
||||||
|
|
||||||
#include <cbor.h>
|
#include <bit>
|
||||||
|
|
||||||
#include <istream>
|
#include <istream>
|
||||||
|
#include <jsoncons/json.hpp>
|
||||||
|
#include <jsoncons_ext/cbor/decode_cbor.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "../Processors.hpp"
|
#include "../Processors.hpp"
|
||||||
#include "../internal/ptr_cast.hpp"
|
|
||||||
#include "../internal/wrap_in_rfl_array_t.hpp"
|
#include "../internal/wrap_in_rfl_array_t.hpp"
|
||||||
#include "Parser.hpp"
|
#include "Parser.hpp"
|
||||||
#include "Reader.hpp"
|
#include "Reader.hpp"
|
||||||
|
|
||||||
namespace rfl {
|
namespace rfl::cbor {
|
||||||
namespace cbor {
|
|
||||||
|
|
||||||
using InputObjectType = typename Reader::InputObjectType;
|
using InputObjectType = typename Reader::InputObjectType;
|
||||||
using InputVarType = typename Reader::InputVarType;
|
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 CBOR using reflection.
|
/// Parses an object from CBOR using reflection.
|
||||||
template <class T, class... Ps>
|
template <class T, class... Ps>
|
||||||
Result<internal::wrap_in_rfl_array_t<T>> read(const char* _bytes,
|
Result<internal::wrap_in_rfl_array_t<T>> read(const std::vector<char>& _bytes) {
|
||||||
const size_t _size) {
|
auto val = jsoncons::cbor::decode_cbor<jsoncons::json>(_bytes);
|
||||||
CborParser parser;
|
auto r = Reader();
|
||||||
InputVarType doc;
|
return Parser<T, Processors<Ps...>>::read(r, InputVarType{&val});
|
||||||
cbor_parser_init(internal::ptr_cast<const uint8_t*>(_bytes), _size, 0,
|
|
||||||
&parser, &doc.val_);
|
|
||||||
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 a stream.
|
/// Parses an object from a stream.
|
||||||
template <class T, class... Ps>
|
template <class T, class... Ps>
|
||||||
auto read(std::istream& _stream) {
|
Result<internal::wrap_in_rfl_array_t<T>> read(std::istream& _stream) {
|
||||||
std::istreambuf_iterator<char> begin(_stream), end;
|
auto val = jsoncons::cbor::decode_cbor<jsoncons::json>(_stream);
|
||||||
auto bytes = std::vector<char>(begin, end);
|
auto r = Reader();
|
||||||
return read<T, Ps...>(bytes.data(), bytes.size());
|
return Parser<T, Processors<Ps...>>::read(r, InputVarType{&val});
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace cbor
|
} // namespace rfl::cbor
|
||||||
} // namespace rfl
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -9,8 +9,7 @@
|
|||||||
#include "../io/save_bytes.hpp"
|
#include "../io/save_bytes.hpp"
|
||||||
#include "write.hpp"
|
#include "write.hpp"
|
||||||
|
|
||||||
namespace rfl {
|
namespace rfl::cbor {
|
||||||
namespace cbor {
|
|
||||||
|
|
||||||
template <class... Ps>
|
template <class... Ps>
|
||||||
Result<Nothing> save(const std::string& _fname, const auto& _obj) {
|
Result<Nothing> save(const std::string& _fname, const auto& _obj) {
|
||||||
@@ -20,7 +19,6 @@ Result<Nothing> save(const std::string& _fname, const auto& _obj) {
|
|||||||
return rfl::io::save_bytes(_fname, _obj, write_func);
|
return rfl::io::save_bytes(_fname, _obj, write_func);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace cbor
|
} // namespace rfl::cbor
|
||||||
} // namespace rfl
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+13
-31
@@ -1,9 +1,8 @@
|
|||||||
#ifndef RFL_CBOR_WRITE_HPP_
|
#ifndef RFL_CBOR_WRITE_HPP_
|
||||||
#define RFL_CBOR_WRITE_HPP_
|
#define RFL_CBOR_WRITE_HPP_
|
||||||
|
|
||||||
#include <cbor.h>
|
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <jsoncons_ext/cbor/cbor_encoder.hpp>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -13,37 +12,21 @@
|
|||||||
#include "../parsing/Parent.hpp"
|
#include "../parsing/Parent.hpp"
|
||||||
#include "Parser.hpp"
|
#include "Parser.hpp"
|
||||||
|
|
||||||
namespace rfl {
|
namespace rfl::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, internal::ptr_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.
|
/// Returns CBOR bytes.
|
||||||
template <class... Ps>
|
template <class... Ps>
|
||||||
std::vector<char> write(const auto& _obj) noexcept {
|
std::vector<char> write(const auto& _obj) noexcept {
|
||||||
std::vector<char> buffer(4096);
|
using T = std::remove_cvref_t<decltype(_obj)>;
|
||||||
CborEncoder encoder;
|
using ParentType = parsing::Parent<Writer>;
|
||||||
write_into_buffer<Ps...>(_obj, &encoder, &buffer);
|
std::vector<uint8_t> buffer;
|
||||||
const auto total_bytes_needed =
|
jsoncons::cbor::cbor_bytes_encoder encoder(buffer);
|
||||||
buffer.size() + cbor_encoder_get_extra_bytes_needed(&encoder);
|
const auto writer = Writer(&encoder);
|
||||||
if (total_bytes_needed != buffer.size()) {
|
Parser<T, Processors<Ps...>>::write(writer, _obj,
|
||||||
buffer.resize(total_bytes_needed);
|
typename ParentType::Root{});
|
||||||
write_into_buffer<Ps...>(_obj, &encoder, &buffer);
|
return std::vector<char>(
|
||||||
}
|
internal::ptr_cast<char*>(buffer.data()),
|
||||||
const auto length = cbor_encoder_get_buffer_size(
|
internal::ptr_cast<char*>(buffer.data() + buffer.size()));
|
||||||
&encoder, internal::ptr_cast<uint8_t*>(buffer.data()));
|
|
||||||
buffer.resize(length);
|
|
||||||
return buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes a CBOR into an ostream.
|
/// Writes a CBOR into an ostream.
|
||||||
@@ -54,7 +37,6 @@ std::ostream& write(const auto& _obj, std::ostream& _stream) noexcept {
|
|||||||
return _stream;
|
return _stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace cbor
|
} // namespace rfl::cbor
|
||||||
} // namespace rfl
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ namespace rfl::parsing {
|
|||||||
/// Because of that, we require all of the fields and then set them to nullptr,
|
/// Because of that, we require all of the fields and then set them to nullptr,
|
||||||
/// if necessary.
|
/// if necessary.
|
||||||
template <class ProcessorsType, class... FieldTypes>
|
template <class ProcessorsType, class... FieldTypes>
|
||||||
requires AreReaderAndWriter<ubjson::Reader, ubjson::Writer,
|
requires AreReaderAndWriter<ubjson::Reader, ubjson::Writer,
|
||||||
NamedTuple<FieldTypes...>>
|
NamedTuple<FieldTypes...>>
|
||||||
struct Parser<ubjson::Reader, ubjson::Writer, NamedTuple<FieldTypes...>,
|
struct Parser<ubjson::Reader, ubjson::Writer, NamedTuple<FieldTypes...>,
|
||||||
ProcessorsType>
|
ProcessorsType>
|
||||||
: public NamedTupleParser<
|
: public NamedTupleParser<
|
||||||
@@ -21,26 +21,23 @@ struct Parser<ubjson::Reader, ubjson::Writer, NamedTuple<FieldTypes...>,
|
|||||||
/*_ignore_empty_containers=*/false,
|
/*_ignore_empty_containers=*/false,
|
||||||
/*_all_required=*/true,
|
/*_all_required=*/true,
|
||||||
/*_no_field_names=*/ProcessorsType::no_field_names_, ProcessorsType,
|
/*_no_field_names=*/ProcessorsType::no_field_names_, ProcessorsType,
|
||||||
FieldTypes...> {
|
FieldTypes...> {};
|
||||||
};
|
|
||||||
|
|
||||||
template <class ProcessorsType, class... Ts>
|
template <class ProcessorsType, class... Ts>
|
||||||
requires AreReaderAndWriter<ubjson::Reader, ubjson::Writer, rfl::Tuple<Ts...>>
|
requires AreReaderAndWriter<ubjson::Reader, ubjson::Writer, rfl::Tuple<Ts...>>
|
||||||
struct Parser<ubjson::Reader, ubjson::Writer, rfl::Tuple<Ts...>, ProcessorsType>
|
struct Parser<ubjson::Reader, ubjson::Writer, rfl::Tuple<Ts...>, ProcessorsType>
|
||||||
: public TupleParser<ubjson::Reader, ubjson::Writer,
|
: public TupleParser<ubjson::Reader, ubjson::Writer,
|
||||||
/*_ignore_empty_containers=*/false,
|
/*_ignore_empty_containers=*/false,
|
||||||
/*_all_required=*/true, ProcessorsType,
|
/*_all_required=*/true, ProcessorsType,
|
||||||
rfl::Tuple<Ts...>> {
|
rfl::Tuple<Ts...>> {};
|
||||||
};
|
|
||||||
|
|
||||||
template <class ProcessorsType, class... Ts>
|
template <class ProcessorsType, class... Ts>
|
||||||
requires AreReaderAndWriter<ubjson::Reader, ubjson::Writer, std::tuple<Ts...>>
|
requires AreReaderAndWriter<ubjson::Reader, ubjson::Writer, std::tuple<Ts...>>
|
||||||
struct Parser<ubjson::Reader, ubjson::Writer, std::tuple<Ts...>, ProcessorsType>
|
struct Parser<ubjson::Reader, ubjson::Writer, std::tuple<Ts...>, ProcessorsType>
|
||||||
: public TupleParser<ubjson::Reader, ubjson::Writer,
|
: public TupleParser<ubjson::Reader, ubjson::Writer,
|
||||||
/*_ignore_empty_containers=*/false,
|
/*_ignore_empty_containers=*/false,
|
||||||
/*_all_required=*/true, ProcessorsType,
|
/*_all_required=*/true, ProcessorsType,
|
||||||
std::tuple<Ts...>> {
|
std::tuple<Ts...>> {};
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace rfl::parsing
|
} // namespace rfl::parsing
|
||||||
|
|
||||||
|
|||||||
+11
-84
@@ -4,71 +4,29 @@ namespace rfl::cbor {
|
|||||||
|
|
||||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
|
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
|
||||||
const size_t _idx, const InputArrayType& _arr) const noexcept {
|
const size_t _idx, const InputArrayType& _arr) const noexcept {
|
||||||
InputVarType var;
|
if (_idx >= _arr.val_->size()) {
|
||||||
auto err = cbor_value_enter_container(&_arr.val_, &var.val_);
|
return Error("Index out of range.");
|
||||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
|
||||||
return Error(cbor_error_string(err));
|
|
||||||
}
|
}
|
||||||
size_t length = 0;
|
return InputVarType{&_arr.val_->at(_idx)};
|
||||||
err = cbor_value_get_array_length(&_arr.val_, &length);
|
|
||||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
|
||||||
return Error(cbor_error_string(err));
|
|
||||||
}
|
|
||||||
if (_idx >= length) {
|
|
||||||
return Error("Index " + std::to_string(_idx) + " of of bounds.");
|
|
||||||
}
|
|
||||||
for (size_t i = 0; i < _idx; ++i) {
|
|
||||||
err = cbor_value_advance(&var.val_);
|
|
||||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
|
||||||
return Error(cbor_error_string(err));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return var;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
|
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
|
||||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||||
InputVarType var;
|
for (auto& kv : _obj.val_->object_range()) {
|
||||||
auto buffer = std::string();
|
if (kv.key() == _name) {
|
||||||
auto err = cbor_value_enter_container(&_obj.val_, &var.val_);
|
return InputVarType{&kv.value()};
|
||||||
if (err != CborNoError) {
|
};
|
||||||
return Error(cbor_error_string(err));
|
|
||||||
}
|
}
|
||||||
size_t length = 0;
|
return Error("Field name '" + _name + "' not found.");
|
||||||
err = cbor_value_get_map_length(&_obj.val_, &length);
|
|
||||||
if (err != CborNoError) {
|
|
||||||
return Error(cbor_error_string(err));
|
|
||||||
}
|
|
||||||
for (size_t i = 0; i < length; ++i) {
|
|
||||||
if (!cbor_value_is_text_string(&var.val_)) {
|
|
||||||
return Error("Expected the key to be a string value.");
|
|
||||||
}
|
|
||||||
err = get_string(&var.val_, &buffer);
|
|
||||||
if (err != CborNoError) {
|
|
||||||
return Error(cbor_error_string(err));
|
|
||||||
}
|
|
||||||
err = cbor_value_advance(&var.val_);
|
|
||||||
if (err != CborNoError) {
|
|
||||||
return Error(cbor_error_string(err));
|
|
||||||
}
|
|
||||||
if (_name == buffer) {
|
|
||||||
return var;
|
|
||||||
}
|
|
||||||
err = cbor_value_advance(&var.val_);
|
|
||||||
if (err != CborNoError) {
|
|
||||||
return Error(cbor_error_string(err));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Error("No field named '" + _name + "' was found.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Reader::is_empty(const InputVarType& _var) const noexcept {
|
bool Reader::is_empty(const InputVarType& _var) const noexcept {
|
||||||
return cbor_value_is_null(&_var.val_);
|
return _var.val_->is_null();
|
||||||
}
|
}
|
||||||
|
|
||||||
rfl::Result<Reader::InputArrayType> Reader::to_array(
|
rfl::Result<Reader::InputArrayType> Reader::to_array(
|
||||||
const InputVarType& _var) const noexcept {
|
const InputVarType& _var) const noexcept {
|
||||||
if (!cbor_value_is_array(&_var.val_)) {
|
if (!_var.val_->is_array()) {
|
||||||
return Error("Could not cast to an array.");
|
return Error("Could not cast to an array.");
|
||||||
}
|
}
|
||||||
return InputArrayType{_var.val_};
|
return InputArrayType{_var.val_};
|
||||||
@@ -76,41 +34,10 @@ rfl::Result<Reader::InputArrayType> Reader::to_array(
|
|||||||
|
|
||||||
rfl::Result<Reader::InputObjectType> Reader::to_object(
|
rfl::Result<Reader::InputObjectType> Reader::to_object(
|
||||||
const InputVarType& _var) const noexcept {
|
const InputVarType& _var) const noexcept {
|
||||||
if (!cbor_value_is_map(&_var.val_)) {
|
if (!_var.val_->is_object()) {
|
||||||
return Error("Could not cast to an object.");
|
return Error("Could not cast to an object.");
|
||||||
}
|
}
|
||||||
return InputObjectType{_var.val_};
|
return InputObjectType{_var.val_};
|
||||||
}
|
}
|
||||||
|
|
||||||
CborError Reader::get_bytestring(const CborValue* _ptr,
|
|
||||||
rfl::Bytestring* _str) const noexcept {
|
|
||||||
size_t length = 0;
|
|
||||||
auto err = cbor_value_get_string_length(_ptr, &length);
|
|
||||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
_str->resize(length);
|
|
||||||
if (length > 0) {
|
|
||||||
return cbor_value_copy_byte_string(
|
|
||||||
_ptr, reinterpret_cast<uint8_t*>(&((*_str)[0])), &length, NULL);
|
|
||||||
} else {
|
|
||||||
return CborNoError;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CborError Reader::get_string(const CborValue* _ptr,
|
|
||||||
std::string* _str) const noexcept {
|
|
||||||
size_t length = 0;
|
|
||||||
auto err = cbor_value_get_string_length(_ptr, &length);
|
|
||||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
_str->resize(length);
|
|
||||||
if (length > 0) {
|
|
||||||
return cbor_value_copy_text_string(_ptr, &((*_str)[0]), &length, NULL);
|
|
||||||
} else {
|
|
||||||
return CborNoError;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace rfl::cbor
|
} // namespace rfl::cbor
|
||||||
|
|||||||
+21
-25
@@ -2,82 +2,78 @@
|
|||||||
|
|
||||||
namespace rfl::cbor {
|
namespace rfl::cbor {
|
||||||
|
|
||||||
Writer::Writer(CborEncoder* _encoder) : encoder_(_encoder) {}
|
Writer::Writer(Encoder* _encoder) : encoder_(_encoder) {}
|
||||||
|
|
||||||
Writer::~Writer() = default;
|
Writer::~Writer() = default;
|
||||||
|
|
||||||
Writer::OutputArrayType Writer::array_as_root(
|
Writer::OutputArrayType Writer::array_as_root(
|
||||||
const size_t _size) const noexcept {
|
const size_t _size) const noexcept {
|
||||||
return new_array(_size, encoder_);
|
return new_array(_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
Writer::OutputObjectType Writer::object_as_root(
|
Writer::OutputObjectType Writer::object_as_root(
|
||||||
const size_t _size) const noexcept {
|
const size_t _size) const noexcept {
|
||||||
return new_object(_size, encoder_);
|
return new_object(_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
Writer::OutputVarType Writer::null_as_root() const noexcept {
|
Writer::OutputVarType Writer::null_as_root() const noexcept {
|
||||||
cbor_encode_null(encoder_);
|
encoder_->null_value();
|
||||||
return OutputVarType{};
|
return OutputVarType{};
|
||||||
}
|
}
|
||||||
|
|
||||||
Writer::OutputArrayType Writer::add_array_to_array(
|
Writer::OutputArrayType Writer::add_array_to_array(
|
||||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||||
return new_array(_size, _parent->encoder_);
|
return new_array(_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
Writer::OutputArrayType Writer::add_array_to_object(
|
Writer::OutputArrayType Writer::add_array_to_object(
|
||||||
const std::string_view& _name, const size_t _size,
|
const std::string_view& _name, const size_t _size,
|
||||||
OutputObjectType* _parent) const noexcept {
|
OutputObjectType* _parent) const noexcept {
|
||||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
encoder_->key(_name);
|
||||||
return new_array(_size, _parent->encoder_);
|
return new_array(_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
Writer::OutputObjectType Writer::add_object_to_array(
|
Writer::OutputObjectType Writer::add_object_to_array(
|
||||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||||
return new_object(_size, _parent->encoder_);
|
return new_object(_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
Writer::OutputObjectType Writer::add_object_to_object(
|
Writer::OutputObjectType Writer::add_object_to_object(
|
||||||
const std::string_view& _name, const size_t _size,
|
const std::string_view& _name, const size_t _size,
|
||||||
OutputObjectType* _parent) const noexcept {
|
OutputObjectType* _parent) const noexcept {
|
||||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
encoder_->key(_name);
|
||||||
return new_object(_size, _parent->encoder_);
|
return new_object(_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
Writer::OutputVarType Writer::add_null_to_array(
|
Writer::OutputVarType Writer::add_null_to_array(
|
||||||
OutputArrayType* _parent) const noexcept {
|
OutputArrayType* _parent) const noexcept {
|
||||||
cbor_encode_null(_parent->encoder_);
|
encoder_->null_value();
|
||||||
return OutputVarType{};
|
return OutputVarType{};
|
||||||
}
|
}
|
||||||
|
|
||||||
Writer::OutputVarType Writer::add_null_to_object(
|
Writer::OutputVarType Writer::add_null_to_object(
|
||||||
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
|
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
|
||||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
encoder_->key(_name);
|
||||||
cbor_encode_null(_parent->encoder_);
|
encoder_->null_value();
|
||||||
return OutputVarType{};
|
return OutputVarType{};
|
||||||
}
|
}
|
||||||
|
|
||||||
void Writer::end_array(OutputArrayType* _arr) const noexcept {
|
void Writer::end_array(OutputArrayType* _arr) const noexcept {
|
||||||
cbor_encoder_close_container(_arr->parent_, _arr->encoder_);
|
encoder_->end_array();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Writer::end_object(OutputObjectType* _obj) const noexcept {
|
void Writer::end_object(OutputObjectType* _obj) const noexcept {
|
||||||
cbor_encoder_close_container(_obj->parent_, _obj->encoder_);
|
encoder_->end_object();
|
||||||
}
|
}
|
||||||
|
|
||||||
Writer::OutputArrayType Writer::new_array(const size_t _size,
|
Writer::OutputArrayType Writer::new_array(const size_t _size) const noexcept {
|
||||||
CborEncoder* _parent) const noexcept {
|
encoder_->begin_array(_size);
|
||||||
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
|
return OutputArrayType{};
|
||||||
cbor_encoder_create_array(_parent, subencoders_->back().get(), _size);
|
|
||||||
return OutputArrayType{subencoders_->back().get(), _parent};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Writer::OutputObjectType Writer::new_object(
|
Writer::OutputObjectType Writer::new_object(const size_t _size) const noexcept {
|
||||||
const size_t _size, CborEncoder* _parent) const noexcept {
|
encoder_->begin_object(_size);
|
||||||
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
|
return OutputObjectType{};
|
||||||
cbor_encoder_create_map(_parent, subencoders_->back().get(), _size);
|
|
||||||
return OutputObjectType{subencoders_->back().get(), _parent};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace rfl::cbor
|
} // namespace rfl::cbor
|
||||||
|
|||||||
@@ -8,9 +8,6 @@ add_executable(
|
|||||||
)
|
)
|
||||||
target_precompile_headers(reflect-cpp-cbor-tests PRIVATE [["rfl.hpp"]] <iostream> <string> <functional> <gtest/gtest.h>)
|
target_precompile_headers(reflect-cpp-cbor-tests PRIVATE [["rfl.hpp"]] <iostream> <string> <functional> <gtest/gtest.h>)
|
||||||
|
|
||||||
target_include_directories(reflect-cpp-cbor-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include")
|
|
||||||
target_include_directories(reflect-cpp-cbor-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include/tinycbor")
|
|
||||||
|
|
||||||
target_link_libraries(
|
target_link_libraries(
|
||||||
reflect-cpp-cbor-tests
|
reflect-cpp-cbor-tests
|
||||||
PRIVATE
|
PRIVATE
|
||||||
|
|||||||
@@ -47,10 +47,6 @@
|
|||||||
"name": "simdjson",
|
"name": "simdjson",
|
||||||
"version>=": "3.9.3"
|
"version>=": "3.9.3"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"name": "tinycbor",
|
|
||||||
"version>=": "0.6.0"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"name": "tomlplusplus",
|
"name": "tomlplusplus",
|
||||||
"version>=": "3.4.0"
|
"version>=": "3.4.0"
|
||||||
|
|||||||
Reference in New Issue
Block a user