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