epoc32/include/babitflags.h
branchSymbian2
changeset 2 2fe1408b6811
parent 0 061f57f2323e
child 4 837f303aceeb
equal deleted inserted replaced
1:666f914201fb 2:2fe1408b6811
     1 babitflags.h
     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".
       
     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 	{ return iFlags & FlagMask(aFlagIndex); }
       
   208 
       
   209 template <class T>
       
   210 inline TBool TBitFlagsT<T>::IsClear(TInt aFlagIndex) const
       
   211 	{ return !IsSet(aFlagIndex); }
       
   212 
       
   213 template <class T>
       
   214 inline void TBitFlagsT<T>::Set(TInt aFlagIndex)
       
   215 	{ iFlags = T(iFlags | FlagMask(aFlagIndex)); }
       
   216 
       
   217 template <class T>
       
   218 inline void TBitFlagsT<T>::Clear(TInt aFlagIndex)
       
   219 	{ iFlags = T(iFlags & ~(FlagMask(aFlagIndex))); }
       
   220 
       
   221 template <class T>
       
   222 inline void TBitFlagsT<T>::Assign(TInt aFlagIndex, TBool aVal)
       
   223 	{ if (aVal) Set(aFlagIndex); else Clear(aFlagIndex); }
       
   224 
       
   225 template <class T>
       
   226 inline void TBitFlagsT<T>::Toggle(TInt aFlagIndex)
       
   227 	{ iFlags = T(iFlags^FlagMask(aFlagIndex)); }
       
   228 
       
   229 template <class T>
       
   230 inline TBool TBitFlagsT<T>::operator[](TInt aFlagIndex) const
       
   231 	{ return IsSet(aFlagIndex); }
       
   232 
       
   233 template <class T>
       
   234 inline TBitFlagsT<T>& TBitFlagsT<T>::operator=(const TBitFlagsT<T>& aFlags)
       
   235 	{ iFlags = aFlags.iFlags; return *this; }
       
   236 
       
   237 template <class T>
       
   238 inline TBool TBitFlagsT<T>::operator==(const TBitFlagsT<T>& aFlags)
       
   239 	{ return iFlags == aFlags.Value(); }
       
   240 
       
   241 template <class T>
       
   242 inline void TBitFlagsT<T>::SetAll()
       
   243 	{ iFlags = T(~(T(0))); }
       
   244 
       
   245 template <class T>
       
   246 inline void TBitFlagsT<T>::ClearAll()
       
   247 	{ iFlags = T(0); }
       
   248 
       
   249 
       
   250 #endif