]> granicus.if.org Git - taglib/commitdiff
Add new type and templatize the "fromNumber" conversion.
authorScott Wheeler <wheeler@kde.org>
Tue, 27 Apr 2004 01:29:22 +0000 (01:29 +0000)
committerScott Wheeler <wheeler@kde.org>
Tue, 27 Apr 2004 01:29:22 +0000 (01:29 +0000)
git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@306704 283d02a7-25f6-0310-bc7c-ecb5cbfe19da

toolkit/tbytevector.cpp
toolkit/tbytevector.h

index 3ef1b6bca4319a73b9577d42a2f656f76db47b09..78c1ac23cdda62c22c038949e9674633c6354316 100644 (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);
-
-  for(int i = 0; i < 4; i++)
-    v[i] = uchar(value >> ((mostSignificantByteFirst ? 3 - i : i) * 8) & 0xff);
+  return fromNumber<uint>(value, mostSignificantByteFirst);
+}
 
-  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);
 }
 
 ////////////////////////////////////////////////////////////////////////////////
index 31265e90c68b73bc775347d6bb80706362dd5d83..b7d88f41ecd896e49d74cb32e7db44fa9dd4bf91 100644 (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