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