sl@0: // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #ifndef __BABITFLAGS_H__ sl@0: #define __BABITFLAGS_H__ sl@0: sl@0: // System includes sl@0: #include sl@0: sl@0: // sl@0: // ----> TBitFlagsT (header) sl@0: // sl@0: template sl@0: class TBitFlagsT sl@0: /** sl@0: A simple class which manages the process of setting and clearing sl@0: flags in an abstract fashion. sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: { sl@0: // sl@0: public: // CONSTRUCT sl@0: // sl@0: sl@0: /** sl@0: * Default constructor - initialize all flags to zero sl@0: */ sl@0: inline TBitFlagsT(); sl@0: sl@0: /** sl@0: * Initialize the whole flag to a certain value sl@0: */ sl@0: inline TBitFlagsT(T aFlags); sl@0: sl@0: /** sl@0: * Copy constructor - initialize this flag object to mirror sl@0: * that of aFlags. sl@0: */ sl@0: inline TBitFlagsT(const TBitFlagsT& aFlags); sl@0: sl@0: // sl@0: public: // MANIPULATORS sl@0: // sl@0: sl@0: /** sl@0: * Set all the flags sl@0: */ sl@0: inline void SetAll(); sl@0: sl@0: /** sl@0: * Clear all the flags sl@0: */ sl@0: inline void ClearAll(); sl@0: sl@0: /** sl@0: * Set a particular flag sl@0: */ sl@0: inline void Set(TInt aFlagIndex); sl@0: sl@0: /** sl@0: * Clear a particular flag sl@0: */ sl@0: inline void Clear(TInt aFlagIndex); sl@0: sl@0: /** sl@0: * Set or clear a particular flag sl@0: * sl@0: * If aValue is 1, then the flag is set sl@0: * If aValue is 0, then the flag is cleared sl@0: */ sl@0: inline void Assign(TInt aFlagIndex, TBool aValue); sl@0: sl@0: /** sl@0: * Change the state of a particular flag. If the flag at the specified sl@0: * index was clear, then it becomes set, otherwise it becomes clear. sl@0: */ sl@0: inline void Toggle(TInt aFlagIndex); sl@0: sl@0: // sl@0: public: // OPERATORS sl@0: // sl@0: sl@0: /** sl@0: * Check if a particular flag is set or not sl@0: * sl@0: * @return A boolean indicating whether the specified flag is set or clear sl@0: */ sl@0: inline TBool operator[](TInt aFlagIndex) const; sl@0: sl@0: /** sl@0: * Assignment operator - assign specific value to the whole flag, replacing sl@0: * any existing value. sl@0: */ sl@0: inline TBitFlagsT& operator=(const TBitFlagsT& aFlags); sl@0: sl@0: /** sl@0: * Compare the value of the whole flag with a given value. sl@0: * sl@0: * @return A boolean indicating whether the two flags are identical. sl@0: */ sl@0: inline TBool operator==(const TBitFlagsT& aFlags); sl@0: sl@0: // sl@0: public: // ACCESS sl@0: // sl@0: sl@0: /** sl@0: * Check if a particular flag is set sl@0: */ sl@0: inline TBool IsSet(TInt aFlagIndex) const; sl@0: sl@0: /** sl@0: * Check if a particular flag is clear sl@0: */ sl@0: inline TBool IsClear(TInt aFlagIndex) const; sl@0: sl@0: /** sl@0: * Access the underlying value of the flag. sl@0: */ sl@0: inline T Value() const { return iFlags; } sl@0: sl@0: /** sl@0: * Assign a new value (directly) to this flag object. Replaces any sl@0: * existing individual flag settings. sl@0: */ sl@0: inline void SetValue(T aFlags) { iFlags = aFlags; } sl@0: sl@0: // sl@0: private: // INTERNAL sl@0: // sl@0: sl@0: /** sl@0: * Generate a mask for a particular flag sl@0: */ sl@0: inline T FlagMask(TInt aFlagIndex) const; sl@0: sl@0: // sl@0: public: // MEMBER DATA sl@0: // sl@0: sl@0: // The underlying object container which represents the flags. sl@0: T iFlags; sl@0: }; sl@0: sl@0: /** sl@0: Type definitions sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: typedef TBitFlagsT TBitFlags8; sl@0: // sl@0: sl@0: /** sl@0: Type definitions sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: typedef TBitFlagsT TBitFlags16; sl@0: // sl@0: sl@0: /** sl@0: Type definitions sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: typedef TBitFlagsT TBitFlags32; sl@0: // sl@0: sl@0: sl@0: typedef TBitFlags32 TBitFlags; sl@0: sl@0: sl@0: // sl@0: // ----> TBitFlagsT (inlines) sl@0: // sl@0: template sl@0: inline TBitFlagsT::TBitFlagsT() : iFlags(T(0)) sl@0: {} sl@0: sl@0: template sl@0: inline TBitFlagsT::TBitFlagsT(T aFlags) : iFlags(aFlags) sl@0: {} sl@0: sl@0: template sl@0: inline TBitFlagsT::TBitFlagsT(const TBitFlagsT& aFlags) : iFlags(aFlags.iFlags) sl@0: {} sl@0: sl@0: template sl@0: inline T TBitFlagsT::FlagMask(TInt aFlagIndex) const sl@0: { return T(T(1)< sl@0: inline TBool TBitFlagsT::IsSet(TInt aFlagIndex) const sl@0: { sl@0: // All out-of-range values should return false sl@0: if(aFlagIndex <= ((sizeof(T)<<3)-1) ) sl@0: { sl@0: return iFlags & FlagMask(aFlagIndex); sl@0: } sl@0: else sl@0: { sl@0: return EFalse; sl@0: } sl@0: sl@0: } sl@0: sl@0: template sl@0: inline TBool TBitFlagsT::IsClear(TInt aFlagIndex) const sl@0: { return !IsSet(aFlagIndex); } sl@0: sl@0: template sl@0: inline void TBitFlagsT::Set(TInt aFlagIndex) sl@0: { iFlags = T(iFlags | FlagMask(aFlagIndex)); } sl@0: sl@0: template sl@0: inline void TBitFlagsT::Clear(TInt aFlagIndex) sl@0: { iFlags = T(iFlags & ~(FlagMask(aFlagIndex))); } sl@0: sl@0: template sl@0: inline void TBitFlagsT::Assign(TInt aFlagIndex, TBool aVal) sl@0: { if (aVal) Set(aFlagIndex); else Clear(aFlagIndex); } sl@0: sl@0: template sl@0: inline void TBitFlagsT::Toggle(TInt aFlagIndex) sl@0: { iFlags = T(iFlags^FlagMask(aFlagIndex)); } sl@0: sl@0: template sl@0: inline TBool TBitFlagsT::operator[](TInt aFlagIndex) const sl@0: { return IsSet(aFlagIndex); } sl@0: sl@0: template sl@0: inline TBitFlagsT& TBitFlagsT::operator=(const TBitFlagsT& aFlags) sl@0: { iFlags = aFlags.iFlags; return *this; } sl@0: sl@0: template sl@0: inline TBool TBitFlagsT::operator==(const TBitFlagsT& aFlags) sl@0: { return iFlags == aFlags.Value(); } sl@0: sl@0: template sl@0: inline void TBitFlagsT::SetAll() sl@0: { iFlags = T(~(T(0))); } sl@0: sl@0: template sl@0: inline void TBitFlagsT::ClearAll() sl@0: { iFlags = T(0); } sl@0: sl@0: sl@0: #endif