Skip to content

Commit

Permalink
REFAC: Use template to support arbitrary integer types
Browse files Browse the repository at this point in the history
Instead of providing explicit overloads for some integer types, we now
use a template to cover all integer types. Using a manually selected
subset has repeatedly led to issues where integer definitions vary
between platforms (and library versions?).
  • Loading branch information
Krzmbrzl committed Jan 8, 2025
1 parent c0eaa32 commit 9138b73
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/PacketDataStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <QString>

#include <cstring>
#include <type_traits>

/*
* GCC doesn't yet do inter-object-file inlining, so unfortunately, this all has to be defined here.
Expand Down Expand Up @@ -280,22 +281,18 @@ class PacketDataStream {
return *this;
}

#define INTMAPOPERATOR(type) \
PacketDataStream &operator<<(const type v) { return *this << static_cast< quint64 >(v); } \
PacketDataStream &operator>>(type &v) { \
quint64 vv; \
*this >> vv; \
v = static_cast< type >(vv); \
return *this; \
template< typename Integer, typename = std::enable_if_t< std::is_integral_v< Integer > > >
PacketDataStream &operator<<(const Integer v) {
return *this << static_cast< quint64 >(v);
}

INTMAPOPERATOR(qsizetype);
INTMAPOPERATOR(int);
INTMAPOPERATOR(unsigned int);
INTMAPOPERATOR(short);
INTMAPOPERATOR(unsigned short);
INTMAPOPERATOR(char);
INTMAPOPERATOR(unsigned char);
template< typename Integer, typename = std::enable_if_t< std::is_integral_v< Integer > > >
PacketDataStream &operator>>(Integer &v) {
quint64 vv;
*this >> vv;
v = static_cast< Integer >(vv);
return *this;
}

union double64u {
quint64 ui;
Expand Down

0 comments on commit 9138b73

Please sign in to comment.