diff --git a/taglib/toolkit/tbytevector.cpp b/taglib/toolkit/tbytevector.cpp index a531733c..b223de43 100644 --- a/taglib/toolkit/tbytevector.cpp +++ b/taglib/toolkit/tbytevector.cpp @@ -139,15 +139,8 @@ T toNumber(const ByteVector &v, size_t offset, bool mostSignificantByteFirst) T tmp; ::memcpy(&tmp, v.data() + offset, sizeof(T)); - if(swap) { - static_assert(sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8, "Byte swap requested for type with invalid size"); - if constexpr (sizeof(T) == 2) - return Utils::byteSwap(static_cast(tmp)); - else if constexpr (sizeof(T) == 4) - return Utils::byteSwap(static_cast(tmp)); - else if constexpr (sizeof(T) == 8) - return Utils::byteSwap(static_cast(tmp)); - } + if(swap) + return Utils::byteSwap(tmp); return tmp; } @@ -156,15 +149,8 @@ ByteVector fromNumber(T value, bool mostSignificantByteFirst) { const bool isBigEndian = Utils::systemByteOrder() == Utils::BigEndian; - if(mostSignificantByteFirst != isBigEndian) { - static_assert(sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8, "Byte swap requested for type with invalid size"); - if constexpr (sizeof(T) == 2) - value = Utils::byteSwap(static_cast(value)); - else if constexpr (sizeof(T) == 4) - value = Utils::byteSwap(static_cast(value)); - else if constexpr (sizeof(T) == 8) - value = Utils::byteSwap(static_cast(value)); - } + if(mostSignificantByteFirst != isBigEndian) + value = Utils::byteSwap(value); return ByteVector(reinterpret_cast(&value), sizeof(T)); } @@ -183,15 +169,8 @@ TFloat toFloat(const ByteVector &v, size_t offset) } tmp; ::memcpy(&tmp, v.data() + offset, sizeof(TInt)); - if(ENDIAN != Utils::systemByteOrder()) { - static_assert(sizeof(TInt) == 2 || sizeof(TInt) == 4 || sizeof(TInt) == 8, "Byte swap requested for type with invalid size"); - if constexpr (sizeof(TInt) == 2) - tmp.i = Utils::byteSwap(static_cast(tmp.i)); - else if constexpr (sizeof(TInt) == 4) - tmp.i = Utils::byteSwap(static_cast(tmp.i)); - else if constexpr (sizeof(TInt) == 8) - tmp.i = Utils::byteSwap(static_cast(tmp.i)); - } + if(ENDIAN != Utils::systemByteOrder()) + tmp.i = Utils::byteSwap(tmp.i); return tmp.f; } @@ -205,15 +184,8 @@ ByteVector fromFloat(TFloat value) } tmp; tmp.f = value; - if(ENDIAN != Utils::systemByteOrder()) { - static_assert(sizeof(TInt) == 2 || sizeof(TInt) == 4 || sizeof(TInt) == 8, "Byte swap requested for type with invalid size"); - if constexpr (sizeof(TInt) == 2) - tmp.i = Utils::byteSwap(static_cast(tmp.i)); - else if constexpr (sizeof(TInt) == 4) - tmp.i = Utils::byteSwap(static_cast(tmp.i)); - else if constexpr (sizeof(TInt) == 8) - tmp.i = Utils::byteSwap(static_cast(tmp.i)); - } + if(ENDIAN != Utils::systemByteOrder()) + tmp.i = Utils::byteSwap(tmp.i); return ByteVector(reinterpret_cast(&tmp), sizeof(TInt)); } @@ -310,47 +282,47 @@ ByteVector ByteVector::fromCString(const char *s, unsigned int length) ByteVector ByteVector::fromUInt(unsigned int value, bool mostSignificantByteFirst) { - return fromNumber(value, mostSignificantByteFirst); + return fromNumber(value, mostSignificantByteFirst); } ByteVector ByteVector::fromShort(short value, bool mostSignificantByteFirst) { - return fromNumber(value, mostSignificantByteFirst); + return fromNumber(value, mostSignificantByteFirst); } ByteVector ByteVector::fromUShort(unsigned short value, bool mostSignificantByteFirst) { - return fromNumber(value, mostSignificantByteFirst); + return fromNumber(value, mostSignificantByteFirst); } ByteVector ByteVector::fromLongLong(long long value, bool mostSignificantByteFirst) { - return fromNumber(value, mostSignificantByteFirst); + return fromNumber(value, mostSignificantByteFirst); } ByteVector ByteVector::fromULongLong(unsigned long long value, bool mostSignificantByteFirst) { - return fromNumber(value, mostSignificantByteFirst); + return fromNumber(value, mostSignificantByteFirst); } ByteVector ByteVector::fromFloat32LE(float value) { - return fromFloat(value); + return fromFloat(value); } ByteVector ByteVector::fromFloat32BE(float value) { - return fromFloat(value); + return fromFloat(value); } ByteVector ByteVector::fromFloat64LE(double value) { - return fromFloat(value); + return fromFloat(value); } ByteVector ByteVector::fromFloat64BE(double value) { - return fromFloat(value); + return fromFloat(value); } //////////////////////////////////////////////////////////////////////////////// @@ -686,77 +658,77 @@ bool ByteVector::isEmpty() const unsigned int ByteVector::toUInt(bool mostSignificantByteFirst) const { - return toNumber(*this, 0, mostSignificantByteFirst); + return toNumber(*this, 0, mostSignificantByteFirst); } unsigned int ByteVector::toUInt(unsigned int offset, bool mostSignificantByteFirst) const { - return toNumber(*this, offset, mostSignificantByteFirst); + return toNumber(*this, offset, mostSignificantByteFirst); } unsigned int ByteVector::toUInt(unsigned int offset, unsigned int length, bool mostSignificantByteFirst) const { - return toNumber(*this, offset, length, mostSignificantByteFirst); + return toNumber(*this, offset, length, mostSignificantByteFirst); } short ByteVector::toShort(bool mostSignificantByteFirst) const { - return toNumber(*this, 0, mostSignificantByteFirst); + return toNumber(*this, 0, mostSignificantByteFirst); } short ByteVector::toShort(unsigned int offset, bool mostSignificantByteFirst) const { - return toNumber(*this, offset, mostSignificantByteFirst); + return toNumber(*this, offset, mostSignificantByteFirst); } unsigned short ByteVector::toUShort(bool mostSignificantByteFirst) const { - return toNumber(*this, 0, mostSignificantByteFirst); + return toNumber(*this, 0, mostSignificantByteFirst); } unsigned short ByteVector::toUShort(unsigned int offset, bool mostSignificantByteFirst) const { - return toNumber(*this, offset, mostSignificantByteFirst); + return toNumber(*this, offset, mostSignificantByteFirst); } long long ByteVector::toLongLong(bool mostSignificantByteFirst) const { - return toNumber(*this, 0, mostSignificantByteFirst); + return toNumber(*this, 0, mostSignificantByteFirst); } long long ByteVector::toLongLong(unsigned int offset, bool mostSignificantByteFirst) const { - return toNumber(*this, offset, mostSignificantByteFirst); + return toNumber(*this, offset, mostSignificantByteFirst); } unsigned long long ByteVector::toULongLong(bool mostSignificantByteFirst) const { - return toNumber(*this, 0, mostSignificantByteFirst); + return toNumber(*this, 0, mostSignificantByteFirst); } unsigned long long ByteVector::toULongLong(unsigned int offset, bool mostSignificantByteFirst) const { - return toNumber(*this, offset, mostSignificantByteFirst); + return toNumber(*this, offset, mostSignificantByteFirst); } float ByteVector::toFloat32LE(size_t offset) const { - return toFloat(*this, offset); + return toFloat(*this, offset); } float ByteVector::toFloat32BE(size_t offset) const { - return toFloat(*this, offset); + return toFloat(*this, offset); } double ByteVector::toFloat64LE(size_t offset) const { - return toFloat(*this, offset); + return toFloat(*this, offset); } double ByteVector::toFloat64BE(size_t offset) const { - return toFloat(*this, offset); + return toFloat(*this, offset); } long double ByteVector::toFloat80LE(size_t offset) const