Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.1 KiB

  1. /**********************************************************************/
  2. /** Microsoft LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1990, 1991 **/
  4. /**********************************************************************/
  5. /*
  6. Bitfield.hxx
  7. This file contains the class definition for the bitfield class.
  8. FILE HISTORY:
  9. Johnl 13-Jul-1991 Created
  10. */
  11. #ifndef _BITFIELD_HXX_
  12. #define _BITFIELD_HXX_
  13. #include "base.hxx"
  14. #include "uibuffer.hxx"
  15. enum BITVALUES { OFF = 0 , ON = 1 } ;
  16. /*************************************************************************
  17. NAME: BITFIELD
  18. SYNOPSIS: This class provides a variable size bitfield class
  19. INTERFACE:
  20. Fill in after comments
  21. PARENT: BASE
  22. CAVEATS:
  23. It is assumed that the last byte of the bitvector extends to a byte
  24. boundary. This allows multi-byte settings without having to worry
  25. about the half byte case at the end. Any unused bits should always
  26. be set to 0.
  27. NOTES:
  28. HISTORY:
  29. Johnl 13-Jul-1991 Created
  30. **************************************************************************/
  31. DLL_CLASS BITFIELD : public BASE
  32. {
  33. public:
  34. /* All bits are initialized to 0 during construction
  35. */
  36. BITFIELD( unsigned cBitsInBitfield, enum BITVALUES bitInit ) ;
  37. /* Initializes the bitfield to the chunk of memory pointed at by
  38. * pbInitValue of size cInitBytes. If cTotalBytes is 0, then the
  39. * size of the bitfield is assumed to be 8*cInitBytes bits long, otherwise
  40. * the bitfield is cTotalBits long.
  41. */
  42. BITFIELD( const BYTE * pbInitValue,
  43. unsigned cInitBytes,
  44. unsigned cTotalBits = 0 ) ;
  45. BITFIELD( const BITFIELD & bitfieldSrc ) ;
  46. /* Provide easy ways to initialize the bitfield with standard types
  47. * which will generally be manifests.
  48. * The size is assumed to be the size of the data type.
  49. */
  50. BITFIELD( USHORT usInit ) ;
  51. BITFIELD( ULONG ulInit ) ;
  52. ~BITFIELD() ;
  53. /* Resizes the bitfield to the specified count of bits.
  54. */
  55. APIERR Resize( unsigned cBitsInBitfield ) ;
  56. /* Clear or set all of the bits in the bitfield
  57. */
  58. void SetAllBits( enum BITVALUES bit = ON ) ;
  59. /* Set the bit at bit offset iBitPos to the value of bBit (defaults to 1).
  60. */
  61. void SetBit( unsigned iBitPos, enum BITVALUES bitVal = ON ) ;
  62. BOOL IsBitSet( unsigned iBitPos ) const ;
  63. //
  64. // Does a bitwise complement of the bitfield
  65. //
  66. void Not( void ) ;
  67. /* Given two bitfields, ANDs the contents and returns TRUE if any of the
  68. * same positioned bits are set.
  69. * Example: if ( bitOldBits & bitMaskBits ) ... ;
  70. *
  71. * For an equivalent OR operation, simply check if any bits are set
  72. */
  73. BOOL operator&( const BITFIELD & bitfieldSrc ) ;
  74. /* ORs or ANDs or Assigns the bitfield on the right with the target bitfield on the
  75. * left.
  76. * Example: bitMyBits |= bitMaskBits ;
  77. *
  78. * The bitfields must be of the same size.
  79. */
  80. BITFIELD & operator=( const BITFIELD & bitfieldSrc ) ;
  81. BITFIELD & operator=( ULONG ulMask ) ;
  82. BITFIELD & operator=( USHORT usMask ) ;
  83. void operator&=( const BITFIELD & bitfieldSrc ) ;
  84. void operator|=( const BITFIELD & bitfieldSrc ) ;
  85. BOOL operator==( BITFIELD & bitfieldSrc ) ;
  86. BOOL operator==( ULONG ulMask ) const ;
  87. BOOL operator==( USHORT usMask ) const ;
  88. BOOL operator!=( BITFIELD & bitfieldSrc )
  89. { return !operator==( bitfieldSrc ) ; }
  90. /* Provide easy way to do masks with common types, the bitfield does not
  91. * have to be the same size as the operand type, must be at least as big
  92. * however.
  93. */
  94. //void operator&=( BYTE bSrc ) ;
  95. void operator&=( USHORT usSrc ) ;
  96. void operator&=( ULONG ulSrc ) ;
  97. //void operator|=( BYTE bSrc ) ;
  98. void operator|=( USHORT usSrc ) ;
  99. void operator|=( ULONG ulSrc ) ;
  100. /* Conversion operators:
  101. *
  102. * The size must match the size of the bitfield.
  103. */
  104. operator ULONG() ;
  105. operator USHORT() ;
  106. //operator BYTE() ;
  107. /* Returns the number of bits in this bitfield
  108. */
  109. unsigned QueryCount( void ) const
  110. { return _cBitsInBitfield ; }
  111. /* Returns the number of BYTEs it takes to represent this bitfield.
  112. *
  113. * Need to add one if a bit overhangs a BYTE boundary.
  114. *
  115. * BUGBUG - Misleading name, is really Byte count of bitfield
  116. */
  117. unsigned QueryAllocSize( void ) const
  118. { return ( QueryCount()/8 + ( QueryCount() % 8 ? 1 : 0 ) ) ; }
  119. /* Return the number of bits into the byte the requested bit is at
  120. */
  121. unsigned QueryOffset( unsigned iBitPos ) const
  122. { return iBitPos % 8 ; }
  123. protected:
  124. /* Get a pointer to the BYTE which contains the requested bit (for
  125. * multi-byte setting, should occur on a BYTE boundary).
  126. * iBitOffset - index of the requested bit (0 = 1st bit, 1 = 2nd bit etc.)
  127. * cbitsTargetOpSize - number of bits that will be used in the
  128. * operation, used for bounds checking
  129. */
  130. BYTE * QueryBitPos( unsigned iBitOffset, unsigned cbitsTargetOpSize ) const ;
  131. /* Returns TRUE if the bitfield had to be allocated in _pbBitVector,
  132. * returns FALSE if the bitfield is wholly contained in _ulBitfield.
  133. */
  134. BOOL IsAllocated( void ) const
  135. { return ( _cBitsInBitfield > QueryMaxNonAllocBitCount() ) ; }
  136. /* Returns the number of bits that a BITFIELD object can hold without
  137. * allocating any memory.
  138. */
  139. unsigned QueryMaxNonAllocBitCount( void ) const
  140. { return (8 * sizeof(ULONG)) ; }
  141. /* Allocates the memory required by the bitfield class if the size
  142. * is greater then what can fit inline.
  143. */
  144. APIERR AllocBitfield( unsigned cBitsInBitfield ) ;
  145. private:
  146. /* The actual bitfield.
  147. */
  148. union
  149. {
  150. BYTE * _pbBitVector ; // Pointer to allocated bitfield
  151. ULONG _ulBitfield ; // ULONG bitfield if bitfield < 32 bits
  152. } ;
  153. /* Count of bits that are in this bitfield.
  154. */
  155. unsigned _cBitsInBitfield ;
  156. } ;
  157. #endif // _BITFIELD_HXX_