|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Collections.Generic; |
|
19 using System.Text; |
|
20 |
|
21 namespace SymbianUtils.BasicTypes |
|
22 { |
|
23 public class SymUInt32 : SymUIntBase |
|
24 { |
|
25 #region Constructors |
|
26 public SymUInt32( uint aValue ) |
|
27 : base( aValue, 32 ) |
|
28 { |
|
29 } |
|
30 |
|
31 public SymUInt32( string aBinary ) |
|
32 : base( aBinary, 32 ) |
|
33 { |
|
34 } |
|
35 #endregion |
|
36 |
|
37 #region Operators |
|
38 public static SymUInt32 operator &( SymUInt32 aLeft, SymUInt32 aRight ) |
|
39 { |
|
40 uint val = aLeft.RawValue & aRight.RawValue; |
|
41 return new SymUInt32( val ); |
|
42 } |
|
43 |
|
44 public static implicit operator SymUInt32( uint aBasicType ) |
|
45 { |
|
46 return new SymUInt32( aBasicType ); |
|
47 } |
|
48 #endregion |
|
49 } |
|
50 |
|
51 public class SymUInt16 : SymUIntBase |
|
52 { |
|
53 #region Constructors |
|
54 public SymUInt16( ushort aValue ) |
|
55 : base( aValue, 16 ) |
|
56 { |
|
57 } |
|
58 |
|
59 public SymUInt16( string aBinary ) |
|
60 : base( aBinary, 16 ) |
|
61 { |
|
62 } |
|
63 #endregion |
|
64 |
|
65 #region Operators |
|
66 public static SymUInt16 operator &( SymUInt16 aLeft, SymUInt16 aRight ) |
|
67 { |
|
68 ushort val = Convert.ToUInt16( aLeft.RawValue & aRight.RawValue ); |
|
69 return new SymUInt16( val ); |
|
70 } |
|
71 public static implicit operator SymUInt16( ushort aBasicType ) |
|
72 { |
|
73 return new SymUInt16( aBasicType ); |
|
74 } |
|
75 #endregion |
|
76 } |
|
77 |
|
78 public class SymUInt8 : SymUIntBase |
|
79 { |
|
80 #region Constructors |
|
81 public SymUInt8( byte aValue ) |
|
82 : base( aValue, 8 ) |
|
83 { |
|
84 } |
|
85 |
|
86 public SymUInt8( string aBinary ) |
|
87 : base( aBinary, 8 ) |
|
88 { |
|
89 } |
|
90 #endregion |
|
91 |
|
92 #region Operators |
|
93 public static SymUInt8 operator &( SymUInt8 aLeft, SymUInt8 aRight ) |
|
94 { |
|
95 byte val = Convert.ToByte( aLeft.RawValue & aRight.RawValue ); |
|
96 return new SymUInt8( val ); |
|
97 } |
|
98 |
|
99 public static implicit operator SymUInt8( byte aBasicType ) |
|
100 { |
|
101 return new SymUInt8( aBasicType ); |
|
102 } |
|
103 #endregion |
|
104 } |
|
105 |
|
106 public abstract class SymUIntBase : IFormattable |
|
107 { |
|
108 #region Constructors |
|
109 protected SymUIntBase() |
|
110 { |
|
111 } |
|
112 |
|
113 protected SymUIntBase( string aBinary, int aNumberOfBits ) |
|
114 { |
|
115 iValue = SymBitUtils.CreateMask( aBinary ); |
|
116 iNumberOfBits = aNumberOfBits; |
|
117 } |
|
118 |
|
119 protected SymUIntBase( uint aValue, int aNumberOfBits ) |
|
120 { |
|
121 iValue = aValue; |
|
122 iNumberOfBits = aNumberOfBits; |
|
123 } |
|
124 #endregion |
|
125 |
|
126 #region API |
|
127 public bool IsMatch( string aBinary ) |
|
128 { |
|
129 uint value = 0; |
|
130 uint mask = SymBitUtils.CreateMask( aBinary, out value ); |
|
131 bool ret = ( mask & iValue ) == value; |
|
132 return ret; |
|
133 } |
|
134 |
|
135 public uint RShift( int aBits ) |
|
136 { |
|
137 uint v = iValue; |
|
138 v >>= aBits; |
|
139 this.RawValue = v; |
|
140 return v; |
|
141 } |
|
142 |
|
143 public uint LShift( int aBits ) |
|
144 { |
|
145 uint v = (uint) ( iValue << aBits ); |
|
146 this.RawValue = v; |
|
147 return v; |
|
148 } |
|
149 |
|
150 public SymUInt32 RotateRight( int aCount ) |
|
151 { |
|
152 uint ret = iValue; |
|
153 // |
|
154 for ( int i = 0; i < aCount; i++ ) |
|
155 { |
|
156 bool bitZeroSet = ( ( ret & 0x1 ) == 0x1 ); |
|
157 ret >>= 1; |
|
158 // |
|
159 if ( bitZeroSet ) |
|
160 { |
|
161 // Wrap the bit around by appling it at the top end |
|
162 ret |= KTopBit; |
|
163 } |
|
164 } |
|
165 // |
|
166 return new SymUInt32( ret ); |
|
167 } |
|
168 |
|
169 public uint ToUInt() |
|
170 { |
|
171 return iValue; |
|
172 } |
|
173 #endregion |
|
174 |
|
175 #region Properties |
|
176 public int NumberOfBits |
|
177 { |
|
178 get { return iNumberOfBits; } |
|
179 protected set |
|
180 { |
|
181 switch ( value ) |
|
182 { |
|
183 default: |
|
184 throw new ArgumentException( "Number of bits must be 8, 16 or 32" ); |
|
185 case 8: |
|
186 case 16: |
|
187 case 32: |
|
188 iNumberOfBits = value; |
|
189 break; |
|
190 } |
|
191 } |
|
192 } |
|
193 |
|
194 public int NumberOfBytes |
|
195 { |
|
196 get { return NumberOfBits / 4; } |
|
197 set |
|
198 { |
|
199 this.NumberOfBits = value * 8; |
|
200 } |
|
201 } |
|
202 |
|
203 public uint MaxValue |
|
204 { |
|
205 get |
|
206 { |
|
207 uint ret = uint.MaxValue; |
|
208 if ( iNumberOfBits == 16 ) |
|
209 { |
|
210 ret = ushort.MaxValue; |
|
211 } |
|
212 else if ( iNumberOfBits == 8 ) |
|
213 { |
|
214 ret = byte.MaxValue; |
|
215 } |
|
216 return ret; |
|
217 } |
|
218 } |
|
219 |
|
220 public string Binary |
|
221 { |
|
222 get |
|
223 { |
|
224 string bits = SymBitUtils.BeautifyBits( iValue, iNumberOfBits ); |
|
225 return bits; |
|
226 } |
|
227 } |
|
228 |
|
229 public string Hex |
|
230 { |
|
231 get |
|
232 { |
|
233 string hex = iValue.ToString( string.Format( "x{0}", this.NumberOfBytes * 2 ) ); |
|
234 return hex; |
|
235 } |
|
236 } |
|
237 |
|
238 public SymBit this[ int aIndex ] |
|
239 { |
|
240 get |
|
241 { |
|
242 if ( aIndex < 0 || aIndex > iNumberOfBits ) |
|
243 { |
|
244 throw new ArgumentException( "Specified bit is out of bounds" ); |
|
245 } |
|
246 |
|
247 uint mask = ( 1u << aIndex ); |
|
248 SymBit ret = ( ( mask & iValue ) == mask ) ? SymBit.ESet : SymBit.EClear; |
|
249 return ret; |
|
250 } |
|
251 } |
|
252 |
|
253 public SymUInt32 this[ int aHighBitIndex, int aLowBitIndex ] |
|
254 { |
|
255 get |
|
256 { |
|
257 if ( aHighBitIndex < aLowBitIndex ) |
|
258 { |
|
259 throw new ArgumentException( "High bit index must be less than low bit index" ); |
|
260 } |
|
261 |
|
262 // Build mask |
|
263 uint mask = 0; |
|
264 for ( int i = aLowBitIndex; i <= aHighBitIndex; i++ ) |
|
265 { |
|
266 mask |= ( 1u << i ); |
|
267 } |
|
268 |
|
269 uint ret = iValue & mask; |
|
270 |
|
271 // Shift |
|
272 ret >>= aLowBitIndex; |
|
273 |
|
274 return new SymUInt32( ret ); |
|
275 } |
|
276 } |
|
277 #endregion |
|
278 |
|
279 #region Operators |
|
280 public static implicit operator uint( SymUIntBase aObject ) |
|
281 { |
|
282 return aObject.RawValue; |
|
283 } |
|
284 |
|
285 public static implicit operator ushort( SymUIntBase aObject ) |
|
286 { |
|
287 ushort ret = Convert.ToUInt16( aObject.RawValue ); |
|
288 return ret; |
|
289 } |
|
290 |
|
291 public static implicit operator byte( SymUIntBase aObject ) |
|
292 { |
|
293 byte ret = Convert.ToByte( aObject.RawValue ); |
|
294 return ret; |
|
295 } |
|
296 |
|
297 public static bool operator ==( SymUIntBase aLeft, SymUIntBase aRight ) |
|
298 { |
|
299 // If both are null, or both are same instance, return true. |
|
300 if ( System.Object.ReferenceEquals( aLeft, aRight ) ) |
|
301 { |
|
302 return true; |
|
303 } |
|
304 |
|
305 // If one is null, but not both, return false. |
|
306 if ( ( (object) aLeft == null ) || ( (object) aRight == null ) ) |
|
307 { |
|
308 return false; |
|
309 } |
|
310 |
|
311 bool ret = aLeft.Equals( aRight ); |
|
312 return ret; |
|
313 } |
|
314 |
|
315 public static bool operator !=( SymUIntBase aLeft, SymUIntBase aRight ) |
|
316 { |
|
317 bool result = !( aLeft == aRight ); |
|
318 return result; |
|
319 } |
|
320 |
|
321 public static SymUInt32 operator &( SymUIntBase aLeft, SymUIntBase aRight ) |
|
322 { |
|
323 uint val = ( aLeft.RawValue & aRight.RawValue ); |
|
324 return new SymUInt32( val ); |
|
325 } |
|
326 #endregion |
|
327 |
|
328 #region IFormattable Members |
|
329 public string ToString( string aFormat, IFormatProvider aFormatProvider ) |
|
330 { |
|
331 if ( aFormatProvider != null ) |
|
332 { |
|
333 ICustomFormatter formatter = aFormatProvider.GetFormat( this.GetType() ) as ICustomFormatter; |
|
334 if ( formatter != null ) |
|
335 { |
|
336 return formatter.Format( aFormat, this, aFormatProvider ); |
|
337 } |
|
338 } |
|
339 |
|
340 string ret = string.Empty; |
|
341 string format = aFormat != null ? aFormat : "full"; |
|
342 // |
|
343 switch ( format ) |
|
344 { |
|
345 case "full": |
|
346 ret = string.Format( "0x{0} [{1}]", Hex, Binary ); |
|
347 break; |
|
348 default: |
|
349 ret = iValue.ToString( aFormat, aFormatProvider ); |
|
350 break; |
|
351 } |
|
352 // |
|
353 return ret; |
|
354 } |
|
355 #endregion |
|
356 |
|
357 #region From System.Object |
|
358 public override string ToString() |
|
359 { |
|
360 return ToString( null ); |
|
361 } |
|
362 |
|
363 public string ToString( string aFormat ) |
|
364 { |
|
365 return ToString( aFormat, null ); |
|
366 } |
|
367 |
|
368 public override bool Equals( object aObject ) |
|
369 { |
|
370 bool ret = false; |
|
371 // |
|
372 if ( aObject != null && aObject is SymUIntBase ) |
|
373 { |
|
374 SymUIntBase other = (SymUIntBase) aObject; |
|
375 // |
|
376 if ( other.NumberOfBits == this.NumberOfBits ) |
|
377 { |
|
378 ret = ( other.RawValue == this.RawValue ); |
|
379 } |
|
380 } |
|
381 // |
|
382 return ret; |
|
383 } |
|
384 |
|
385 public override int GetHashCode() |
|
386 { |
|
387 return iValue.GetHashCode(); |
|
388 } |
|
389 #endregion |
|
390 |
|
391 #region Internal properties & methods |
|
392 protected uint RawValue |
|
393 { |
|
394 get { return iValue; } |
|
395 set |
|
396 { |
|
397 if ( value > MaxValue ) |
|
398 { |
|
399 throw new ArgumentException( string.Format( "Specified value {0} exceeds bit range ({1})", value, this.NumberOfBits ) ); |
|
400 } |
|
401 iValue = value; |
|
402 } |
|
403 } |
|
404 #endregion |
|
405 |
|
406 #region Internal constants |
|
407 private const uint KTopBit = 0x80000000; |
|
408 #endregion |
|
409 |
|
410 #region Data ember |
|
411 private uint iValue = 0; |
|
412 private int iNumberOfBits = 32; |
|
413 #endregion |
|
414 } |
|
415 } |