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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #ifndef __BABITFLAGS_H__
17 #define __BABITFLAGS_H__
23 // ----> TBitFlagsT (header)
28 A simple class which manages the process of setting and clearing
29 flags in an abstract fashion.
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);
55 public: // MANIPULATORS
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);
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);
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; }
146 * Generate a mask for a particular flag
148 inline T FlagMask(TInt aFlagIndex) const;
151 public: // MEMBER DATA
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;
187 // ----> TBitFlagsT (inlines)
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
208 // All out-of-range values should return false
209 if(aFlagIndex <= ((sizeof(T)<<3)-1) )
211 return iFlags & FlagMask(aFlagIndex);
221 inline TBool TBitFlagsT<T>::IsClear(TInt aFlagIndex) const
222 { return !IsSet(aFlagIndex); }
225 inline void TBitFlagsT<T>::Set(TInt aFlagIndex)
226 { iFlags = T(iFlags | FlagMask(aFlagIndex)); }
229 inline void TBitFlagsT<T>::Clear(TInt aFlagIndex)
230 { iFlags = T(iFlags & ~(FlagMask(aFlagIndex))); }
233 inline void TBitFlagsT<T>::Assign(TInt aFlagIndex, TBool aVal)
234 { if (aVal) Set(aFlagIndex); else Clear(aFlagIndex); }
237 inline void TBitFlagsT<T>::Toggle(TInt aFlagIndex)
238 { iFlags = T(iFlags^FlagMask(aFlagIndex)); }
241 inline TBool TBitFlagsT<T>::operator[](TInt aFlagIndex) const
242 { return IsSet(aFlagIndex); }
245 inline TBitFlagsT<T>& TBitFlagsT<T>::operator=(const TBitFlagsT<T>& aFlags)
246 { iFlags = aFlags.iFlags; return *this; }
249 inline TBool TBitFlagsT<T>::operator==(const TBitFlagsT<T>& aFlags)
250 { return iFlags == aFlags.Value(); }
253 inline void TBitFlagsT<T>::SetAll()
254 { iFlags = T(~(T(0))); }
257 inline void TBitFlagsT<T>::ClearAll()