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