Skip to content

Commit

Permalink
fix(types): improve comp. with old compilers (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrogTheFrog authored Feb 6, 2025
1 parent c04eee4 commit 591387c
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/common/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,24 @@ namespace {
return false;
}

std::byte operator+(const std::byte &lhs, const std::byte &rhs) {
std::byte operator+(const std::byte lhs, const std::byte &rhs) {
return std::byte {static_cast<std::uint8_t>(static_cast<int>(lhs) + static_cast<int>(rhs))};
}

// This madness should be removed once the minimum compiler version increases...
std::byte logicalAnd(const std::byte &lhs, const std::byte &rhs) {
return std::byte {static_cast<std::uint8_t>(static_cast<int>(lhs) & static_cast<int>(rhs))};
}

// This madness should be removed once the minimum compiler version increases...
std::byte shiftLeft(const std::byte &lhs, const int rhs) {
return std::byte {static_cast<std::uint8_t>(static_cast<int>(lhs) << rhs)};
}

// This madness should be removed once the minimum compiler version increases...
std::byte shiftRight(const std::byte &lhs, const int rhs) {
return std::byte {static_cast<std::uint8_t>(static_cast<int>(lhs) >> rhs)};
}
} // namespace

namespace display_device {
Expand Down Expand Up @@ -86,12 +96,12 @@ namespace display_device {
{
constexpr std::byte ascii_offset {'@'};

auto byte_a {data[8]};
auto byte_b {data[9]};
const auto byte_a {data[8]};
const auto byte_b {data[9]};
std::array<char, 3> man_id {};

man_id[0] = static_cast<char>(ascii_offset + (logicalAnd(byte_a, std::byte {0x7C}) >> 2));
man_id[1] = static_cast<char>(ascii_offset + (logicalAnd(byte_a, std::byte {0x03}) << 3) + (logicalAnd(byte_b, std::byte {0xE0}) >> 5));
man_id[0] = static_cast<char>(ascii_offset + shiftRight(logicalAnd(byte_a, std::byte {0x7C}), 2));
man_id[1] = static_cast<char>(ascii_offset + shiftLeft(logicalAnd(byte_a, std::byte {0x03}), 3) + shiftRight(logicalAnd(byte_b, std::byte {0xE0}), 5));
man_id[2] = static_cast<char>(ascii_offset + logicalAnd(byte_b, std::byte {0x1F}));

for (const char ch : man_id) {
Expand Down

0 comments on commit 591387c

Please sign in to comment.