1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: a bitset suited for replacing the current style flags
4 // Author: Stefan Csomor
8 // Copyright: (c) 2003 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "flags.h"
19 // wxBitset should be applied to an enum, then this can be used like
20 // bitwise operators but keeps the type safety and information, the
21 // enums must be in a sequence , their value determines the bit position
22 // that they represent
23 // The api is made as close as possible to <bitset>
25 template <class T
> class wxBitset
27 friend class wxEnumData
;
29 // creates a wxBitset<> object with all flags initialized to 0
30 wxBitset() { m_data
= 0; }
32 // created a wxBitset<> object initialized according to the bits of the
34 wxBitset(unsigned long val
) { m_data
= val
; }
36 // copies the content in the new wxBitset<> object from another one
37 wxBitset(const wxBitset
&src
) { m_data
= src
.m_data
; }
39 // creates a wxBitset<> object that has the specific flag set
40 wxBitset(const T el
) { m_data
|= 1 << el
; }
42 // returns the integral value that the bits of this object represent
43 unsigned long to_ulong() const { return m_data
; }
46 wxBitset
&operator =(const wxBitset
&rhs
)
52 // bitwise or operator, sets all bits that are in rhs and leaves
54 wxBitset
&operator |=(const wxBitset
&rhs
)
60 // bitwsie exclusive-or operator, toggles the value of all bits
61 // that are set in bits and leaves all others unchanged
62 wxBitset
&operator ^=(const wxBitset
&rhs
) // difference
68 // bitwise and operator, resets all bits that are not in rhs and leaves
69 // all others unchanged
70 wxBitset
&operator &=(const wxBitset
&rhs
) // intersection
76 // bitwise or operator, returns a new bitset that has all bits set that set are in
77 // bitset2 or in this bitset
78 wxBitset
operator |(const wxBitset
&bitset2
) const // union
81 s
.m_data
= m_data
| bitset2
.m_data
;
85 // bitwise exclusive-or operator, returns a new bitset that has all bits set that are set either in
86 // bitset2 or in this bitset but not in both
87 wxBitset
operator ^(const wxBitset
&bitset2
) const // difference
90 s
.m_data
= m_data
^ bitset2
.m_data
;
94 // bitwise and operator, returns a new bitset that has all bits set that are set both in
95 // bitset2 and in this bitset
96 wxBitset
operator &(const wxBitset
&bitset2
) const // intersection
99 s
.m_data
= m_data
& bitset2
.m_data
;
103 // sets appropriate the bit to true
104 wxBitset
& set(const T el
) //Add element
110 // clears the appropriate flag to false
111 wxBitset
& reset(const T el
) //remove element
113 m_data
&= ~(1 << el
);
124 // true if this flag is set
125 bool test(const T el
) const
127 return (m_data
& (1 << el
)) ? true : false;
130 // true if no flag is set
136 // true if any flag is set
142 // true if both have the same flags
143 bool operator ==(const wxBitset
&rhs
) const
145 return m_data
== rhs
.m_data
;
148 // true if both differ in their flags set
149 bool operator !=(const wxBitset
&rhs
) const
151 return !operator==(rhs
);
154 bool operator[] (const T el
) const { return test(el
) ; }
157 unsigned long m_data
;
160 #define WX_DEFINE_FLAGS( flags ) \
161 class WXDLLEXPORT flags \
164 flags(long data=0) :m_data(data) {} \
166 bool operator ==(const flags &rhs) const { return m_data == rhs.m_data; }\