]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/flags.h
0b2ffe8dc8cbea9b43b6197d3aacbbe13db14fd1
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 // wxBitset should be applied to an enum, then this can be used like
16 // bitwise operators but keeps the type safety and information, the
17 // enums must be in a sequence , their value determines the bit position
18 // that they represent
19 // The api is made as close as possible to <bitset>
21 template <class T
> class wxBitset
23 friend class wxEnumData
;
25 // creates a wxBitset<> object with all flags initialized to 0
26 wxBitset() { m_data
= 0; }
28 // created a wxBitset<> object initialized according to the bits of the
30 wxBitset(unsigned long val
) { m_data
= val
; }
32 // copies the content in the new wxBitset<> object from another one
33 wxBitset(const wxBitset
&src
) { m_data
= src
.m_data
; }
35 // creates a wxBitset<> object that has the specific flag set
36 wxBitset(const T el
) { m_data
|= 1 << el
; }
38 // returns the integral value that the bits of this object represent
39 unsigned long to_ulong() const { return m_data
; }
42 wxBitset
&operator =(const wxBitset
&rhs
)
48 // bitwise or operator, sets all bits that are in rhs and leaves
50 wxBitset
&operator |=(const wxBitset
&rhs
)
56 // bitwsie exclusive-or operator, toggles the value of all bits
57 // that are set in bits and leaves all others unchanged
58 wxBitset
&operator ^=(const wxBitset
&rhs
) // difference
64 // bitwise and operator, resets all bits that are not in rhs and leaves
65 // all others unchanged
66 wxBitset
&operator &=(const wxBitset
&rhs
) // intersection
72 // bitwise or operator, returns a new bitset that has all bits set that set are in
73 // bitset2 or in this bitset
74 wxBitset
operator |(const wxBitset
&bitset2
) const // union
77 s
.m_data
= m_data
| bitset2
.m_data
;
81 // bitwise exclusive-or operator, returns a new bitset that has all bits set that are set either in
82 // bitset2 or in this bitset but not in both
83 wxBitset
operator ^(const wxBitset
&bitset2
) const // difference
86 s
.m_data
= m_data
^ bitset2
.m_data
;
90 // bitwise and operator, returns a new bitset that has all bits set that are set both in
91 // bitset2 and in this bitset
92 wxBitset
operator &(const wxBitset
&bitset2
) const // intersection
95 s
.m_data
= m_data
& bitset2
.m_data
;
99 // sets appropriate the bit to true
100 wxBitset
& set(const T el
) //Add element
106 // clears the appropriate flag to false
107 wxBitset
& reset(const T el
) //remove element
109 m_data
&= ~(1 << el
);
120 // true if this flag is set
121 bool test(const T el
) const
123 return (m_data
& (1 << el
)) ? true : false;
126 // true if no flag is set
132 // true if any flag is set
138 // true if both have the same flags
139 bool operator ==(const wxBitset
&rhs
) const
141 return m_data
== rhs
.m_data
;
144 // true if both differ in their flags set
145 bool operator !=(const wxBitset
&rhs
) const
147 return !operator==(rhs
);
150 bool operator[] (const T el
) const { return test(el
) ; }
153 unsigned long m_data
;
156 #if wxUSE_EXTENDED_RTTI
158 #define wxDEFINE_FLAGS( flags ) \
159 class WXDLLIMPEXP_BASE flags \
162 flags(long data=0) :m_data(data) {} \
164 bool operator ==(const flags &rhs) const { return m_data == rhs.m_data; }\
169 #define wxDEFINE_FLAGS( flags )
173 #if WXWIN_COMPATIBILITY_2_8
174 #define WX_DEFINE_FLAGS wxDEFINE_FLAGS