gwnavruntime/containers/bitfieldfixedsize.h Source File

bitfieldfixedsize.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 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 #pragma once
9 
11 #include <string.h>
12 
13 namespace Kaim
14 {
15 
16 template <KyUInt32 N>
17 class BitFieldFixedSize
18 {
19 public:
20  BitFieldFixedSize() { UnsetAllBits(); }
21 
22  void SetBit(KyUInt32 index) { GetByteByGlobalIndex(index) |= (KyUInt8) 1 << GetPosInElem(index); }
23  void UnsetBit(KyUInt32 index) { GetByteByGlobalIndex(index) &= (KyUInt8) ~(1 << GetPosInElem(index)); }
24 
25  void UnsetAllBits() { memset(m_bytes, '\0', sizeof (m_bytes)); }
26  void SetAllBits() { memset(m_bytes, '\xFF', sizeof (m_bytes)); }
27 
28  bool IsBitSet(KyUInt32 index) const { return ((GetByteByGlobalIndex(index) >> GetPosInElem(index)) & 0x01) != 0; }
29 protected:
30  const KyUInt8& GetByteByGlobalIndex(KyUInt32 index) const { return m_bytes[index / nbBitPerByte]; }
31  KyUInt8& GetByteByGlobalIndex(KyUInt32 index) { return m_bytes[index / nbBitPerByte]; }
32  KyUInt32 GetPosInElem(KyUInt32 index) const { return (index & mask); }
33 protected:
34  enum { mask = 0x07, nbBitPerByte = 8};
35  enum { nbBytes = (N + 7) / 8 };
36 protected:
37  KyUInt8 m_bytes[nbBytes];
38 };
39 
40 } // namespace Kaim
41 
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
std::uint8_t KyUInt8
uint8_t
Definition: types.h:27