Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use static_cast for the pointer transformations #298

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions include/rfl/Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions include/rfl/Tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down
18 changes: 10 additions & 8 deletions include/rfl/Variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,34 @@ 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));
}

template <class T,
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);
}

template <class T,
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));
}

Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions include/rfl/bson/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <bson/bson.h>

#include <array>
#include <bit>
#include <concepts>
#include <cstddef>
#include <exception>
Expand All @@ -23,6 +22,7 @@
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
#include "../internal/ptr_cast.hpp"

namespace rfl {
namespace bson {
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions include/rfl/bson/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <bson/bson.h>

#include <bit>
#include <cstddef>
#include <exception>
#include <map>
Expand All @@ -20,6 +19,7 @@
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
#include "../internal/ptr_cast.hpp"

namespace rfl {
namespace bson {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions include/rfl/bson/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions include/rfl/bson/write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions include/rfl/cbor/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <cbor.h>

#include <bit>
#include <exception>
#include <map>
#include <sstream>
Expand All @@ -19,6 +18,7 @@
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
#include "../internal/ptr_cast.hpp"

namespace rfl {
namespace cbor {
Expand Down Expand Up @@ -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>>()) {
Expand Down
6 changes: 3 additions & 3 deletions include/rfl/cbor/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions include/rfl/cbor/write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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,
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions include/rfl/flexbuf/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <flatbuffers/flexbuffers.h>

#include <bit>
#include <cstddef>
#include <exception>
#include <map>
Expand All @@ -17,6 +16,7 @@
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
#include "../internal/ptr_cast.hpp"

namespace rfl {
namespace flexbuf {
Expand Down Expand Up @@ -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()) {
Expand Down
Loading
Loading