Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #pragma once
00015
00019 #include "BitOps.h"
00020 #include "NetException.h"
00021
00022 namespace kNet
00023 {
00024
00027 template<int bits1, int bits2>
00028 class VLEType2
00029 {
00030 public:
00031 static const int numBits1 = bits1;
00032 static const int numBits2 = bits2;
00033
00034
00035 static const int numBits3 = 0;
00036
00037 static const u32 maxValue1 = LSBT<numBits1>::val;
00038 static const u32 maxValue2 = LSBT<numBits1+numBits2>::val;
00039
00040 static const u32 maxValue = maxValue2;
00041
00042 static const u32 bitsValue1 = numBits1 + 1;
00043 static const u32 bitsValue2 = numBits1 + 1 + numBits2;
00044
00045 static const u32 maxBits = bitsValue2;
00046
00047 static const u32 bitMask1 = BitMaskT<0, numBits1>::val;
00048 static const u32 bitMask2 = BitMaskT<bitsValue1, numBits2>::val;
00049
00050 static int GetEncodedBitLength(u32 value)
00051 {
00052 if (value <= maxValue1)
00053 return bitsValue1;
00054 else
00055 return bitsValue2;
00056 }
00057
00058 static u32 Encode(u32 value)
00059 {
00060 if (value > maxValue)
00061 throw NetException("VLEType2::Encode: Trying to encode too large value!");
00062 if (value <= maxValue1)
00063 return value;
00064 else
00065 {
00066 if (value > maxValue)
00067 value = maxValue;
00068
00069 u32 lowPart = value & BitMaskT<0, numBits1>::val;
00070 u32 medPart = value & BitMaskT<numBits1, numBits2>::val;
00071 assert(medPart != 0);
00072 return lowPart | (1 << numBits1) | (medPart << 1);
00073 }
00074 }
00075
00076 static u32 Decode(u32 value)
00077 {
00078 if ((value & (1 << numBits1)) == 0)
00079 return value;
00080 else
00081 {
00082 u32 lowPart = value & BitMaskT<0, numBits1>::val;
00083 u32 medPart = value & BitMaskT<numBits1+1, numBits2>::val;
00084 return lowPart | (medPart >> 1);
00085 }
00086 }
00087 };
00088
00091 template<int bits1, int bits2, int bits3>
00092 class VLEType3
00093 {
00094 public:
00095 static const int numBits1 = bits1;
00096 static const int numBits2 = bits2;
00097 static const int numBits3 = bits3;
00098
00099 static const u32 maxValue1 = LSBT<numBits1>::val;
00100 static const u32 maxValue2 = LSBT<numBits1+numBits2>::val;
00101 static const u32 maxValue3 = LSBT<numBits1+numBits2+numBits3>::val;
00102
00103 static const u32 maxValue = maxValue3;
00104
00105 static const u32 bitsValue1 = numBits1 + 1;
00106 static const u32 bitsValue2 = numBits1 + 1 + numBits2 + 1;
00107 static const u32 bitsValue3 = numBits1 + 1 + numBits2 + 1 + numBits3;
00108
00109 static const u32 maxBits = bitsValue3;
00110
00111 static const u32 bitMask1 = BitMaskT<0, numBits1>::val;
00112 static const u32 bitMask2 = BitMaskT<bitsValue1, numBits2>::val;
00113 static const u32 bitMask3 = BitMaskT<bitsValue2, numBits3>::val;
00114
00115 static int GetEncodedBitLength(u32 value)
00116 {
00117 if (value <= maxValue1)
00118 return bitsValue1;
00119 else if (value <= maxValue2)
00120 return bitsValue2;
00121 else
00122 return bitsValue3;
00123 }
00124
00125 static u32 Encode(u32 value)
00126 {
00127 if (value > maxValue)
00128 throw NetException("VLEType2::Encode: Trying to encode too large value!");
00129
00130 if (value > maxValue)
00131 value = maxValue;
00132
00133 if (value <= maxValue1)
00134 return value;
00135 else if (value <= maxValue2)
00136 {
00137 u32 lowPart = value & BitMaskT<0, numBits1>::val;
00138 u32 medPart = value & BitMaskT<numBits1, numBits2>::val;
00139 assert(medPart != 0);
00140 return lowPart | (1 << numBits1) | (medPart << 1);
00141 }
00142 else
00143 {
00144 u32 lowPart = value & BitMaskT<0, numBits1>::val;
00145 u32 medPart = value & BitMaskT<numBits1, numBits2>::val;
00146 u32 highPart = value & BitMaskT<numBits1+numBits2, numBits3>::val;
00147 assert(highPart != 0);
00148 return lowPart | (1 << numBits1) | (medPart << 1) | (1 << (bitsValue1 + numBits2)) | (highPart << 2);
00149 }
00150 }
00151
00152 static u32 Decode(u32 value)
00153 {
00154 if ((value & (1 << numBits1)) == 0)
00155 return value;
00156 else if ((value & (1 << (bitsValue1 + numBits2))) == 0)
00157 {
00158 u32 lowPart = value & BitMaskT<0, numBits1>::val;
00159 u32 medPart = value & BitMaskT<numBits1+1, numBits2>::val;
00160 return lowPart | (medPart >> 1);
00161 }
00162 else
00163 {
00164 u32 lowPart = value & BitMaskT<0, numBits1>::val;
00165 u32 medPart = (value & BitMaskT<numBits1+1, numBits2>::val);
00166 u32 highPart = (value & BitMaskT<numBits1+1+numBits2+1, numBits3>::val);
00167 return lowPart | (medPart >> 1) | (highPart >> 2);
00168 }
00169 }
00170 };
00171
00172 typedef VLEType3<7, 7, 16> VLE8_16_32;
00173 typedef VLEType2<7, 8> VLE8_16;
00174 typedef VLEType2<7, 24> VLE8_32;
00175 typedef VLEType2<15, 16> VLE16_32;
00176
00177 }