mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 18:56:19 +08:00
@@ -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.");
|
||||
|
||||
@@ -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.");
|
||||
|
||||
Reference in New Issue
Block a user