Add new type and templatize the "fromNumber" conversion.

git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@306704 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
This commit is contained in:
Scott Wheeler
2004-04-27 01:29:22 +00:00
parent cf6506668d
commit de4bd42ef0
2 changed files with 29 additions and 11 deletions

View File

@ -197,6 +197,19 @@ namespace TagLib {
return sum;
}
template <class T>
ByteVector fromNumber(T value, bool mostSignificantByteFirst)
{
int size = sizeof(T);
ByteVector v(size, 0);
for(int i = 0; i < size; i++)
v[i] = uchar(value >> ((mostSignificantByteFirst ? size - 1 - i : i) * 8) & 0xff);
return v;
}
}
using namespace TagLib;
@ -235,22 +248,17 @@ ByteVector ByteVector::fromCString(const char *s, uint length)
ByteVector ByteVector::fromUInt(uint value, bool mostSignificantByteFirst)
{
ByteVector v(4, 0);
return fromNumber<uint>(value, mostSignificantByteFirst);
}
for(int i = 0; i < 4; i++)
v[i] = uchar(value >> ((mostSignificantByteFirst ? 3 - i : i) * 8) & 0xff);
return v;
ByteVector ByteVector::fromShort(short value, bool mostSignificantByteFirst)
{
return fromNumber<short>(value, mostSignificantByteFirst);
}
ByteVector ByteVector::fromLongLong(long long value, bool mostSignificantByteFirst)
{
ByteVector v(8, 0);
for(int i = 0; i < 8; i++)
v[i] = uchar(value >> ((mostSignificantByteFirst ? 7 - i : i) * 8) & 0xff);
return v;
return fromNumber<long long>(value, mostSignificantByteFirst);
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -277,6 +277,16 @@ namespace TagLib {
*/
static ByteVector fromUInt(uint value, bool mostSignificantByteFirst = true);
/*!
* Creates a 2 byte ByteVector based on \a value. If
* \a mostSignificantByteFirst is true, then this will operate left to right
* in building the ByteVector. For example if \a mostSignificantByteFirst is
* true then $00 01 == 0x0001 == 1, if false, $01 00 == 0x0100 == 1.
*
* \see toShort()
*/
static ByteVector fromShort(short value, bool mostSignificantByteFirst = true);
/*!
* Creates a 8 byte ByteVector based on \a value. If
* \a mostSignificantByteFirst is true, then this will operate left to right