]> git.saurik.com Git - wxWidgets.git/blob - include/wx/flags.h
fixed bundles dependency
[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 #include <bitset>
20
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>
26
27 template <class T> class wxFlags
28 {
29 friend class wxEnumData ;
30 public:
31 // creates a wxFlags<> object with all flags initialized to 0
32 wxFlags() { m_data = 0; }
33
34 // created a wxFlags<> object initialized according to the bits of the
35 // integral value val
36 wxFlags(unsigned long val) { m_data = val ; }
37
38 // copies the content in the new wxFlags<> object from another one
39 wxFlags(const wxFlags &src) { m_data = src.m_data; }
40
41 // creates a wxFlags<> object that has the specific flag set
42 wxFlags(const T el) { m_data |= 1 << el; }
43
44 // returns the integral value that the bits of this object represent
45 unsigned long to_ulong() const { return m_data ; }
46
47 // assignment
48 wxFlags &operator =(const wxFlags &rhs)
49 {
50 m_data = rhs.m_data;
51 return *this;
52 }
53
54 // bitwise or operator, sets all bits that are in rhs and leaves
55 // the rest unchanged
56 wxFlags &operator |=(const wxFlags &rhs)
57 {
58 m_data |= rhs.m_data;
59 return *this;
60 }
61
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
65 {
66 m_data ^= rhs.m_data;
67 return *this;
68 }
69
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
73 {
74 m_data &= rhs.m_data;
75 return *this;
76 }
77
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
81 {
82 wxFlags<T> s;
83 s.m_data = m_data | bitset2.m_data;
84 return s;
85 }
86
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
90 {
91 wxFlags<T> s;
92 s.m_data = m_data ^ bitset2.m_data;
93 return s;
94 }
95
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
99 {
100 wxFlags<T> s;
101 s.m_data = m_data & bitset2.m_data;
102 return s;
103 }
104
105 // sets appropriate the bit to true
106 wxFlags& set(const T el) //Add element
107 {
108 m_data |= 1 << el;
109 return *this;
110 }
111
112 // clears the appropriate flag to false
113 wxFlags& reset(const T el) //remove element
114 {
115 m_data &= ~(1 << el);
116 return *this;
117 }
118
119 // clear all flags
120 wxFlags& reset()
121 {
122 m_data = 0;
123 return *this;
124 }
125
126 // true if this flag is set
127 bool test(const T el) const
128 {
129 return (m_data & (1 << el)) ? true : false;
130 }
131
132 // true if no flag is set
133 bool none() const
134 {
135 return m_data == 0;
136 }
137
138 // true if any flag is set
139 bool any() const
140 {
141 return m_data != 0;
142 }
143
144 // true if both have the same flags
145 bool operator ==(const wxFlags &rhs) const
146 {
147 return m_data == rhs.m_data;
148 }
149
150 // true if both differ in their flags set
151 bool operator !=(const wxFlags &rhs) const
152 {
153 return !operator==(rhs);
154 }
155
156 bool operator[] (const T el) const { return test(el) ; }
157
158 private :
159 unsigned long m_data;
160 };
161
162
163 #endif