diff options
author | João Abecasis <joao.abecasis@nokia.com> | 2012-03-21 18:09:09 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-03-25 13:12:24 +0200 |
commit | fe778b94bd58c12949d763f428d214e8c16d144e (patch) | |
tree | 878c41fcc5f18a33d57c34a27cc69f0eb940bb7e /src/corelib/global/qendian.h | |
parent | 552e162a67a1afcdc4e3e344100abaa6a69ab918 (diff) |
Enable endianness conversions on q(u)int8
Lack of support for these types is not a real issue as endian
conversions on byte-sized types are no-ops. Still, the conversions are
useful as they facilitate writing of generic code. They can also be used
explicitly as a way to document in code an endian-specific binary
format:
uchar *data;
quint8 tag = qFromLittleEndian<quint8>(data++);
quint32 size = qFromLittleEndian<quint32>(data);
This commit also adds a test for functions documented in the QtEndian
header.
Change-Id: I2f6c876ce89d2adb8c03a1c8a25921d225bf6f92
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/global/qendian.h')
-rw-r--r-- | src/corelib/global/qendian.h | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/corelib/global/qendian.h b/src/corelib/global/qendian.h index 8ecff5e165..e049fb6549 100644 --- a/src/corelib/global/qendian.h +++ b/src/corelib/global/qendian.h @@ -171,6 +171,11 @@ template <> inline qint16 qFromLittleEndian<qint16>(const uchar *src) { return static_cast<qint16>(qFromLittleEndian<quint16>(src)); } #endif +template <> inline quint8 qFromLittleEndian<quint8>(const uchar *src) +{ return static_cast<quint8>(src[0]); } +template <> inline qint8 qFromLittleEndian<qint8>(const uchar *src) +{ return static_cast<qint8>(src[0]); } + /* This function will read a big-endian (also known as network order) encoded value from \a src * and return the value in host-endian encoding. * There is no requirement that \a src must be aligned. @@ -263,6 +268,12 @@ template <> inline qint32 qFromBigEndian<qint32>(const uchar *src) template <> inline qint16 qFromBigEndian<qint16>(const uchar *src) { return static_cast<qint16>(qFromBigEndian<quint16>(src)); } #endif + +template <> inline quint8 qFromBigEndian<quint8>(const uchar *src) +{ return static_cast<quint8>(src[0]); } +template <> inline qint8 qFromBigEndian<qint8>(const uchar *src) +{ return static_cast<qint8>(src[0]); } + /* * T qbswap(T source). * Changes the byte order of a value from big endian to little endian or vice versa. @@ -367,6 +378,11 @@ template <> inline quint8 qbswap<quint8>(quint8 source) return source; } +template <> inline qint8 qbswap<qint8>(qint8 source) +{ + return source; +} + QT_END_NAMESPACE QT_END_HEADER |