Skip to content

Commit

Permalink
Replacing reinpterpret_casts by std::bit_cast
Browse files Browse the repository at this point in the history
  • Loading branch information
flamewing committed Mar 22, 2024
1 parent e2fc774 commit 22f1205
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions include/mdcomp/basic_decoder.hh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public:
}

auto const size_bytes = count * sizeof(T);
return reinterpret_cast<T*>(
return std::bit_cast<T*>(
::operator new[](size_bytes, std::align_val_t{Align}));
}

Expand All @@ -91,7 +91,7 @@ bool basic_decoder<Format, Pad, Args...>::encode(
} else {
data.resize(full_size);
}
source.read(reinterpret_cast<char*>(data.data()), std::ssize(data));
source.read(std::bit_cast<char*>(data.data()), std::ssize(data));
if constexpr (Pad == pad_mode::pad_even) {
if (data.size() > full_size) {
data.back() = 0;
Expand Down
9 changes: 4 additions & 5 deletions include/mdcomp/lzss.hh
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,7 @@ auto find_optimal_lzss_parse(std::span<uint8_t const> data_in, Adaptor adaptor)
using data_t = std::span<stream_t const>;

auto read_stream = [](data_t data, size_t offset) {
auto const* pointer
= reinterpret_cast<uint8_t const*>(std::addressof(data[offset]));
auto const* pointer = std::bit_cast<uint8_t const*>(std::addressof(data[offset]));
return stream_endian_t::template read<stream_t>(pointer);
};

Expand Down Expand Up @@ -498,7 +497,7 @@ private:
using descriptor_t = typename Adaptor::descriptor_t;
using descriptor_endian_t = typename Adaptor::descriptor_endian_t;
using bit_buffer_t = obitstream<
descriptor_t, Adaptor::descriptor_bit_order, descriptor_endian_t>;
descriptor_t, Adaptor::descriptor_bit_order, descriptor_endian_t>;
// Where we will output to.
std::ostream& out;
// Internal bitstream output buffer.
Expand Down Expand Up @@ -573,8 +572,8 @@ private:
using descriptor_t = typename Adaptor::descriptor_t;
using descriptor_endian_t = typename Adaptor::descriptor_endian_t;
using bit_buffer_t = ibitstream<
descriptor_t, Adaptor::descriptor_bit_order, descriptor_endian_t,
Adaptor::need_early_descriptor>;
descriptor_t, Adaptor::descriptor_bit_order, descriptor_endian_t,
Adaptor::need_early_descriptor>;
// Where we will input to.
std::istream* in;
// Internal bitstream input buffer.
Expand Down
2 changes: 1 addition & 1 deletion include/mdcomp/moduled_adaptor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ bool moduled_adaptor<Format, DefaultModuleSize, DefaultModulePadding>::moduled_e
std::vector<uint8_t> data;
data.resize(static_cast<size_t>(full_size));
auto pointer = std::ranges::cbegin(data);
source.read(reinterpret_cast<char*>(data.data()), full_size);
source.read(std::bit_cast<char*>(data.data()), full_size);

big_endian::write2(
dest, static_cast<size_t>(full_size) & std::numeric_limits<uint16_t>::max());
Expand Down
8 changes: 4 additions & 4 deletions src/lib/enigma.cc
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ class enigma_internal {
}
};

bool enigma::decode(std::istream & source, std::ostream & dest) {
bool enigma::decode(std::istream& source, std::ostream& dest) {
auto const location = source.tellg();
std::stringstream input(std::ios::in | std::ios::out | std::ios::binary);
extract(source, input);
Expand All @@ -371,14 +371,14 @@ bool enigma::decode(std::istream & source, std::ostream & dest) {
return true;
}

bool enigma::encode(std::istream & source, std::ostream & dest) {
bool enigma::encode(std::istream& source, std::ostream& dest) {
enigma_internal::encode(source, dest);
return true;
}

bool enigma::encode(std::ostream & dest, std::span<uint8_t const> data) {
bool enigma::encode(std::ostream& dest, std::span<uint8_t const> data) {
std::stringstream source(std::ios::in | std::ios::out | std::ios::binary);
source.write(reinterpret_cast<char const*>(data.data()), std::ssize(data));
source.write(std::bit_cast<char const*>(data.data()), std::ssize(data));
source.seekg(0);
return encode(source, dest);
}
2 changes: 1 addition & 1 deletion src/lib/nemesis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ bool nemesis::encode(std::istream& source, std::ostream& dest) {

bool nemesis::encode(std::ostream& dest, std::span<uint8_t const> data) {
std::stringstream source(std::ios::in | std::ios::out | std::ios::binary);
source.write(reinterpret_cast<char const*>(data.data()), std::ssize(data));
source.write(std::bit_cast<char const*>(data.data()), std::ssize(data));
source.seekg(0);
return encode(source, dest);
}
2 changes: 1 addition & 1 deletion src/lib/snkrle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ bool snkrle::decode(std::istream& source, std::ostream& dest) {

bool snkrle::encode(std::ostream& dest, std::span<uint8_t const> data) {
std::stringstream source(std::ios::in | std::ios::out | std::ios::binary);
source.write(reinterpret_cast<char const*>(data.data()), std::ssize(data));
source.write(std::bit_cast<char const*>(data.data()), std::ssize(data));
source.seekg(0);
snkrle_internal::encode(source, dest);
return true;
Expand Down

0 comments on commit 22f1205

Please sign in to comment.