]>
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
8 // Copyright: (c) 2003 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "flags.h"
21 // wxFlags should be applied to an enum, then this can be used like
22 // bitwise operators but keeps the type safety and information, the
23 // enums must be in a sequence , their value determines the bit position
24 // that they represent
25 // The api is made as close as possible to <bitset>
27 template <class T
> class wxFlags
29 friend class wxEnumData
;
31 // creates a wxFlags<> object with all flags initialized to 0
32 wxFlags() { m_data
= 0; }
34 // created a wxFlags<> object initialized according to the bits of the
36 wxFlags(unsigned long val
) { m_data
= val
; }
38 // copies the content in the new wxFlags<> object from another one
39 wxFlags(const wxFlags
&src
) { m_data
= src
.m_data
; }
41 // creates a wxFlags<> object that has the specific flag set
42 wxFlags(const T el
) { m_data
|= 1 << el
; }
44 // returns the integral value that the bits of this object represent
45 unsigned long to_ulong() const { return m_data
; }
48 wxFlags
&operator =(const wxFlags
&rhs
)
54 // bitwise or operator, sets all bits that are in rhs and leaves
56 wxFlags
&operator |=(const wxFlags
&rhs
)
62 // bitwsie exclusive-or operator, toggles the value of all bits
63 // that are set in bits and leaves all others unchanged
64 wxFlags
&operator ^=(const wxFlags
&rhs
) // difference
70 // bitwise and operator, resets all bits that are not in rhs and leaves
71 // all others unchanged
72 wxFlags
&operator &=(const wxFlags
&rhs
) // intersection
78 // bitwise or operator, returns a new bitset that has all bits set that set are in
79 // bitset2 or in this bitset
80 wxFlags
operator |(const wxFlags
&bitset2
) const // union
83 s
.m_data
= m_data
| bitset2
.m_data
;
87 // bitwise exclusive-or operator, returns a new bitset that has all bits set that are set either in
88 // bitset2 or in this bitset but not in both
89 wxFlags
operator ^(const wxFlags
&bitset2
) const // difference
92 s
.m_data
= m_data
^ bitset2
.m_data
;
96 // bitwise and operator, returns a new bitset that has all bits set that are set both in
97 // bitset2 and in this bitset
98 wxFlags
operator &(const wxFlags
&bitset2
) const // intersection
101 s
.m_data
= m_data
& bitset2
.m_data
;
105 // sets appropriate the bit to true
106 wxFlags
& set(const T el
) //Add element
112 // clears the appropriate flag to false
113 wxFlags
& reset(const T el
) //remove element
115 m_data
&= ~(1 << el
);
126 // true if this flag is set
127 bool test(const T el
) const
129 return (m_data
& (1 << el
)) ? true : false;
132 // true if no flag is set
138 // true if any flag is set
144 // true if both have the same flags
145 bool operator ==(const wxFlags
&rhs
) const
147 return m_data
== rhs
.m_data
;
150 // true if both differ in their flags set
151 bool operator !=(const wxFlags
&rhs
) const
153 return !operator==(rhs
);
156 bool operator[] (const T el
) const { return test(el
) ; }
159 unsigned long m_data
;