flags.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/flags.h
  3. // Purpose: a bitset suited for replacing the current style flags
  4. // Author: Stefan Csomor
  5. // Modified by:
  6. // Created: 27/07/03
  7. // Copyright: (c) 2003 Stefan Csomor
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_SETH__
  11. #define _WX_SETH__
  12. // wxBitset should be applied to an enum, then this can be used like
  13. // bitwise operators but keeps the type safety and information, the
  14. // enums must be in a sequence , their value determines the bit position
  15. // that they represent
  16. // The api is made as close as possible to <bitset>
  17. template <class T> class wxBitset
  18. {
  19. friend class wxEnumData ;
  20. public:
  21. // creates a wxBitset<> object with all flags initialized to 0
  22. wxBitset() { m_data = 0; }
  23. // created a wxBitset<> object initialized according to the bits of the
  24. // integral value val
  25. wxBitset(unsigned long val) { m_data = val ; }
  26. // copies the content in the new wxBitset<> object from another one
  27. wxBitset(const wxBitset &src) { m_data = src.m_data; }
  28. // creates a wxBitset<> object that has the specific flag set
  29. wxBitset(const T el) { m_data |= 1 << el; }
  30. // returns the integral value that the bits of this object represent
  31. unsigned long to_ulong() const { return m_data ; }
  32. // assignment
  33. wxBitset &operator =(const wxBitset &rhs)
  34. {
  35. m_data = rhs.m_data;
  36. return *this;
  37. }
  38. // bitwise or operator, sets all bits that are in rhs and leaves
  39. // the rest unchanged
  40. wxBitset &operator |=(const wxBitset &rhs)
  41. {
  42. m_data |= rhs.m_data;
  43. return *this;
  44. }
  45. // bitwsie exclusive-or operator, toggles the value of all bits
  46. // that are set in bits and leaves all others unchanged
  47. wxBitset &operator ^=(const wxBitset &rhs) // difference
  48. {
  49. m_data ^= rhs.m_data;
  50. return *this;
  51. }
  52. // bitwise and operator, resets all bits that are not in rhs and leaves
  53. // all others unchanged
  54. wxBitset &operator &=(const wxBitset &rhs) // intersection
  55. {
  56. m_data &= rhs.m_data;
  57. return *this;
  58. }
  59. // bitwise or operator, returns a new bitset that has all bits set that set are in
  60. // bitset2 or in this bitset
  61. wxBitset operator |(const wxBitset &bitset2) const // union
  62. {
  63. wxBitset<T> s;
  64. s.m_data = m_data | bitset2.m_data;
  65. return s;
  66. }
  67. // bitwise exclusive-or operator, returns a new bitset that has all bits set that are set either in
  68. // bitset2 or in this bitset but not in both
  69. wxBitset operator ^(const wxBitset &bitset2) const // difference
  70. {
  71. wxBitset<T> s;
  72. s.m_data = m_data ^ bitset2.m_data;
  73. return s;
  74. }
  75. // bitwise and operator, returns a new bitset that has all bits set that are set both in
  76. // bitset2 and in this bitset
  77. wxBitset operator &(const wxBitset &bitset2) const // intersection
  78. {
  79. wxBitset<T> s;
  80. s.m_data = m_data & bitset2.m_data;
  81. return s;
  82. }
  83. // sets appropriate the bit to true
  84. wxBitset& set(const T el) //Add element
  85. {
  86. m_data |= 1 << el;
  87. return *this;
  88. }
  89. // clears the appropriate flag to false
  90. wxBitset& reset(const T el) //remove element
  91. {
  92. m_data &= ~(1 << el);
  93. return *this;
  94. }
  95. // clear all flags
  96. wxBitset& reset()
  97. {
  98. m_data = 0;
  99. return *this;
  100. }
  101. // true if this flag is set
  102. bool test(const T el) const
  103. {
  104. return (m_data & (1 << el)) ? true : false;
  105. }
  106. // true if no flag is set
  107. bool none() const
  108. {
  109. return m_data == 0;
  110. }
  111. // true if any flag is set
  112. bool any() const
  113. {
  114. return m_data != 0;
  115. }
  116. // true if both have the same flags
  117. bool operator ==(const wxBitset &rhs) const
  118. {
  119. return m_data == rhs.m_data;
  120. }
  121. // true if both differ in their flags set
  122. bool operator !=(const wxBitset &rhs) const
  123. {
  124. return !operator==(rhs);
  125. }
  126. bool operator[] (const T el) const { return test(el) ; }
  127. private :
  128. unsigned long m_data;
  129. };
  130. #if wxUSE_EXTENDED_RTTI
  131. #define wxDEFINE_FLAGS( flags ) \
  132. class WXDLLIMPEXP_BASE flags \
  133. {\
  134. public : \
  135. flags(long data=0) :m_data(data) {} \
  136. long m_data ;\
  137. bool operator ==(const flags &rhs) const { return m_data == rhs.m_data; }\
  138. } ;
  139. #else
  140. #define wxDEFINE_FLAGS( flags )
  141. #endif
  142. #if WXWIN_COMPATIBILITY_2_8
  143. #define WX_DEFINE_FLAGS wxDEFINE_FLAGS
  144. #endif
  145. #endif