Skip to content

Commit 2a4d1a1

Browse files
Use static_cast for the pointer transformations
1 parent 5934aea commit 2a4d1a1

21 files changed

+97
-73
lines changed

include/rfl/Result.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <type_traits>
1111

1212
#include "internal/is_array.hpp"
13+
#include "internal/ptr_cast.hpp"
1314
#include "internal/to_std_array.hpp"
1415

1516
namespace rfl {
@@ -314,20 +315,18 @@ class Result {
314315
}
315316
}
316317

317-
T& get_t() noexcept {
318-
return *std::launder((reinterpret_cast<T*>(t_or_err_.data())));
319-
}
318+
T& get_t() noexcept { return *internal::ptr_cast<T*>(t_or_err_.data()); }
320319

321320
const T& get_t() const noexcept {
322-
return *std::launder((reinterpret_cast<const T*>(t_or_err_.data())));
321+
return *internal::ptr_cast<const T*>(t_or_err_.data());
323322
}
324323

325324
Error& get_err() noexcept {
326-
return *std::launder((reinterpret_cast<Error*>(t_or_err_.data())));
325+
return *internal::ptr_cast<Error*>(t_or_err_.data());
327326
}
328327

329328
const Error& get_err() const noexcept {
330-
return *std::launder((reinterpret_cast<const Error*>(t_or_err_.data())));
329+
return *internal::ptr_cast<const Error*>(t_or_err_.data());
331330
}
332331

333332
void move_from_other(Result<T>& _other) noexcept {

include/rfl/Tuple.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <utility>
1515

1616
#include "internal/nth_element_t.hpp"
17+
#include "internal/ptr_cast.hpp"
1718
#include "internal/tuple/calculate_positions.hpp"
1819

1920
namespace rfl {
@@ -59,14 +60,14 @@ class Tuple {
5960
template <int _index>
6061
constexpr auto& get() {
6162
using Type = internal::nth_element_t<_index, Types...>;
62-
return *std::bit_cast<Type*>(data_.data() + pos<_index>());
63+
return *internal::ptr_cast<Type*>(data_.data() + pos<_index>());
6364
}
6465

6566
/// Gets an element by index.
6667
template <int _index>
6768
constexpr const auto& get() const {
6869
using Type = internal::nth_element_t<_index, Types...>;
69-
return *std::bit_cast<const Type*>(data_.data() + pos<_index>());
70+
return *internal::ptr_cast<const Type*>(data_.data() + pos<_index>());
7071
}
7172

7273
/// Assigns the underlying object.
@@ -100,8 +101,7 @@ class Tuple {
100101
};
101102
return [&]<int... _is>(std::integer_sequence<int, _is...>) {
102103
return (true && ... && is_same(std::integral_constant<int, _is>{}));
103-
}
104-
(std::make_integer_sequence<int, sizeof...(Types)>());
104+
}(std::make_integer_sequence<int, sizeof...(Types)>());
105105
}
106106

107107
/// Three-way comparison operator.
@@ -122,8 +122,7 @@ class Tuple {
122122
auto ordering = std::strong_ordering::equivalent;
123123
(compare(&ordering, std::integral_constant<int, _is>{}), ...);
124124
return ordering;
125-
}
126-
(std::make_integer_sequence<int, sizeof...(Types)>());
125+
}(std::make_integer_sequence<int, sizeof...(Types)>());
127126
}
128127

129128
private:

include/rfl/Variant.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,34 @@ class Variant {
4343
struct TypeWrapper {};
4444

4545
public:
46-
Variant() {
46+
Variant() : index_(IndexType()), data_(DataType()) {
4747
using FirstAlternative = internal::nth_element_t<0, AlternativeTypes...>;
4848
move_from_type(FirstAlternative());
4949
}
5050

51-
Variant(const Variant<AlternativeTypes...>& _other) {
51+
Variant(const Variant<AlternativeTypes...>& _other)
52+
: index_(IndexType()), data_(DataType()) {
5253
copy_from_other(_other);
5354
}
5455

55-
Variant(Variant<AlternativeTypes...>&& _other) noexcept {
56+
Variant(Variant<AlternativeTypes...>&& _other) noexcept
57+
: index_(IndexType()), data_(DataType()) {
5658
move_from_other(std::move(_other));
5759
}
5860

5961
template <class T,
6062
typename std::enable_if<internal::variant::is_alternative_type<
6163
T, AlternativeTypes...>(),
6264
bool>::type = true>
63-
Variant(const T& _t) {
65+
Variant(const T& _t) : index_(IndexType()), data_(DataType()) {
6466
copy_from_type(_t);
6567
}
6668

6769
template <class T,
6870
typename std::enable_if<internal::variant::is_alternative_type<
6971
T, AlternativeTypes...>(),
7072
bool>::type = true>
71-
Variant(T&& _t) noexcept {
73+
Variant(T&& _t) noexcept : index_(IndexType()), data_(DataType()) {
7274
move_from_type(std::forward<T>(_t));
7375
}
7476

@@ -80,7 +82,7 @@ class Variant {
8082
auto t = T{std::forward<Args>(_args)...};
8183
destroy_if_necessary();
8284
move_from_type(std::move(t));
83-
return *std::launder(reinterpret_cast<T*>(data_.data()));
85+
return *internal::ptr_cast<T*>(data_.data());
8486
}
8587

8688
/// Emplaces a new element into the variant.
@@ -384,13 +386,13 @@ class Variant {
384386
template <IndexType _i>
385387
auto& get_alternative() noexcept {
386388
using CurrentType = internal::nth_element_t<_i, AlternativeTypes...>;
387-
return *std::launder(reinterpret_cast<CurrentType*>(data_.data()));
389+
return *internal::ptr_cast<CurrentType*>(data_.data());
388390
}
389391

390392
template <IndexType _i>
391393
const auto& get_alternative() const noexcept {
392394
using CurrentType = internal::nth_element_t<_i, AlternativeTypes...>;
393-
return *std::launder(reinterpret_cast<const CurrentType*>(data_.data()));
395+
return *internal::ptr_cast<const CurrentType*>(data_.data());
394396
}
395397

396398
void move_from_other(Variant<AlternativeTypes...>&& _other) noexcept {

include/rfl/bson/Reader.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <bson/bson.h>
55

66
#include <array>
7-
#include <bit>
87
#include <concepts>
98
#include <cstddef>
109
#include <exception>
@@ -23,6 +22,7 @@
2322
#include "../Bytestring.hpp"
2423
#include "../Result.hpp"
2524
#include "../always_false.hpp"
25+
#include "../internal/ptr_cast.hpp"
2626

2727
namespace rfl {
2828
namespace bson {
@@ -46,9 +46,8 @@ struct Reader {
4646
using InputVarType = BSONInputVar;
4747

4848
template <class T>
49-
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
50-
T::from_bson_obj(var);
51-
});
49+
static constexpr bool has_custom_constructor =
50+
(requires(InputVarType var) { T::from_bson_obj(var); });
5251

5352
rfl::Result<InputVarType> get_field_from_array(
5453
const size_t _idx, const InputArrayType& _arr) const noexcept;
@@ -85,7 +84,7 @@ struct Reader {
8584
"bytestring.");
8685
}
8786
return rfl::Bytestring(
88-
std::bit_cast<const std::byte*>(value.v_binary.data),
87+
internal::ptr_cast<const std::byte*>(value.v_binary.data),
8988
value.v_binary.data_len);
9089
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
9190
if (btype != BSON_TYPE_BOOL) {

include/rfl/bson/Writer.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include <bson/bson.h>
55

6-
#include <bit>
76
#include <cstddef>
87
#include <exception>
98
#include <map>
@@ -20,6 +19,7 @@
2019
#include "../Ref.hpp"
2120
#include "../Result.hpp"
2221
#include "../always_false.hpp"
22+
#include "../internal/ptr_cast.hpp"
2323

2424
namespace rfl {
2525
namespace bson {
@@ -104,7 +104,7 @@ class Writer {
104104
rfl::Bytestring>()) {
105105
bson_array_builder_append_binary(
106106
_parent->val_, BSON_SUBTYPE_BINARY,
107-
std::bit_cast<const uint8_t*>(_var.c_str()),
107+
internal::ptr_cast<const uint8_t*>(_var.c_str()),
108108
static_cast<uint32_t>(_var.size()));
109109
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
110110
bson_array_builder_append_bool(_parent->val_, _var);
@@ -134,7 +134,7 @@ class Writer {
134134
rfl::Bytestring>()) {
135135
bson_append_binary(_parent->val_, _name.data(),
136136
static_cast<int>(_name.size()), BSON_SUBTYPE_BINARY,
137-
std::bit_cast<const uint8_t*>(_var.c_str()),
137+
internal::ptr_cast<const uint8_t*>(_var.c_str()),
138138
static_cast<uint32_t>(_var.size()));
139139
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
140140
bson_append_bool(_parent->val_, _name.data(),

include/rfl/bson/read.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
#include <bson/bson.h>
55

6-
#include <bit>
76
#include <istream>
87
#include <string>
98

109
#include "../Processors.hpp"
10+
#include "../internal/ptr_cast.hpp"
1111
#include "../internal/wrap_in_rfl_array_t.hpp"
1212
#include "Parser.hpp"
1313
#include "Reader.hpp"
@@ -42,7 +42,7 @@ auto read(const uint8_t* _bytes, const size_t _size) {
4242
/// Parses an BSON object using reflection.
4343
template <class T, class... Ps>
4444
auto read(const char* _bytes, const size_t _size) {
45-
return read<T, Ps...>(std::bit_cast<const uint8_t*>(_bytes), _size);
45+
return read<T, Ps...>(internal::ptr_cast<const uint8_t*>(_bytes), _size);
4646
}
4747

4848
/// Parses an object from BSON using reflection.

include/rfl/bson/write.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
#include <bson/bson.h>
55

6-
#include <bit>
76
#include <ostream>
87
#include <sstream>
98
#include <string>
109
#include <utility>
1110

1211
#include "../Processors.hpp"
12+
#include "../internal/ptr_cast.hpp"
1313
#include "../parsing/Parent.hpp"
1414
#include "Parser.hpp"
1515

@@ -45,8 +45,8 @@ std::pair<uint8_t*, size_t> to_buffer(const auto& _obj) noexcept {
4545
template <class... Ps>
4646
std::vector<char> write(const auto& _obj) noexcept {
4747
auto [buf, len] = to_buffer<Ps...>(_obj);
48-
const auto result = std::vector<char>(std::bit_cast<char*>(buf),
49-
std::bit_cast<char*>(buf) + len);
48+
const auto result = std::vector<char>(internal::ptr_cast<char*>(buf),
49+
internal::ptr_cast<char*>(buf) + len);
5050
bson_free(buf);
5151
return result;
5252
}
@@ -55,7 +55,7 @@ std::vector<char> write(const auto& _obj) noexcept {
5555
template <class... Ps>
5656
std::ostream& write(const auto& _obj, std::ostream& _stream) noexcept {
5757
auto [buf, len] = to_buffer<Ps...>(_obj);
58-
_stream.write(std::bit_cast<const char*>(buf), len);
58+
_stream.write(internal::ptr_cast<const char*>(buf), len);
5959
bson_free(buf);
6060
return _stream;
6161
}

include/rfl/cbor/Writer.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include <cbor.h>
55

6-
#include <bit>
76
#include <exception>
87
#include <map>
98
#include <sstream>
@@ -19,6 +18,7 @@
1918
#include "../Ref.hpp"
2019
#include "../Result.hpp"
2120
#include "../always_false.hpp"
21+
#include "../internal/ptr_cast.hpp"
2222

2323
namespace rfl {
2424
namespace cbor {
@@ -106,8 +106,9 @@ class Writer {
106106
cbor_encode_text_string(_parent, _var.c_str(), _var.size());
107107
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
108108
rfl::Bytestring>()) {
109-
cbor_encode_byte_string(
110-
_parent, std::bit_cast<const uint8_t*>(_var.c_str()), _var.size());
109+
cbor_encode_byte_string(_parent,
110+
internal::ptr_cast<const uint8_t*>(_var.c_str()),
111+
_var.size());
111112
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
112113
cbor_encode_boolean(_parent, _var);
113114
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {

include/rfl/cbor/read.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
#include <cbor.h>
55

6-
#include <bit>
76
#include <istream>
87
#include <string>
98

109
#include "../Processors.hpp"
10+
#include "../internal/ptr_cast.hpp"
1111
#include "../internal/wrap_in_rfl_array_t.hpp"
1212
#include "Parser.hpp"
1313
#include "Reader.hpp"
@@ -31,8 +31,8 @@ Result<internal::wrap_in_rfl_array_t<T>> read(const char* _bytes,
3131
const size_t _size) {
3232
CborParser parser;
3333
InputVarType doc;
34-
cbor_parser_init(std::bit_cast<const uint8_t*>(_bytes), _size, 0, &parser,
35-
&doc.val_);
34+
cbor_parser_init(internal::ptr_cast<const uint8_t*>(_bytes), _size, 0,
35+
&parser, &doc.val_);
3636
auto result = read<T, Ps...>(doc);
3737
return result;
3838
}

include/rfl/cbor/write.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
#include <cbor.h>
55

6-
#include <bit>
76
#include <cstdint>
87
#include <ostream>
98
#include <sstream>
109
#include <string>
1110
#include <utility>
1211

12+
#include "../internal/ptr_cast.hpp"
1313
#include "../parsing/Parent.hpp"
1414
#include "Parser.hpp"
1515

@@ -21,7 +21,7 @@ void write_into_buffer(const auto& _obj, CborEncoder* _encoder,
2121
std::vector<char>* _buffer) noexcept {
2222
using T = std::remove_cvref_t<decltype(_obj)>;
2323
using ParentType = parsing::Parent<Writer>;
24-
cbor_encoder_init(_encoder, std::bit_cast<uint8_t*>(_buffer->data()),
24+
cbor_encoder_init(_encoder, internal::ptr_cast<uint8_t*>(_buffer->data()),
2525
_buffer->size(), 0);
2626
const auto writer = Writer(_encoder);
2727
Parser<T, Processors<Ps...>>::write(writer, _obj,
@@ -41,7 +41,7 @@ std::vector<char> write(const auto& _obj) noexcept {
4141
write_into_buffer<Ps...>(_obj, &encoder, &buffer);
4242
}
4343
const auto length = cbor_encoder_get_buffer_size(
44-
&encoder, std::bit_cast<uint8_t*>(buffer.data()));
44+
&encoder, internal::ptr_cast<uint8_t*>(buffer.data()));
4545
buffer.resize(length);
4646
return buffer;
4747
}

0 commit comments

Comments
 (0)