]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/flags.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: a bitset suited for replacing the current style flags
4 // Author: Stefan Csomor
7 // Copyright: (c) 2003 Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
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>
20 template <class T
> class wxBitset
22 friend class wxEnumData
;
24 // creates a wxBitset<> object with all flags initialized to 0
25 wxBitset() { m_data
= 0; }
27 // created a wxBitset<> object initialized according to the bits of the
29 wxBitset(unsigned long val
) { m_data
= val
; }
31 // copies the content in the new wxBitset<> object from another one
32 wxBitset(const wxBitset
&src
) { m_data
= src
.m_data
; }
34 // creates a wxBitset<> object that has the specific flag set
35 wxBitset(const T el
) { m_data
|= 1 << el
; }
37 // returns the integral value that the bits of this object represent
38 unsigned long to_ulong() const { return m_data
; }
41 wxBitset
&operator =(const wxBitset
&rhs
)
47 // bitwise or operator, sets all bits that are in rhs and leaves
49 wxBitset
&operator |=(const wxBitset
&rhs
)
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
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
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
76 s
.m_data
= m_data
| bitset2
.m_data
;
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
85 s
.m_data
= m_data
^ bitset2
.m_data
;
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
94 s
.m_data
= m_data
& bitset2
.m_data
;
98 // sets appropriate the bit to true
99 wxBitset
& set(const T el
) //Add element
105 // clears the appropriate flag to false
106 wxBitset
& reset(const T el
) //remove element
108 m_data
&= ~(1 << el
);
119 // true if this flag is set
120 bool test(const T el
) const
122 return (m_data
& (1 << el
)) ? true : false;
125 // true if no flag is set
131 // true if any flag is set
137 // true if both have the same flags
138 bool operator ==(const wxBitset
&rhs
) const
140 return m_data
== rhs
.m_data
;
143 // true if both differ in their flags set
144 bool operator !=(const wxBitset
&rhs
) const
146 return !operator==(rhs
);
149 bool operator[] (const T el
) const { return test(el
) ; }
152 unsigned long m_data
;
155 #if wxUSE_EXTENDED_RTTI
157 #define wxDEFINE_FLAGS( flags ) \
158 class WXDLLIMPEXP_BASE flags \
161 flags(long data=0) :m_data(data) {} \
163 bool operator ==(const flags &rhs) const { return m_data == rhs.m_data; }\
168 #define wxDEFINE_FLAGS( flags )
172 #if WXWIN_COMPATIBILITY_2_8
173 #define WX_DEFINE_FLAGS wxDEFINE_FLAGS