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 the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #ifndef __BABITFLAGS_H__
17 #define __BABITFLAGS_H__
22 ///////////////////////////////////////////////////////////////////////////////////////
23 // ----> TBitFlagsT (header)
24 ///////////////////////////////////////////////////////////////////////////////////////
28 A simple class which manages the process of setting and clearing
29 flags in an abstract fashion.
34 ///////////////////////////////////////////////////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////////////////////
39 * Default constructor - initialize all flags to zero
44 * Initialize the whole flag to a certain value
46 inline TBitFlagsT(T aFlags);
49 * Copy constructor - initialize this flag object to mirror
52 inline TBitFlagsT(const TBitFlagsT& aFlags);
54 ///////////////////////////////////////////////////////////////////////////////////////
55 public: // MANIPULATORS
56 ///////////////////////////////////////////////////////////////////////////////////////
66 inline void ClearAll();
69 * Set a particular flag
71 inline void Set(TInt aFlagIndex);
74 * Clear a particular flag
76 inline void Clear(TInt aFlagIndex);
79 * Set or clear a particular flag
81 * If aValue is 1, then the flag is set
82 * If aValue is 0, then the flag is cleared
84 inline void Assign(TInt aFlagIndex, TBool aValue);
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.
90 inline void Toggle(TInt aFlagIndex);
92 ///////////////////////////////////////////////////////////////////////////////////////
94 ///////////////////////////////////////////////////////////////////////////////////////
97 * Check if a particular flag is set or not
99 * @return A boolean indicating whether the specified flag is set or clear
101 inline TBool operator[](TInt aFlagIndex) const;
104 * Assignment operator - assign specific value to the whole flag, replacing
105 * any existing value.
107 inline TBitFlagsT& operator=(const TBitFlagsT& aFlags);
110 * Compare the value of the whole flag with a given value.
112 * @return A boolean indicating whether the two flags are identical.
114 inline TBool operator==(const TBitFlagsT& aFlags);
116 ///////////////////////////////////////////////////////////////////////////////////////
118 ///////////////////////////////////////////////////////////////////////////////////////
121 * Check if a particular flag is set
123 inline TBool IsSet(TInt aFlagIndex) const;
126 * Check if a particular flag is clear
128 inline TBool IsClear(TInt aFlagIndex) const;
131 * Access the underlying value of the flag.
133 inline T Value() const { return iFlags; }
136 * Assign a new value (directly) to this flag object. Replaces any
137 * existing individual flag settings.
139 inline void SetValue(T aFlags) { iFlags = aFlags; }
141 ///////////////////////////////////////////////////////////////////////////////////////
143 ///////////////////////////////////////////////////////////////////////////////////////
146 * Generate a mask for a particular flag
148 inline T FlagMask(TInt aFlagIndex) const;
150 ///////////////////////////////////////////////////////////////////////////////////////
151 public: // MEMBER DATA
152 ///////////////////////////////////////////////////////////////////////////////////////
154 // The underlying object container which represents the flags.
163 typedef TBitFlagsT<TUint8> TBitFlags8;
171 typedef TBitFlagsT<TUint16> TBitFlags16;
179 typedef TBitFlagsT<TUint32> TBitFlags32;
183 typedef TBitFlags32 TBitFlags;
186 ///////////////////////////////////////////////////////////////////////////////////////
187 // ----> TBitFlagsT (inlines)
188 ///////////////////////////////////////////////////////////////////////////////////////
190 inline TBitFlagsT<T>::TBitFlagsT() : iFlags(T(0))
194 inline TBitFlagsT<T>::TBitFlagsT(T aFlags) : iFlags(aFlags)
198 inline TBitFlagsT<T>::TBitFlagsT(const TBitFlagsT<T>& aFlags) : iFlags(aFlags.iFlags)
202 inline T TBitFlagsT<T>::FlagMask(TInt aFlagIndex) const
203 { return T(T(1)<<aFlagIndex); }
206 inline TBool TBitFlagsT<T>::IsSet(TInt aFlagIndex) const
207 { return iFlags & FlagMask(aFlagIndex); }
210 inline TBool TBitFlagsT<T>::IsClear(TInt aFlagIndex) const
211 { return !IsSet(aFlagIndex); }
214 inline void TBitFlagsT<T>::Set(TInt aFlagIndex)
215 { iFlags = T(iFlags | FlagMask(aFlagIndex)); }
218 inline void TBitFlagsT<T>::Clear(TInt aFlagIndex)
219 { iFlags = T(iFlags & ~(FlagMask(aFlagIndex))); }
222 inline void TBitFlagsT<T>::Assign(TInt aFlagIndex, TBool aVal)
223 { if (aVal) Set(aFlagIndex); else Clear(aFlagIndex); }
226 inline void TBitFlagsT<T>::Toggle(TInt aFlagIndex)
227 { iFlags = T(iFlags^FlagMask(aFlagIndex)); }
230 inline TBool TBitFlagsT<T>::operator[](TInt aFlagIndex) const
231 { return IsSet(aFlagIndex); }
234 inline TBitFlagsT<T>& TBitFlagsT<T>::operator=(const TBitFlagsT<T>& aFlags)
235 { iFlags = aFlags.iFlags; return *this; }
238 inline TBool TBitFlagsT<T>::operator==(const TBitFlagsT<T>& aFlags)
239 { return iFlags == aFlags.Value(); }
242 inline void TBitFlagsT<T>::SetAll()
243 { iFlags = T(~(T(0))); }
246 inline void TBitFlagsT<T>::ClearAll()