Added support for bytestrings; #69 (#132)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2024-06-30 20:41:53 +02:00
committed by GitHub
parent 005f9a1c52
commit fae5e5ff58
18 changed files with 243 additions and 20 deletions
+13 -2
View File
@@ -3,11 +3,13 @@
#include <msgpack.h>
#include <cstddef>
#include <exception>
#include <string>
#include <string_view>
#include <type_traits>
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
@@ -20,8 +22,9 @@ struct Reader {
using InputVarType = msgpack_object;
template <class T>
static constexpr bool has_custom_constructor =
(requires(InputVarType var) { T::from_msgpack_obj(var); });
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
T::from_msgpack_obj(var);
});
rfl::Result<InputVarType> get_field_from_array(
const size_t _idx, const InputArrayType _arr) const noexcept {
@@ -61,6 +64,14 @@ struct Reader {
}
const auto str = _var.via.str;
return std::string(str.ptr, str.size);
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
if (type != MSGPACK_OBJECT_BIN) {
return Error("Could not cast to a bytestring.");
}
const auto bin = _var.via.bin;
return rfl::Bytestring(reinterpret_cast<const std::byte*>(bin.ptr),
bin.size);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (type != MSGPACK_OBJECT_BOOLEAN) {
return Error("Could not cast to boolean.");
+8 -4
View File
@@ -125,18 +125,22 @@ class Writer {
template <class T>
OutputVarType new_value(const T& _var) const noexcept {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
using Type = std::remove_cvref_t<T>;
if constexpr (std::is_same<Type, std::string>()) {
msgpack_pack_str(pk_, _var.size());
msgpack_pack_str_body(pk_, _var.c_str(), _var.size());
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
} else if constexpr (std::is_same<Type, rfl::Bytestring>()) {
msgpack_pack_bin(pk_, _var.size());
msgpack_pack_bin_body(pk_, _var.c_str(), _var.size());
} else if constexpr (std::is_same<Type, bool>()) {
if (_var) {
msgpack_pack_true(pk_);
} else {
msgpack_pack_false(pk_);
}
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
} else if constexpr (std::is_floating_point<Type>()) {
msgpack_pack_double(pk_, static_cast<double>(_var));
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
} else if constexpr (std::is_integral<Type>()) {
msgpack_pack_int64(pk_, static_cast<std::int64_t>(_var));
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");