|
1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #ifndef __BABITFLAGS_H__ |
|
17 #define __BABITFLAGS_H__ |
|
18 |
|
19 // System includes |
|
20 #include <e32std.h> |
|
21 |
|
22 // |
|
23 // ----> TBitFlagsT (header) |
|
24 // |
|
25 template <class T> |
|
26 class TBitFlagsT |
|
27 /** |
|
28 A simple class which manages the process of setting and clearing |
|
29 flags in an abstract fashion. |
|
30 @publishedAll |
|
31 @released |
|
32 */ |
|
33 { |
|
34 // |
|
35 public: // CONSTRUCT |
|
36 // |
|
37 |
|
38 /** |
|
39 * Default constructor - initialize all flags to zero |
|
40 */ |
|
41 inline TBitFlagsT(); |
|
42 |
|
43 /** |
|
44 * Initialize the whole flag to a certain value |
|
45 */ |
|
46 inline TBitFlagsT(T aFlags); |
|
47 |
|
48 /** |
|
49 * Copy constructor - initialize this flag object to mirror |
|
50 * that of aFlags. |
|
51 */ |
|
52 inline TBitFlagsT(const TBitFlagsT& aFlags); |
|
53 |
|
54 // |
|
55 public: // MANIPULATORS |
|
56 // |
|
57 |
|
58 /** |
|
59 * Set all the flags |
|
60 */ |
|
61 inline void SetAll(); |
|
62 |
|
63 /** |
|
64 * Clear all the flags |
|
65 */ |
|
66 inline void ClearAll(); |
|
67 |
|
68 /** |
|
69 * Set a particular flag |
|
70 */ |
|
71 inline void Set(TInt aFlagIndex); |
|
72 |
|
73 /** |
|
74 * Clear a particular flag |
|
75 */ |
|
76 inline void Clear(TInt aFlagIndex); |
|
77 |
|
78 /** |
|
79 * Set or clear a particular flag |
|
80 * |
|
81 * If aValue is 1, then the flag is set |
|
82 * If aValue is 0, then the flag is cleared |
|
83 */ |
|
84 inline void Assign(TInt aFlagIndex, TBool aValue); |
|
85 |
|
86 /** |
|
87 * Change the state of a particular flag. If the flag at the specified |
|
88 * index was clear, then it becomes set, otherwise it becomes clear. |
|
89 */ |
|
90 inline void Toggle(TInt aFlagIndex); |
|
91 |
|
92 // |
|
93 public: // OPERATORS |
|
94 // |
|
95 |
|
96 /** |
|
97 * Check if a particular flag is set or not |
|
98 * |
|
99 * @return A boolean indicating whether the specified flag is set or clear |
|
100 */ |
|
101 inline TBool operator[](TInt aFlagIndex) const; |
|
102 |
|
103 /** |
|
104 * Assignment operator - assign specific value to the whole flag, replacing |
|
105 * any existing value. |
|
106 */ |
|
107 inline TBitFlagsT& operator=(const TBitFlagsT& aFlags); |
|
108 |
|
109 /** |
|
110 * Compare the value of the whole flag with a given value. |
|
111 * |
|
112 * @return A boolean indicating whether the two flags are identical. |
|
113 */ |
|
114 inline TBool operator==(const TBitFlagsT& aFlags); |
|
115 |
|
116 // |
|
117 public: // ACCESS |
|
118 // |
|
119 |
|
120 /** |
|
121 * Check if a particular flag is set |
|
122 */ |
|
123 inline TBool IsSet(TInt aFlagIndex) const; |
|
124 |
|
125 /** |
|
126 * Check if a particular flag is clear |
|
127 */ |
|
128 inline TBool IsClear(TInt aFlagIndex) const; |
|
129 |
|
130 /** |
|
131 * Access the underlying value of the flag. |
|
132 */ |
|
133 inline T Value() const { return iFlags; } |
|
134 |
|
135 /** |
|
136 * Assign a new value (directly) to this flag object. Replaces any |
|
137 * existing individual flag settings. |
|
138 */ |
|
139 inline void SetValue(T aFlags) { iFlags = aFlags; } |
|
140 |
|
141 // |
|
142 private: // INTERNAL |
|
143 // |
|
144 |
|
145 /** |
|
146 * Generate a mask for a particular flag |
|
147 */ |
|
148 inline T FlagMask(TInt aFlagIndex) const; |
|
149 |
|
150 // |
|
151 public: // MEMBER DATA |
|
152 // |
|
153 |
|
154 // The underlying object container which represents the flags. |
|
155 T iFlags; |
|
156 }; |
|
157 |
|
158 /** |
|
159 Type definitions |
|
160 @publishedAll |
|
161 @released |
|
162 */ |
|
163 typedef TBitFlagsT<TUint8> TBitFlags8; |
|
164 // |
|
165 |
|
166 /** |
|
167 Type definitions |
|
168 @publishedAll |
|
169 @released |
|
170 */ |
|
171 typedef TBitFlagsT<TUint16> TBitFlags16; |
|
172 // |
|
173 |
|
174 /** |
|
175 Type definitions |
|
176 @publishedAll |
|
177 @released |
|
178 */ |
|
179 typedef TBitFlagsT<TUint32> TBitFlags32; |
|
180 // |
|
181 |
|
182 |
|
183 typedef TBitFlags32 TBitFlags; |
|
184 |
|
185 |
|
186 // |
|
187 // ----> TBitFlagsT (inlines) |
|
188 // |
|
189 template <class T> |
|
190 inline TBitFlagsT<T>::TBitFlagsT() : iFlags(T(0)) |
|
191 {} |
|
192 |
|
193 template <class T> |
|
194 inline TBitFlagsT<T>::TBitFlagsT(T aFlags) : iFlags(aFlags) |
|
195 {} |
|
196 |
|
197 template <class T> |
|
198 inline TBitFlagsT<T>::TBitFlagsT(const TBitFlagsT<T>& aFlags) : iFlags(aFlags.iFlags) |
|
199 {} |
|
200 |
|
201 template <class T> |
|
202 inline T TBitFlagsT<T>::FlagMask(TInt aFlagIndex) const |
|
203 { return T(T(1)<<aFlagIndex); } |
|
204 |
|
205 template <class T> |
|
206 inline TBool TBitFlagsT<T>::IsSet(TInt aFlagIndex) const |
|
207 { |
|
208 // All out-of-range values should return false |
|
209 if(aFlagIndex <= ((sizeof(T)<<3)-1) ) |
|
210 { |
|
211 return iFlags & FlagMask(aFlagIndex); |
|
212 } |
|
213 else |
|
214 { |
|
215 return EFalse; |
|
216 } |
|
217 |
|
218 } |
|
219 |
|
220 template <class T> |
|
221 inline TBool TBitFlagsT<T>::IsClear(TInt aFlagIndex) const |
|
222 { return !IsSet(aFlagIndex); } |
|
223 |
|
224 template <class T> |
|
225 inline void TBitFlagsT<T>::Set(TInt aFlagIndex) |
|
226 { iFlags = T(iFlags | FlagMask(aFlagIndex)); } |
|
227 |
|
228 template <class T> |
|
229 inline void TBitFlagsT<T>::Clear(TInt aFlagIndex) |
|
230 { iFlags = T(iFlags & ~(FlagMask(aFlagIndex))); } |
|
231 |
|
232 template <class T> |
|
233 inline void TBitFlagsT<T>::Assign(TInt aFlagIndex, TBool aVal) |
|
234 { if (aVal) Set(aFlagIndex); else Clear(aFlagIndex); } |
|
235 |
|
236 template <class T> |
|
237 inline void TBitFlagsT<T>::Toggle(TInt aFlagIndex) |
|
238 { iFlags = T(iFlags^FlagMask(aFlagIndex)); } |
|
239 |
|
240 template <class T> |
|
241 inline TBool TBitFlagsT<T>::operator[](TInt aFlagIndex) const |
|
242 { return IsSet(aFlagIndex); } |
|
243 |
|
244 template <class T> |
|
245 inline TBitFlagsT<T>& TBitFlagsT<T>::operator=(const TBitFlagsT<T>& aFlags) |
|
246 { iFlags = aFlags.iFlags; return *this; } |
|
247 |
|
248 template <class T> |
|
249 inline TBool TBitFlagsT<T>::operator==(const TBitFlagsT<T>& aFlags) |
|
250 { return iFlags == aFlags.Value(); } |
|
251 |
|
252 template <class T> |
|
253 inline void TBitFlagsT<T>::SetAll() |
|
254 { iFlags = T(~(T(0))); } |
|
255 |
|
256 template <class T> |
|
257 inline void TBitFlagsT<T>::ClearAll() |
|
258 { iFlags = T(0); } |
|
259 |
|
260 |
|
261 #endif |