Skip to content

Commit

Permalink
Improving endian reader interface
Browse files Browse the repository at this point in the history
  • Loading branch information
flamewing committed Dec 2, 2021
1 parent 8bdf591 commit dcfa784
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions include/mdcomp/bigendian_io.hh
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,34 @@ namespace detail {
Base::Read(in, val);
}

template <typename Src, typename T>
static inline auto Read(Src& in, T& val) noexcept
-> std::enable_if_t<std::is_signed<T>::value, void> {
using uint_t = std::make_unsigned_t<T>;
uint_t uval;
Base::Read(in, uval);
std::memcpy(&val, &uval, sizeof(T));
}

template <typename T, typename Src>
static inline auto Read(Src& in) noexcept
-> std::enable_if_t<std::is_unsigned<T>::value, T> {
T val;
Base::Read(in, val);
return val;
}

template <typename T, typename Src>
static inline auto Read(Src& in) noexcept
-> std::enable_if_t<std::is_signed<T>::value, T> {
using uint_t = std::make_unsigned_t<T>;
uint_t uval;
Base::Read(in, uval);
T val;
std::memcpy(&val, &uval, sizeof(T));
return val;
}

template <typename Dst>
static inline void Write1(Dst& out, uint8_t val) noexcept {
Base::Write(out, val);
Expand Down

0 comments on commit dcfa784

Please sign in to comment.