mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-02 16:16:19 +08:00
Use static_cast for the pointer transformations (#298)
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include <type_traits>
|
||||
|
||||
#include "internal/is_array.hpp"
|
||||
#include "internal/ptr_cast.hpp"
|
||||
#include "internal/to_std_array.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -315,19 +316,19 @@ class Result {
|
||||
}
|
||||
|
||||
T& get_t() noexcept {
|
||||
return *std::launder((reinterpret_cast<T*>(t_or_err_.data())));
|
||||
return *std::launder(reinterpret_cast<T*>(t_or_err_.data()));
|
||||
}
|
||||
|
||||
const T& get_t() const noexcept {
|
||||
return *std::launder((reinterpret_cast<const T*>(t_or_err_.data())));
|
||||
return *std::launder(reinterpret_cast<const T*>(t_or_err_.data()));
|
||||
}
|
||||
|
||||
Error& get_err() noexcept {
|
||||
return *std::launder((reinterpret_cast<Error*>(t_or_err_.data())));
|
||||
return *std::launder(reinterpret_cast<Error*>(t_or_err_.data()));
|
||||
}
|
||||
|
||||
const Error& get_err() const noexcept {
|
||||
return *std::launder((reinterpret_cast<const Error*>(t_or_err_.data())));
|
||||
return *std::launder(reinterpret_cast<const Error*>(t_or_err_.data()));
|
||||
}
|
||||
|
||||
void move_from_other(Result<T>& _other) noexcept {
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <utility>
|
||||
|
||||
#include "internal/nth_element_t.hpp"
|
||||
#include "internal/ptr_cast.hpp"
|
||||
#include "internal/tuple/calculate_positions.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -59,14 +60,14 @@ class Tuple {
|
||||
template <int _index>
|
||||
constexpr auto& get() {
|
||||
using Type = internal::nth_element_t<_index, Types...>;
|
||||
return *std::bit_cast<Type*>(data_.data() + pos<_index>());
|
||||
return *internal::ptr_cast<Type*>(data_.data() + pos<_index>());
|
||||
}
|
||||
|
||||
/// Gets an element by index.
|
||||
template <int _index>
|
||||
constexpr const auto& get() const {
|
||||
using Type = internal::nth_element_t<_index, Types...>;
|
||||
return *std::bit_cast<const Type*>(data_.data() + pos<_index>());
|
||||
return *internal::ptr_cast<const Type*>(data_.data() + pos<_index>());
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
@@ -100,8 +101,7 @@ class Tuple {
|
||||
};
|
||||
return [&]<int... _is>(std::integer_sequence<int, _is...>) {
|
||||
return (true && ... && is_same(std::integral_constant<int, _is>{}));
|
||||
}
|
||||
(std::make_integer_sequence<int, sizeof...(Types)>());
|
||||
}(std::make_integer_sequence<int, sizeof...(Types)>());
|
||||
}
|
||||
|
||||
/// Three-way comparison operator.
|
||||
@@ -122,8 +122,7 @@ class Tuple {
|
||||
auto ordering = std::strong_ordering::equivalent;
|
||||
(compare(&ordering, std::integral_constant<int, _is>{}), ...);
|
||||
return ordering;
|
||||
}
|
||||
(std::make_integer_sequence<int, sizeof...(Types)>());
|
||||
}(std::make_integer_sequence<int, sizeof...(Types)>());
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
+10
-8
@@ -43,16 +43,18 @@ class Variant {
|
||||
struct TypeWrapper {};
|
||||
|
||||
public:
|
||||
Variant() {
|
||||
Variant() : index_(IndexType()), data_(DataType()) {
|
||||
using FirstAlternative = internal::nth_element_t<0, AlternativeTypes...>;
|
||||
move_from_type(FirstAlternative());
|
||||
}
|
||||
|
||||
Variant(const Variant<AlternativeTypes...>& _other) {
|
||||
Variant(const Variant<AlternativeTypes...>& _other)
|
||||
: index_(IndexType()), data_(DataType()) {
|
||||
copy_from_other(_other);
|
||||
}
|
||||
|
||||
Variant(Variant<AlternativeTypes...>&& _other) noexcept {
|
||||
Variant(Variant<AlternativeTypes...>&& _other) noexcept
|
||||
: index_(IndexType()), data_(DataType()) {
|
||||
move_from_other(std::move(_other));
|
||||
}
|
||||
|
||||
@@ -60,7 +62,7 @@ class Variant {
|
||||
typename std::enable_if<internal::variant::is_alternative_type<
|
||||
T, AlternativeTypes...>(),
|
||||
bool>::type = true>
|
||||
Variant(const T& _t) {
|
||||
Variant(const T& _t) : index_(IndexType()), data_(DataType()) {
|
||||
copy_from_type(_t);
|
||||
}
|
||||
|
||||
@@ -68,7 +70,7 @@ class Variant {
|
||||
typename std::enable_if<internal::variant::is_alternative_type<
|
||||
T, AlternativeTypes...>(),
|
||||
bool>::type = true>
|
||||
Variant(T&& _t) noexcept {
|
||||
Variant(T&& _t) noexcept : index_(IndexType()), data_(DataType()) {
|
||||
move_from_type(std::forward<T>(_t));
|
||||
}
|
||||
|
||||
@@ -80,7 +82,7 @@ class Variant {
|
||||
auto t = T{std::forward<Args>(_args)...};
|
||||
destroy_if_necessary();
|
||||
move_from_type(std::move(t));
|
||||
return *std::launder(reinterpret_cast<T*>(data_.data()));
|
||||
return *internal::ptr_cast<T*>(data_.data());
|
||||
}
|
||||
|
||||
/// Emplaces a new element into the variant.
|
||||
@@ -384,13 +386,13 @@ class Variant {
|
||||
template <IndexType _i>
|
||||
auto& get_alternative() noexcept {
|
||||
using CurrentType = internal::nth_element_t<_i, AlternativeTypes...>;
|
||||
return *std::launder(reinterpret_cast<CurrentType*>(data_.data()));
|
||||
return *internal::ptr_cast<CurrentType*>(data_.data());
|
||||
}
|
||||
|
||||
template <IndexType _i>
|
||||
const auto& get_alternative() const noexcept {
|
||||
using CurrentType = internal::nth_element_t<_i, AlternativeTypes...>;
|
||||
return *std::launder(reinterpret_cast<const CurrentType*>(data_.data()));
|
||||
return *internal::ptr_cast<const CurrentType*>(data_.data());
|
||||
}
|
||||
|
||||
void move_from_other(Variant<AlternativeTypes...>&& _other) noexcept {
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include <bson/bson.h>
|
||||
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <concepts>
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
@@ -23,6 +22,7 @@
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace bson {
|
||||
@@ -46,9 +46,8 @@ struct Reader {
|
||||
using InputVarType = BSONInputVar;
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
|
||||
T::from_bson_obj(var);
|
||||
});
|
||||
static constexpr bool has_custom_constructor =
|
||||
(requires(InputVarType var) { T::from_bson_obj(var); });
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_array(
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept;
|
||||
@@ -85,7 +84,7 @@ struct Reader {
|
||||
"bytestring.");
|
||||
}
|
||||
return rfl::Bytestring(
|
||||
std::bit_cast<const std::byte*>(value.v_binary.data),
|
||||
internal::ptr_cast<const std::byte*>(value.v_binary.data),
|
||||
value.v_binary.data_len);
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (btype != BSON_TYPE_BOOL) {
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <bson/bson.h>
|
||||
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
@@ -20,6 +19,7 @@
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace bson {
|
||||
@@ -104,7 +104,7 @@ class Writer {
|
||||
rfl::Bytestring>()) {
|
||||
bson_array_builder_append_binary(
|
||||
_parent->val_, BSON_SUBTYPE_BINARY,
|
||||
std::bit_cast<const uint8_t*>(_var.c_str()),
|
||||
internal::ptr_cast<const uint8_t*>(_var.c_str()),
|
||||
static_cast<uint32_t>(_var.size()));
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
bson_array_builder_append_bool(_parent->val_, _var);
|
||||
@@ -134,7 +134,7 @@ class Writer {
|
||||
rfl::Bytestring>()) {
|
||||
bson_append_binary(_parent->val_, _name.data(),
|
||||
static_cast<int>(_name.size()), BSON_SUBTYPE_BINARY,
|
||||
std::bit_cast<const uint8_t*>(_var.c_str()),
|
||||
internal::ptr_cast<const uint8_t*>(_var.c_str()),
|
||||
static_cast<uint32_t>(_var.size()));
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
bson_append_bool(_parent->val_, _name.data(),
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
|
||||
#include <bson/bson.h>
|
||||
|
||||
#include <bit>
|
||||
#include <istream>
|
||||
#include <string>
|
||||
|
||||
#include "../Processors.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
#include "../internal/wrap_in_rfl_array_t.hpp"
|
||||
#include "Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
@@ -42,7 +42,7 @@ auto read(const uint8_t* _bytes, const size_t _size) {
|
||||
/// Parses an BSON object using reflection.
|
||||
template <class T, class... Ps>
|
||||
auto read(const char* _bytes, const size_t _size) {
|
||||
return read<T, Ps...>(std::bit_cast<const uint8_t*>(_bytes), _size);
|
||||
return read<T, Ps...>(internal::ptr_cast<const uint8_t*>(_bytes), _size);
|
||||
}
|
||||
|
||||
/// Parses an object from BSON using reflection.
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
|
||||
#include <bson/bson.h>
|
||||
|
||||
#include <bit>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "../Processors.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
@@ -45,8 +45,8 @@ std::pair<uint8_t*, size_t> to_buffer(const auto& _obj) noexcept {
|
||||
template <class... Ps>
|
||||
std::vector<char> write(const auto& _obj) noexcept {
|
||||
auto [buf, len] = to_buffer<Ps...>(_obj);
|
||||
const auto result = std::vector<char>(std::bit_cast<char*>(buf),
|
||||
std::bit_cast<char*>(buf) + len);
|
||||
const auto result = std::vector<char>(internal::ptr_cast<char*>(buf),
|
||||
internal::ptr_cast<char*>(buf) + len);
|
||||
bson_free(buf);
|
||||
return result;
|
||||
}
|
||||
@@ -55,7 +55,7 @@ std::vector<char> write(const auto& _obj) noexcept {
|
||||
template <class... Ps>
|
||||
std::ostream& write(const auto& _obj, std::ostream& _stream) noexcept {
|
||||
auto [buf, len] = to_buffer<Ps...>(_obj);
|
||||
_stream.write(std::bit_cast<const char*>(buf), len);
|
||||
_stream.write(internal::ptr_cast<const char*>(buf), len);
|
||||
bson_free(buf);
|
||||
return _stream;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <cbor.h>
|
||||
|
||||
#include <bit>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
@@ -19,6 +18,7 @@
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace cbor {
|
||||
@@ -106,8 +106,9 @@ class Writer {
|
||||
cbor_encode_text_string(_parent, _var.c_str(), _var.size());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
cbor_encode_byte_string(
|
||||
_parent, std::bit_cast<const uint8_t*>(_var.c_str()), _var.size());
|
||||
cbor_encode_byte_string(_parent,
|
||||
internal::ptr_cast<const uint8_t*>(_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>>()) {
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
|
||||
#include <cbor.h>
|
||||
|
||||
#include <bit>
|
||||
#include <istream>
|
||||
#include <string>
|
||||
|
||||
#include "../Processors.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
#include "../internal/wrap_in_rfl_array_t.hpp"
|
||||
#include "Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
@@ -31,8 +31,8 @@ Result<internal::wrap_in_rfl_array_t<T>> read(const char* _bytes,
|
||||
const size_t _size) {
|
||||
CborParser parser;
|
||||
InputVarType doc;
|
||||
cbor_parser_init(std::bit_cast<const uint8_t*>(_bytes), _size, 0, &parser,
|
||||
&doc.val_);
|
||||
cbor_parser_init(internal::ptr_cast<const uint8_t*>(_bytes), _size, 0,
|
||||
&parser, &doc.val_);
|
||||
auto result = read<T, Ps...>(doc);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
|
||||
#include <cbor.h>
|
||||
|
||||
#include <bit>
|
||||
#include <cstdint>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
@@ -21,7 +21,7 @@ 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, std::bit_cast<uint8_t*>(_buffer->data()),
|
||||
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,
|
||||
@@ -41,7 +41,7 @@ std::vector<char> write(const auto& _obj) noexcept {
|
||||
write_into_buffer<Ps...>(_obj, &encoder, &buffer);
|
||||
}
|
||||
const auto length = cbor_encoder_get_buffer_size(
|
||||
&encoder, std::bit_cast<uint8_t*>(buffer.data()));
|
||||
&encoder, internal::ptr_cast<uint8_t*>(buffer.data()));
|
||||
buffer.resize(length);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
@@ -17,6 +16,7 @@
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
@@ -65,7 +65,7 @@ struct Reader {
|
||||
return rfl::Error("Could not cast to a bytestring.");
|
||||
}
|
||||
const auto blob = _var.AsBlob();
|
||||
return rfl::Bytestring(std::bit_cast<const std::byte*>(blob.data()),
|
||||
return rfl::Bytestring(internal::ptr_cast<const std::byte*>(blob.data()),
|
||||
blob.size());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!_var.IsBool()) {
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <bit>
|
||||
#include <istream>
|
||||
#include <vector>
|
||||
|
||||
#include "../Processors.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -27,7 +27,7 @@ auto read(const InputVarType& _obj) {
|
||||
template <class T, class... Ps>
|
||||
auto read(const char* _bytes, const size_t _size) {
|
||||
const InputVarType root =
|
||||
flexbuffers::GetRoot(std::bit_cast<const uint8_t*>(_bytes), _size);
|
||||
flexbuffers::GetRoot(internal::ptr_cast<const uint8_t*>(_bytes), _size);
|
||||
return read<T, Ps...>(root);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
@@ -11,6 +10,7 @@
|
||||
|
||||
#include "../Processors.hpp"
|
||||
#include "../Ref.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
@@ -32,7 +32,7 @@ std::vector<uint8_t> to_buffer(const auto& _obj) {
|
||||
template <class... Ps>
|
||||
std::vector<char> write(const auto& _obj) {
|
||||
const auto buffer = to_buffer<Ps...>(_obj);
|
||||
const auto data = std::bit_cast<const char*>(buffer.data());
|
||||
const auto data = internal::ptr_cast<const char*>(buffer.data());
|
||||
return std::vector<char>(data, data + buffer.size());
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ std::vector<char> write(const auto& _obj) {
|
||||
template <class... Ps>
|
||||
std::ostream& write(const auto& _obj, std::ostream& _stream) {
|
||||
const auto buffer = to_buffer<Ps...>(_obj);
|
||||
const auto data = std::bit_cast<const char*>(buffer.data());
|
||||
const auto data = internal::ptr_cast<const char*>(buffer.data());
|
||||
_stream.write(data, buffer.size());
|
||||
return _stream;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef RFL_INTERNAL_PTRCAST_HPP_
|
||||
#define RFL_INTERNAL_PTRCAST_HPP_
|
||||
|
||||
#include <bit>
|
||||
|
||||
namespace rfl::internal {
|
||||
|
||||
/// Normally, we would use std::launder(reinterpret_cast<...>(...)),
|
||||
/// but there are weird issues on GCC 12 under certain compiler settings,
|
||||
/// so we are using this workaround instead.
|
||||
|
||||
template <class T1, class T2>
|
||||
inline T1 ptr_cast(T2* _ptr) {
|
||||
return static_cast<T1>(static_cast<void*>(_ptr));
|
||||
}
|
||||
|
||||
template <class T1, class T2>
|
||||
inline T1 ptr_cast(const T2* _ptr) {
|
||||
return static_cast<T1>(static_cast<const void*>(_ptr));
|
||||
}
|
||||
|
||||
} // namespace rfl::internal
|
||||
|
||||
#endif
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <msgpack.h>
|
||||
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <string>
|
||||
@@ -13,6 +12,7 @@
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace msgpack {
|
||||
@@ -23,9 +23,8 @@ 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;
|
||||
@@ -50,7 +49,7 @@ struct Reader {
|
||||
return Error("Could not cast to a bytestring.");
|
||||
}
|
||||
const auto bin = _var.via.bin;
|
||||
return rfl::Bytestring(std::bit_cast<const std::byte*>(bin.ptr),
|
||||
return rfl::Bytestring(internal::ptr_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) {
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#define RFL_PARSING_NAMEDTUPLEPARSER_HPP_
|
||||
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <tuple>
|
||||
@@ -20,6 +19,7 @@
|
||||
#include "../internal/is_skip.hpp"
|
||||
#include "../internal/no_duplicate_field_names.hpp"
|
||||
#include "../internal/nth_element_t.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
#include "../internal/strings/replace_all.hpp"
|
||||
#include "../to_view.hpp"
|
||||
#include "AreReaderAndWriter.hpp"
|
||||
@@ -40,7 +40,7 @@ namespace parsing {
|
||||
|
||||
template <class R, class W, bool _ignore_empty_containers, bool _all_required,
|
||||
bool _no_field_names, class ProcessorsType, class... FieldTypes>
|
||||
requires AreReaderAndWriter<R, W, NamedTuple<FieldTypes...>>
|
||||
requires AreReaderAndWriter<R, W, NamedTuple<FieldTypes...>>
|
||||
struct NamedTupleParser {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
|
||||
@@ -86,7 +86,7 @@ struct NamedTupleParser {
|
||||
internal::no_duplicate_field_names<typename NamedTupleType::Fields>());
|
||||
alignas(NamedTuple<FieldTypes...>) unsigned char
|
||||
buf[sizeof(NamedTuple<FieldTypes...>)];
|
||||
auto ptr = std::bit_cast<NamedTuple<FieldTypes...>*>(&buf);
|
||||
auto ptr = internal::ptr_cast<NamedTuple<FieldTypes...>*>(&buf);
|
||||
auto view = rfl::to_view(*ptr);
|
||||
using ViewType = std::remove_cvref_t<decltype(view)>;
|
||||
const auto [set, err] =
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
#include "ArrayReader.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
@@ -19,7 +20,7 @@
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
template <class R, class W, class T, size_t _size, class ProcessorsType>
|
||||
requires AreReaderAndWriter<R, W, std::array<T, _size>>
|
||||
requires AreReaderAndWriter<R, W, std::array<T, _size>>
|
||||
struct Parser<R, W, std::array<T, _size>, ProcessorsType> {
|
||||
public:
|
||||
using InputArrayType = typename R::InputArrayType;
|
||||
@@ -33,7 +34,7 @@ struct Parser<R, W, std::array<T, _size>, ProcessorsType> {
|
||||
[&](const InputArrayType& _arr) -> Result<std::array<T, _size>> {
|
||||
alignas(
|
||||
std::array<T, _size>) unsigned char buf[sizeof(std::array<T, _size>)];
|
||||
auto ptr = std::bit_cast<std::array<T, _size>*>(&buf);
|
||||
auto ptr = internal::ptr_cast<std::array<T, _size>*>(&buf);
|
||||
const auto array_reader =
|
||||
ArrayReader<R, W, ProcessorsType, T, _size>(&_r, ptr);
|
||||
auto err = _r.read_array(array_reader, _arr);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#ifndef RFL_PARSING_PARSER_DEFAULT_HPP_
|
||||
#define RFL_PARSING_PARSER_DEFAULT_HPP_
|
||||
|
||||
#include <bit>
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
@@ -19,6 +18,7 @@
|
||||
#include "../internal/is_underlying_enums_v.hpp"
|
||||
#include "../internal/is_validator.hpp"
|
||||
#include "../internal/processed_t.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
#include "../internal/to_ptr_named_tuple.hpp"
|
||||
#include "../to_view.hpp"
|
||||
#include "../type_name_t.hpp"
|
||||
@@ -34,7 +34,7 @@ namespace parsing {
|
||||
|
||||
/// Default case - anything that cannot be explicitly matched.
|
||||
template <class R, class W, class T, class ProcessorsType>
|
||||
requires AreReaderAndWriter<R, W, T>
|
||||
requires AreReaderAndWriter<R, W, T>
|
||||
struct Parser {
|
||||
public:
|
||||
using InputVarType = typename R::InputVarType;
|
||||
@@ -256,7 +256,7 @@ struct Parser {
|
||||
/// fields might not be default-constructible.
|
||||
static Result<T> read_struct(const R& _r, const InputVarType& _var) {
|
||||
alignas(T) unsigned char buf[sizeof(T)]{};
|
||||
auto ptr = std::bit_cast<T*>(&buf);
|
||||
auto ptr = internal::ptr_cast<T*>(&buf);
|
||||
auto view = ProcessorsType::template process<T>(to_view(*ptr));
|
||||
using ViewType = std::remove_cvref_t<decltype(view)>;
|
||||
const auto [set, err] =
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "../Tuple.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/nth_element_t.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "TupleReader.hpp"
|
||||
#include "call_destructors_on_tuple_where_necessary.hpp"
|
||||
@@ -20,7 +21,7 @@ namespace rfl::parsing {
|
||||
|
||||
template <class R, class W, bool _ignore_empty_containers, bool _all_required,
|
||||
class ProcessorsType, class TupleType>
|
||||
requires AreReaderAndWriter<R, W, TupleType>
|
||||
requires AreReaderAndWriter<R, W, TupleType>
|
||||
struct TupleParser {
|
||||
public:
|
||||
using InputArrayType = typename R::InputArrayType;
|
||||
@@ -31,8 +32,8 @@ struct TupleParser {
|
||||
static Result<TupleType> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
const auto parse = [&](const InputArrayType& _arr) -> Result<TupleType> {
|
||||
alignas(TupleType) unsigned char buf[sizeof(TupleType)];
|
||||
auto ptr = std::bit_cast<TupleType*>(&buf);
|
||||
alignas(TupleType) unsigned char buf[sizeof(TupleType)]{};
|
||||
auto ptr = internal::ptr_cast<TupleType*>(&buf);
|
||||
const auto tuple_reader =
|
||||
TupleReader<R, W, TupleType, _ignore_empty_containers, _all_required,
|
||||
ProcessorsType>(&_r, ptr);
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
|
||||
namespace rfl::ubjson {
|
||||
|
||||
@@ -38,9 +39,8 @@ class Reader {
|
||||
~Reader() = default;
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
|
||||
T::from_ubjson_obj(var);
|
||||
});
|
||||
static constexpr bool has_custom_constructor =
|
||||
(requires(InputVarType var) { T::from_ubjson_obj(var); });
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_array(
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept;
|
||||
@@ -63,7 +63,7 @@ class Reader {
|
||||
return Error("Could not cast to bytestring.");
|
||||
}
|
||||
const auto vec = _var.val_->as<std::vector<uint8_t>>();
|
||||
return rfl::Bytestring(std::bit_cast<const std::byte*>(vec.data()),
|
||||
return rfl::Bytestring(internal::ptr_cast<const std::byte*>(vec.data()),
|
||||
vec.size());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!_var.val_->is_bool()) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#ifndef RFL_UBJSON_WRITE_HPP_
|
||||
#define RFL_UBJSON_WRITE_HPP_
|
||||
|
||||
#include <bit>
|
||||
#include <cstdint>
|
||||
#include <jsoncons_ext/ubjson/ubjson_encoder.hpp>
|
||||
#include <ostream>
|
||||
@@ -9,6 +8,7 @@
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
@@ -24,8 +24,9 @@ std::vector<char> write(const auto& _obj) noexcept {
|
||||
const auto writer = Writer(&encoder);
|
||||
Parser<T, Processors<Ps...>>::write(writer, _obj,
|
||||
typename ParentType::Root{});
|
||||
return std::vector<char>(std::bit_cast<char*>(buffer.data()),
|
||||
std::bit_cast<char*>(buffer.data() + buffer.size()));
|
||||
return std::vector<char>(
|
||||
internal::ptr_cast<char*>(buffer.data()),
|
||||
internal::ptr_cast<char*>(buffer.data() + buffer.size()));
|
||||
}
|
||||
|
||||
/// Writes a UBJSON into an ostream.
|
||||
|
||||
Reference in New Issue
Block a user