]> git.saurik.com Git - wxWidgets.git/blob - include/wx/flags.h
Fix for the fix for wxChoice selection.
[wxWidgets.git] / include / wx / flags.h
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 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_SETH__
13 #define _WX_SETH__
14
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "flags.h"
17 #endif
18
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>
24
25 template <class T> class wxBitset
26 {
27 friend class wxEnumData ;
28 public:
29 // creates a wxBitset<> object with all flags initialized to 0
30 wxBitset() { m_data = 0; }
31
32 // created a wxBitset<> object initialized according to the bits of the
33 // integral value val
34 wxBitset(unsigned long val) { m_data = val ; }
35
36 // copies the content in the new wxBitset<> object from another one
37 wxBitset(const wxBitset &src) { m_data = src.m_data; }
38
39 // creates a wxBitset<> object that has the specific flag set
40 wxBitset(const T el) { m_data |= 1 << el; }
41
42 // returns the integral value that the bits of this object represent
43 unsigned long to_ulong() const { return m_data ; }
44
45 // assignment
46 wxBitset &operator =(const wxBitset &rhs)
47 {
48 m_data = rhs.m_data;
49 return *this;
50 }
51
52 // bitwise or operator, sets all bits that are in rhs and leaves
53 // the rest unchanged
54 wxBitset &operator |=(const wxBitset &rhs)
55 {
56 m_data |= rhs.m_data;
57 return *this;
58 }
59
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
63 {
64 m_data ^= rhs.m_data;
65 return *this;
66 }
67
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
71 {
72 m_data &= rhs.m_data;
73 return *this;
74 }
75
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
79 {
80 wxBitset<T> s;
81 s.m_data = m_data | bitset2.m_data;
82 return s;
83 }
84
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
88 {
89 wxBitset<T> s;
90 s.m_data = m_data ^ bitset2.m_data;
91 return s;
92 }
93
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
97 {
98 wxBitset<T> s;
99 s.m_data = m_data & bitset2.m_data;
100 return s;
101 }
102
103 // sets appropriate the bit to true
104 wxBitset& set(const T el) //Add element
105 {
106 m_data |= 1 << el;
107 return *this;
108 }
109
110 // clears the appropriate flag to false
111 wxBitset& reset(const T el) //remove element
112 {
113 m_data &= ~(1 << el);
114 return *this;
115 }
116
117 // clear all flags
118 wxBitset& reset()
119 {
120 m_data = 0;
121 return *this;
122 }
123
124 // true if this flag is set
125 bool test(const T el) const
126 {
127 return (m_data & (1 << el)) ? true : false;
128 }
129
130 // true if no flag is set
131 bool none() const
132 {
133 return m_data == 0;
134 }
135
136 // true if any flag is set
137 bool any() const
138 {
139 return m_data != 0;
140 }
141
142 // true if both have the same flags
143 bool operator ==(const wxBitset &rhs) const
144 {
145 return m_data == rhs.m_data;
146 }
147
148 // true if both differ in their flags set
149 bool operator !=(const wxBitset &rhs) const
150 {
151 return !operator==(rhs);
152 }
153
154 bool operator[] (const T el) const { return test(el) ; }
155
156 private :
157 unsigned long m_data;
158 };
159
160 #define WX_DEFINE_FLAGS( flags ) \
161 class WXDLLEXPORT flags \
162 {\
163 public : \
164 flags(long data=0) :m_data(data) {} \
165 long m_data ;\
166 bool operator ==(const flags &rhs) const { return m_data == rhs.m_data; }\
167 } ;
168
169 #endif