]>
Commit | Line | Data |
---|---|---|
ae820c69 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/flags.h | |
a62848fd | 3 | // Purpose: a bitset suited for replacing the current style flags |
ae820c69 | 4 | // Author: Stefan Csomor |
a62848fd | 5 | // Modified by: |
ae820c69 | 6 | // Created: 27/07/03 |
ae820c69 | 7 | // Copyright: (c) 2003 Stefan Csomor |
65571936 | 8 | // Licence: wxWindows licence |
ae820c69 SC |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | #ifndef _WX_SETH__ | |
12 | #define _WX_SETH__ | |
13 | ||
0886e254 | 14 | // wxBitset should be applied to an enum, then this can be used like |
ae820c69 SC |
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 | |
a62848fd | 18 | // The api is made as close as possible to <bitset> |
ae820c69 | 19 | |
0886e254 | 20 | template <class T> class wxBitset |
ae820c69 | 21 | { |
a62848fd | 22 | friend class wxEnumData ; |
ae820c69 | 23 | public: |
0886e254 SC |
24 | // creates a wxBitset<> object with all flags initialized to 0 |
25 | wxBitset() { m_data = 0; } | |
9c8046dd | 26 | |
a62848fd | 27 | // created a wxBitset<> object initialized according to the bits of the |
9c8046dd | 28 | // integral value val |
0886e254 | 29 | wxBitset(unsigned long val) { m_data = val ; } |
9c8046dd | 30 | |
0886e254 SC |
31 | // copies the content in the new wxBitset<> object from another one |
32 | wxBitset(const wxBitset &src) { m_data = src.m_data; } | |
9c8046dd | 33 | |
0886e254 SC |
34 | // creates a wxBitset<> object that has the specific flag set |
35 | wxBitset(const T el) { m_data |= 1 << el; } | |
ae820c69 | 36 | |
9c8046dd SC |
37 | // returns the integral value that the bits of this object represent |
38 | unsigned long to_ulong() const { return m_data ; } | |
ae820c69 | 39 | |
9c8046dd | 40 | // assignment |
0886e254 | 41 | wxBitset &operator =(const wxBitset &rhs) |
ae820c69 | 42 | { |
a62848fd WS |
43 | m_data = rhs.m_data; |
44 | return *this; | |
ae820c69 | 45 | } |
9c8046dd SC |
46 | |
47 | // bitwise or operator, sets all bits that are in rhs and leaves | |
48 | // the rest unchanged | |
a62848fd | 49 | wxBitset &operator |=(const wxBitset &rhs) |
ae820c69 | 50 | { |
a62848fd WS |
51 | m_data |= rhs.m_data; |
52 | return *this; | |
ae820c69 | 53 | } |
9c8046dd SC |
54 | |
55 | // bitwsie exclusive-or operator, toggles the value of all bits | |
56 | // that are set in bits and leaves all others unchanged | |
0886e254 | 57 | wxBitset &operator ^=(const wxBitset &rhs) // difference |
ae820c69 | 58 | { |
a62848fd WS |
59 | m_data ^= rhs.m_data; |
60 | return *this; | |
ae820c69 SC |
61 | } |
62 | ||
9c8046dd SC |
63 | // bitwise and operator, resets all bits that are not in rhs and leaves |
64 | // all others unchanged | |
0886e254 | 65 | wxBitset &operator &=(const wxBitset &rhs) // intersection |
ae820c69 | 66 | { |
a62848fd WS |
67 | m_data &= rhs.m_data; |
68 | return *this; | |
ae820c69 SC |
69 | } |
70 | ||
a62848fd | 71 | // bitwise or operator, returns a new bitset that has all bits set that set are in |
9c8046dd | 72 | // bitset2 or in this bitset |
a62848fd | 73 | wxBitset operator |(const wxBitset &bitset2) const // union |
ae820c69 | 74 | { |
a62848fd WS |
75 | wxBitset<T> s; |
76 | s.m_data = m_data | bitset2.m_data; | |
77 | return s; | |
ae820c69 | 78 | } |
9c8046dd | 79 | |
a62848fd | 80 | // bitwise exclusive-or operator, returns a new bitset that has all bits set that are set either in |
9c8046dd | 81 | // bitset2 or in this bitset but not in both |
0886e254 | 82 | wxBitset operator ^(const wxBitset &bitset2) const // difference |
ae820c69 | 83 | { |
a62848fd WS |
84 | wxBitset<T> s; |
85 | s.m_data = m_data ^ bitset2.m_data; | |
86 | return s; | |
ae820c69 | 87 | } |
9c8046dd | 88 | |
a62848fd | 89 | // bitwise and operator, returns a new bitset that has all bits set that are set both in |
9c8046dd | 90 | // bitset2 and in this bitset |
0886e254 | 91 | wxBitset operator &(const wxBitset &bitset2) const // intersection |
ae820c69 | 92 | { |
a62848fd WS |
93 | wxBitset<T> s; |
94 | s.m_data = m_data & bitset2.m_data; | |
95 | return s; | |
ae820c69 SC |
96 | } |
97 | ||
9c8046dd | 98 | // sets appropriate the bit to true |
0886e254 | 99 | wxBitset& set(const T el) //Add element |
ae820c69 | 100 | { |
a62848fd WS |
101 | m_data |= 1 << el; |
102 | return *this; | |
ae820c69 | 103 | } |
a62848fd | 104 | |
9c8046dd | 105 | // clears the appropriate flag to false |
0886e254 | 106 | wxBitset& reset(const T el) //remove element |
ae820c69 | 107 | { |
a62848fd WS |
108 | m_data &= ~(1 << el); |
109 | return *this; | |
ae820c69 SC |
110 | } |
111 | ||
9c8046dd | 112 | // clear all flags |
0886e254 | 113 | wxBitset& reset() |
ae820c69 | 114 | { |
a62848fd WS |
115 | m_data = 0; |
116 | return *this; | |
ae820c69 SC |
117 | } |
118 | ||
9c8046dd SC |
119 | // true if this flag is set |
120 | bool test(const T el) const | |
ae820c69 | 121 | { |
a62848fd | 122 | return (m_data & (1 << el)) ? true : false; |
ae820c69 SC |
123 | } |
124 | ||
9c8046dd SC |
125 | // true if no flag is set |
126 | bool none() const | |
ae820c69 | 127 | { |
a62848fd | 128 | return m_data == 0; |
ae820c69 SC |
129 | } |
130 | ||
9c8046dd SC |
131 | // true if any flag is set |
132 | bool any() const | |
133 | { | |
a62848fd | 134 | return m_data != 0; |
9c8046dd SC |
135 | } |
136 | ||
137 | // true if both have the same flags | |
0886e254 | 138 | bool operator ==(const wxBitset &rhs) const |
ae820c69 | 139 | { |
a62848fd | 140 | return m_data == rhs.m_data; |
ae820c69 SC |
141 | } |
142 | ||
9c8046dd | 143 | // true if both differ in their flags set |
0886e254 | 144 | bool operator !=(const wxBitset &rhs) const |
ae820c69 | 145 | { |
a62848fd | 146 | return !operator==(rhs); |
ae820c69 | 147 | } |
9c8046dd SC |
148 | |
149 | bool operator[] (const T el) const { return test(el) ; } | |
150 | ||
ae820c69 | 151 | private : |
a62848fd | 152 | unsigned long m_data; |
ae820c69 SC |
153 | }; |
154 | ||
28953245 SC |
155 | #if wxUSE_EXTENDED_RTTI |
156 | ||
157 | #define wxDEFINE_FLAGS( flags ) \ | |
163b3ad7 | 158 | class WXDLLIMPEXP_BASE flags \ |
0886e254 | 159 | {\ |
583150e3 SC |
160 | public : \ |
161 | flags(long data=0) :m_data(data) {} \ | |
0886e254 | 162 | long m_data ;\ |
583150e3 | 163 | bool operator ==(const flags &rhs) const { return m_data == rhs.m_data; }\ |
0886e254 | 164 | } ; |
ae820c69 | 165 | |
28953245 SC |
166 | #else |
167 | ||
168 | #define wxDEFINE_FLAGS( flags ) | |
169 | ||
170 | #endif | |
171 | ||
172 | #if WXWIN_COMPATIBILITY_2_8 | |
173 | #define WX_DEFINE_FLAGS wxDEFINE_FLAGS | |
174 | #endif | |
175 | ||
ae820c69 | 176 | #endif |