]>
Commit | Line | Data |
---|---|---|
ae820c69 SC |
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 | ||
0886e254 | 19 | // wxBitset should be applied to an enum, then this can be used like |
ae820c69 SC |
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 | |
9c8046dd | 23 | // The api is made as close as possible to <bitset> |
ae820c69 | 24 | |
0886e254 | 25 | template <class T> class wxBitset |
ae820c69 SC |
26 | { |
27 | friend class wxEnumData ; | |
28 | public: | |
0886e254 SC |
29 | // creates a wxBitset<> object with all flags initialized to 0 |
30 | wxBitset() { m_data = 0; } | |
9c8046dd | 31 | |
0886e254 | 32 | // created a wxBitset<> object initialized according to the bits of the |
9c8046dd | 33 | // integral value val |
0886e254 | 34 | wxBitset(unsigned long val) { m_data = val ; } |
9c8046dd | 35 | |
0886e254 SC |
36 | // copies the content in the new wxBitset<> object from another one |
37 | wxBitset(const wxBitset &src) { m_data = src.m_data; } | |
9c8046dd | 38 | |
0886e254 SC |
39 | // creates a wxBitset<> object that has the specific flag set |
40 | wxBitset(const T el) { m_data |= 1 << el; } | |
ae820c69 | 41 | |
9c8046dd SC |
42 | // returns the integral value that the bits of this object represent |
43 | unsigned long to_ulong() const { return m_data ; } | |
ae820c69 | 44 | |
9c8046dd | 45 | // assignment |
0886e254 | 46 | wxBitset &operator =(const wxBitset &rhs) |
ae820c69 SC |
47 | { |
48 | m_data = rhs.m_data; | |
49 | return *this; | |
50 | } | |
9c8046dd SC |
51 | |
52 | // bitwise or operator, sets all bits that are in rhs and leaves | |
53 | // the rest unchanged | |
0886e254 | 54 | wxBitset &operator |=(const wxBitset &rhs) |
ae820c69 SC |
55 | { |
56 | m_data |= rhs.m_data; | |
57 | return *this; | |
58 | } | |
9c8046dd SC |
59 | |
60 | // bitwsie exclusive-or operator, toggles the value of all bits | |
61 | // that are set in bits and leaves all others unchanged | |
0886e254 | 62 | wxBitset &operator ^=(const wxBitset &rhs) // difference |
ae820c69 SC |
63 | { |
64 | m_data ^= rhs.m_data; | |
65 | return *this; | |
66 | } | |
67 | ||
9c8046dd SC |
68 | // bitwise and operator, resets all bits that are not in rhs and leaves |
69 | // all others unchanged | |
0886e254 | 70 | wxBitset &operator &=(const wxBitset &rhs) // intersection |
ae820c69 SC |
71 | { |
72 | m_data &= rhs.m_data; | |
73 | return *this; | |
74 | } | |
75 | ||
9c8046dd SC |
76 | // bitwise or operator, returns a new bitset that has all bits set that set are in |
77 | // bitset2 or in this bitset | |
0886e254 | 78 | wxBitset operator |(const wxBitset &bitset2) const // union |
ae820c69 | 79 | { |
0886e254 | 80 | wxBitset<T> s; |
9c8046dd | 81 | s.m_data = m_data | bitset2.m_data; |
ae820c69 SC |
82 | return s; |
83 | } | |
9c8046dd SC |
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 | |
0886e254 | 87 | wxBitset operator ^(const wxBitset &bitset2) const // difference |
ae820c69 | 88 | { |
0886e254 | 89 | wxBitset<T> s; |
9c8046dd | 90 | s.m_data = m_data ^ bitset2.m_data; |
ae820c69 SC |
91 | return s; |
92 | } | |
9c8046dd SC |
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 | |
0886e254 | 96 | wxBitset operator &(const wxBitset &bitset2) const // intersection |
ae820c69 | 97 | { |
0886e254 | 98 | wxBitset<T> s; |
9c8046dd | 99 | s.m_data = m_data & bitset2.m_data; |
ae820c69 SC |
100 | return s; |
101 | } | |
102 | ||
9c8046dd | 103 | // sets appropriate the bit to true |
0886e254 | 104 | wxBitset& set(const T el) //Add element |
ae820c69 SC |
105 | { |
106 | m_data |= 1 << el; | |
107 | return *this; | |
108 | } | |
9c8046dd SC |
109 | |
110 | // clears the appropriate flag to false | |
0886e254 | 111 | wxBitset& reset(const T el) //remove element |
ae820c69 SC |
112 | { |
113 | m_data &= ~(1 << el); | |
114 | return *this; | |
115 | } | |
116 | ||
9c8046dd | 117 | // clear all flags |
0886e254 | 118 | wxBitset& reset() |
ae820c69 | 119 | { |
9c8046dd SC |
120 | m_data = 0; |
121 | return *this; | |
ae820c69 SC |
122 | } |
123 | ||
9c8046dd SC |
124 | // true if this flag is set |
125 | bool test(const T el) const | |
ae820c69 | 126 | { |
9c8046dd | 127 | return (m_data & (1 << el)) ? true : false; |
ae820c69 SC |
128 | } |
129 | ||
9c8046dd SC |
130 | // true if no flag is set |
131 | bool none() const | |
ae820c69 SC |
132 | { |
133 | return m_data == 0; | |
134 | } | |
135 | ||
9c8046dd SC |
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 | |
0886e254 | 143 | bool operator ==(const wxBitset &rhs) const |
ae820c69 SC |
144 | { |
145 | return m_data == rhs.m_data; | |
146 | } | |
147 | ||
9c8046dd | 148 | // true if both differ in their flags set |
0886e254 | 149 | bool operator !=(const wxBitset &rhs) const |
ae820c69 SC |
150 | { |
151 | return !operator==(rhs); | |
152 | } | |
9c8046dd SC |
153 | |
154 | bool operator[] (const T el) const { return test(el) ; } | |
155 | ||
ae820c69 | 156 | private : |
9c8046dd | 157 | unsigned long m_data; |
ae820c69 SC |
158 | }; |
159 | ||
0886e254 SC |
160 | #define WX_DEFINE_FLAGS( flags ) \ |
161 | struct flags \ | |
162 | {\ | |
163 | flags(long data=0) :m_data(data) {} \ | |
164 | long m_data ;\ | |
165 | } ; | |
ae820c69 SC |
166 | |
167 | #endif |