From: Scott Wheeler Date: Tue, 27 Apr 2004 01:29:22 +0000 (+0000) Subject: Add new type and templatize the "fromNumber" conversion. X-Git-Tag: v1.5~399 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=de4bd42ef06fa7283a23b2c2db16e5a105d99295;p=taglib 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 --- diff --git a/toolkit/tbytevector.cpp b/toolkit/tbytevector.cpp index 3ef1b6bc..78c1ac23 100644 --- a/toolkit/tbytevector.cpp +++ b/toolkit/tbytevector.cpp @@ -197,6 +197,19 @@ namespace TagLib { return sum; } + + template + 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); - - for(int i = 0; i < 4; i++) - v[i] = uchar(value >> ((mostSignificantByteFirst ? 3 - i : i) * 8) & 0xff); + return fromNumber(value, mostSignificantByteFirst); +} - return v; +ByteVector ByteVector::fromShort(short value, bool mostSignificantByteFirst) +{ + return fromNumber(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(value, mostSignificantByteFirst); } //////////////////////////////////////////////////////////////////////////////// diff --git a/toolkit/tbytevector.h b/toolkit/tbytevector.h index 31265e90..b7d88f41 100644 --- a/toolkit/tbytevector.h +++ b/toolkit/tbytevector.h @@ -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