gwnavruntime/containers/bitfieldfixedsize.h Source File

bitfieldfixedsize.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 #ifndef Navigation_BitFieldFixedSize_H
9 #define Navigation_BitFieldFixedSize_H
10 
12 #include <string.h>
13 
14 namespace Kaim
15 {
16 
17 template <KyUInt32 N>
18 class BitFieldFixedSize
19 {
20 public:
21  BitFieldFixedSize() { UnsetAllBits(); }
22 
23  void SetBit(KyUInt32 index) { GetByteByGlobalIndex(index) |= (KyUInt8) 1 << GetPosInElem(index); }
24  void UnsetBit(KyUInt32 index) { GetByteByGlobalIndex(index) &= (KyUInt8) ~(1 << GetPosInElem(index)); }
25 
26  void UnsetAllBits() { memset(m_bytes, '\0', sizeof (m_bytes)); }
27  void SetAllBits() { memset(m_bytes, '\xFF', sizeof (m_bytes)); }
28 
29  bool IsBitSet(KyUInt32 index) const { return ((GetByteByGlobalIndex(index) >> GetPosInElem(index)) & 0x01) != 0; }
30 protected:
31  const KyUInt8& GetByteByGlobalIndex(KyUInt32 index) const { return m_bytes[index / nbBitPerByte]; }
32  KyUInt8& GetByteByGlobalIndex(KyUInt32 index) { return m_bytes[index / nbBitPerByte]; }
33  KyUInt32 GetPosInElem(KyUInt32 index) const { return (index & mask); }
34 protected:
35  enum { mask = 0x07, nbBitPerByte = 8};
36  enum { nbBytes = (N + 7) / 8 };
37 protected:
38  KyUInt8 m_bytes[nbBytes];
39 };
40 
41 } // namespace Kaim
42 
43 #endif // Navigation_BitFieldFixedSize_H
unsigned char KyUInt8
Type used internally to represent an unsigned 8-bit integer.
Definition: types.h:41
Definition: gamekitcrowddispersion.h:20
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36