Counter Strike : Global Offensive Source Code
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.

124 lines
2.9 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Simple class to make it easier to deal with flags
  4. //
  5. //=============================================================================
  6. #ifndef UTLFLAGS_H
  7. #define UTLFLAGS_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "tier0/dbg.h"
  12. //-----------------------------------------------------------------------------
  13. // Simple class to make it easier to deal with flags
  14. //-----------------------------------------------------------------------------
  15. template< class T >
  16. class CUtlFlags
  17. {
  18. public:
  19. CUtlFlags( int nInitialFlags = 0 );
  20. // Flag setting
  21. void SetFlag( int nFlagMask );
  22. void SetFlag( int nFlagMask, bool bEnable );
  23. // Flag clearing
  24. void ClearFlag( int nFlagMask );
  25. void ClearAllFlags();
  26. bool IsFlagSet( int nFlagMask ) const;
  27. // Is any flag set?
  28. bool IsAnyFlagSet() const;
  29. private:
  30. T m_nFlags;
  31. };
  32. //-----------------------------------------------------------------------------
  33. // Constructor
  34. //-----------------------------------------------------------------------------
  35. template< class T >
  36. CUtlFlags<T>::CUtlFlags( int nInitialFlags )
  37. {
  38. // Makes sure we didn't truncate
  39. Assert( nInitialFlags == (T)nInitialFlags );
  40. m_nFlags = (T)nInitialFlags;
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Set flags
  44. //-----------------------------------------------------------------------------
  45. template< class T >
  46. void CUtlFlags<T>::SetFlag( int nFlagMask )
  47. {
  48. // Makes sure we didn't truncate
  49. Assert( nFlagMask == (T)nFlagMask );
  50. m_nFlags |= (T)nFlagMask;
  51. }
  52. template< class T >
  53. void CUtlFlags<T>::SetFlag( int nFlagMask, bool bEnable )
  54. {
  55. // Makes sure we didn't truncate
  56. Assert( nFlagMask == (T)nFlagMask );
  57. if ( bEnable )
  58. {
  59. m_nFlags |= (T)nFlagMask;
  60. }
  61. else
  62. {
  63. m_nFlags &= ~((T)nFlagMask);
  64. }
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Clear flags
  68. //-----------------------------------------------------------------------------
  69. template< class T >
  70. void CUtlFlags<T>::ClearFlag( int nFlagMask )
  71. {
  72. // Makes sure we didn't truncate
  73. Assert( nFlagMask == (T)nFlagMask );
  74. m_nFlags &= ~((T)nFlagMask);
  75. }
  76. template< class T >
  77. void CUtlFlags<T>::ClearAllFlags()
  78. {
  79. m_nFlags = 0;
  80. }
  81. //-----------------------------------------------------------------------------
  82. // Is a flag set?
  83. //-----------------------------------------------------------------------------
  84. template< class T >
  85. bool CUtlFlags<T>::IsFlagSet( int nFlagMask ) const
  86. {
  87. // Makes sure we didn't truncate
  88. Assert( nFlagMask == (T)nFlagMask );
  89. return ( m_nFlags & nFlagMask ) != 0;
  90. }
  91. //-----------------------------------------------------------------------------
  92. // Is any flag set?
  93. //-----------------------------------------------------------------------------
  94. template< class T >
  95. bool CUtlFlags<T>::IsAnyFlagSet() const
  96. {
  97. return m_nFlags != 0;
  98. }
  99. #endif // UTLFLAGS_H