mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 16:56:17 +08:00
Remove most instances of reinterpret_cast with std::bit_cast (#242)
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <bson/bson.h>
|
||||
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <concepts>
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
@@ -84,7 +85,7 @@ struct Reader {
|
||||
"bytestring.");
|
||||
}
|
||||
return rfl::Bytestring(
|
||||
reinterpret_cast<const std::byte*>(value.v_binary.data),
|
||||
std::bit_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) {
|
||||
@@ -121,8 +122,7 @@ struct Reader {
|
||||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept;
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <bson/bson.h>
|
||||
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
@@ -82,12 +83,12 @@ class Writer {
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
OutputArrayType add_array_to_object(const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept;
|
||||
OutputObjectType add_object_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
@@ -103,7 +104,7 @@ class Writer {
|
||||
rfl::Bytestring>()) {
|
||||
bson_array_builder_append_binary(
|
||||
_parent->val_, BSON_SUBTYPE_BINARY,
|
||||
reinterpret_cast<const uint8_t*>(_var.c_str()),
|
||||
std::bit_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);
|
||||
@@ -133,7 +134,7 @@ class Writer {
|
||||
rfl::Bytestring>()) {
|
||||
bson_append_binary(_parent->val_, _name.data(),
|
||||
static_cast<int>(_name.size()), BSON_SUBTYPE_BINARY,
|
||||
reinterpret_cast<const uint8_t*>(_var.c_str()),
|
||||
std::bit_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,6 +3,7 @@
|
||||
|
||||
#include <bson/bson.h>
|
||||
|
||||
#include <bit>
|
||||
#include <istream>
|
||||
#include <string>
|
||||
|
||||
@@ -41,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...>(reinterpret_cast<const uint8_t*>(_bytes), _size);
|
||||
return read<T, Ps...>(std::bit_cast<const uint8_t*>(_bytes), _size);
|
||||
}
|
||||
|
||||
/// Parses an object from BSON using reflection.
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <bson/bson.h>
|
||||
|
||||
#include <bit>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
@@ -44,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>(reinterpret_cast<char*>(buf),
|
||||
reinterpret_cast<char*>(buf) + len);
|
||||
const auto result = std::vector<char>(std::bit_cast<char*>(buf),
|
||||
std::bit_cast<char*>(buf) + len);
|
||||
bson_free(buf);
|
||||
return result;
|
||||
}
|
||||
@@ -54,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(reinterpret_cast<const char*>(buf), len);
|
||||
_stream.write(std::bit_cast<const char*>(buf), len);
|
||||
bson_free(buf);
|
||||
return _stream;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <cbor.h>
|
||||
|
||||
#include <bit>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
@@ -58,8 +59,8 @@ class Writer {
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputArrayType add_array_to_object(const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_array(const size_t _size,
|
||||
@@ -106,7 +107,7 @@ class Writer {
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
cbor_encode_byte_string(
|
||||
_parent, reinterpret_cast<const uint8_t*>(_var.c_str()), _var.size());
|
||||
_parent, std::bit_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,6 +3,7 @@
|
||||
|
||||
#include <cbor.h>
|
||||
|
||||
#include <bit>
|
||||
#include <istream>
|
||||
#include <string>
|
||||
|
||||
@@ -30,7 +31,7 @@ Result<internal::wrap_in_rfl_array_t<T>> read(const char* _bytes,
|
||||
const size_t _size) {
|
||||
CborParser parser;
|
||||
InputVarType doc;
|
||||
cbor_parser_init(reinterpret_cast<const uint8_t*>(_bytes), _size, 0, &parser,
|
||||
cbor_parser_init(std::bit_cast<const uint8_t*>(_bytes), _size, 0, &parser,
|
||||
&doc.val_);
|
||||
auto result = read<T, Ps...>(doc);
|
||||
return result;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <cbor.h>
|
||||
|
||||
#include <bit>
|
||||
#include <cstdint>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
@@ -20,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, reinterpret_cast<uint8_t*>(_buffer->data()),
|
||||
cbor_encoder_init(_encoder, std::bit_cast<uint8_t*>(_buffer->data()),
|
||||
_buffer->size(), 0);
|
||||
const auto writer = Writer(_encoder);
|
||||
Parser<T, Processors<Ps...>>::write(writer, _obj,
|
||||
@@ -40,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, reinterpret_cast<uint8_t*>(buffer.data()));
|
||||
&encoder, std::bit_cast<uint8_t*>(buffer.data()));
|
||||
buffer.resize(length);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
@@ -64,7 +65,7 @@ struct Reader {
|
||||
return rfl::Error("Could not cast to a bytestring.");
|
||||
}
|
||||
const auto blob = _var.AsBlob();
|
||||
return rfl::Bytestring(reinterpret_cast<const std::byte*>(blob.data()),
|
||||
return rfl::Bytestring(std::bit_cast<const std::byte*>(blob.data()),
|
||||
blob.size());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!_var.IsBool()) {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <bit>
|
||||
#include <istream>
|
||||
#include <vector>
|
||||
|
||||
@@ -26,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(reinterpret_cast<const uint8_t*>(_bytes), _size);
|
||||
flexbuffers::GetRoot(std::bit_cast<const uint8_t*>(_bytes), _size);
|
||||
return read<T, Ps...>(root);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
@@ -31,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 = reinterpret_cast<const char*>(buffer.data());
|
||||
const auto data = std::bit_cast<const char*>(buffer.data());
|
||||
return std::vector<char>(data, data + buffer.size());
|
||||
}
|
||||
|
||||
@@ -39,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 = reinterpret_cast<const char*>(buffer.data());
|
||||
const auto data = std::bit_cast<const char*>(buffer.data());
|
||||
_stream.write(data, buffer.size());
|
||||
return _stream;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <msgpack.h>
|
||||
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <string>
|
||||
@@ -30,7 +31,7 @@ struct Reader {
|
||||
const size_t _idx, const InputArrayType _arr) const noexcept;
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept ;
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept;
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept;
|
||||
|
||||
@@ -49,7 +50,7 @@ struct Reader {
|
||||
return Error("Could not cast to a bytestring.");
|
||||
}
|
||||
const auto bin = _var.via.bin;
|
||||
return rfl::Bytestring(reinterpret_cast<const std::byte*>(bin.ptr),
|
||||
return rfl::Bytestring(std::bit_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) {
|
||||
@@ -74,8 +75,7 @@ struct Reader {
|
||||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept;
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept;
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define RFL_PARSING_NAMEDTUPLEPARSER_HPP_
|
||||
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <map>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
@@ -84,7 +85,7 @@ struct NamedTupleParser {
|
||||
internal::no_duplicate_field_names<typename NamedTupleType::Fields>());
|
||||
alignas(NamedTuple<FieldTypes...>) unsigned char
|
||||
buf[sizeof(NamedTuple<FieldTypes...>)];
|
||||
auto ptr = std::launder(reinterpret_cast<NamedTuple<FieldTypes...>*>(buf));
|
||||
auto ptr = std::bit_cast<NamedTuple<FieldTypes...>*>(&buf);
|
||||
auto view = rfl::to_view(*ptr);
|
||||
using ViewType = std::remove_cvref_t<decltype(view)>;
|
||||
const auto [set, err] =
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define RFL_PARSING_PARSER_ARRAY_HPP_
|
||||
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
@@ -32,7 +33,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::launder(reinterpret_cast<std::array<T, _size>*>(buf));
|
||||
auto ptr = std::bit_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,6 +1,7 @@
|
||||
#ifndef RFL_PARSING_PARSER_DEFAULT_HPP_
|
||||
#define RFL_PARSING_PARSER_DEFAULT_HPP_
|
||||
|
||||
#include <bit>
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
@@ -254,8 +255,8 @@ struct Parser {
|
||||
/// views and placement new. This is how we deal with the fact that some
|
||||
/// 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::launder(reinterpret_cast<T*>(buf));
|
||||
alignas(T) unsigned char buf[sizeof(T)]{};
|
||||
auto ptr = std::bit_cast<T*>(&buf);
|
||||
auto view = ProcessorsType::template process<T>(to_view(*ptr));
|
||||
using ViewType = std::remove_cvref_t<decltype(view)>;
|
||||
const auto [set, err] =
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef RFL_PARSING_TUPLEPARSER_HPP_
|
||||
#define RFL_PARSING_TUPLEPARSER_HPP_
|
||||
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <map>
|
||||
#include <type_traits>
|
||||
@@ -31,7 +32,7 @@ struct TupleParser {
|
||||
const InputVarType& _var) noexcept {
|
||||
const auto parse = [&](const InputArrayType& _arr) -> Result<TupleType> {
|
||||
alignas(TupleType) unsigned char buf[sizeof(TupleType)];
|
||||
auto ptr = std::launder(reinterpret_cast<TupleType*>(buf));
|
||||
auto ptr = std::bit_cast<TupleType*>(&buf);
|
||||
const auto tuple_reader =
|
||||
TupleReader<R, W, TupleType, _ignore_empty_containers, _all_required,
|
||||
ProcessorsType>(&_r, ptr);
|
||||
|
||||
Reference in New Issue
Block a user