os/security/contentmgmt/contentaccessfwfordrm/source/cafutils/bitset.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 #ifndef REMOVE_CAF1
    20 
    21 #include <e32std.h>
    22 #include <s32strm.h>
    23 #include <stdarg.h>
    24 #include <caf/caftypes.h>
    25 #include <caf/bitset.h>
    26 
    27 using namespace ContentAccess;
    28 
    29 EXPORT_C CBitset* CBitset::NewLC(TInt aMaxBits)
    30 	{
    31 	CBitset* self = new(ELeave) CBitset();
    32 	CleanupStack::PushL(self);
    33 	self->ConstructL(aMaxBits);
    34 	return self;
    35 	}
    36 
    37 EXPORT_C CBitset* CBitset::NewL(TInt aMaxBits)
    38 	{
    39 	CBitset* self=CBitset::NewLC(aMaxBits);
    40 	CleanupStack::Pop(self);
    41 	return self;
    42 	}
    43 
    44 EXPORT_C CBitset* CBitset::NewLC(const CBitset& aSource)
    45 	{
    46 	CBitset* self = new (ELeave) CBitset(aSource);
    47 	CleanupStack::PushL(self);
    48 	self->ConstructL(aSource);
    49 	return self;
    50 	}
    51 
    52 EXPORT_C CBitset* CBitset::NewL(const CBitset& aSource)
    53 	{
    54 	CBitset* self=CBitset::NewLC(aSource);
    55 	CleanupStack::Pop(self);
    56 	return self;
    57 	}
    58 
    59 CBitset::~CBitset() 
    60 	{ 
    61 	delete [] iBitSet;
    62 	}
    63 
    64 CBitset::CBitset()
    65 	{
    66 	}
    67 
    68 CBitset::CBitset(const CBitset& aSource)
    69 	: iWidth(aSource.iWidth), iMaxBits(aSource.iMaxBits)
    70 	{
    71 	}
    72 
    73 void CBitset::IdentifyBit(TInt aBitId, // in
    74 						  TInt &aByte, // out
    75 						  TInt &aBitInByte) const // out
    76 	{
    77 	// Identify the byte & bit corresponding to the Id
    78 	aByte = aBitId / 8;
    79 	aBitInByte  = aBitId % 8;
    80 	}
    81 
    82 EXPORT_C void CBitset::Invert()
    83 	{
    84 	// Invert array
    85 	for (TInt j = 0; j < iWidth ; j++)
    86 		{
    87 		iBitSet[j] = static_cast<TUint8> (~iBitSet[j]);
    88 		}
    89 	}
    90 
    91 EXPORT_C TInt CBitset::MaxBits() const
    92 	{
    93 	return iMaxBits;
    94 	}
    95 
    96 EXPORT_C void CBitset::Set(TInt aBit)
    97 	{
    98 	__ASSERT_ALWAYS(ValidBit(aBit), User::Panic(KCafUtilsPanic, EInvalidBit));
    99 	
   100 	TInt targetByte;
   101 	TInt targetBit;
   102 	IdentifyBit(aBit, targetByte, targetBit);
   103 
   104 	// Or-in the relevant bit
   105 	iBitSet[targetByte] |= (1 << targetBit);
   106 	}
   107 
   108 EXPORT_C void CBitset::Unset(TInt aBit)
   109 	{
   110 	__ASSERT_ALWAYS(ValidBit(aBit), User::Panic(KCafUtilsPanic, EInvalidBit));
   111 	TInt targetByte;
   112 	TInt targetBit;
   113 	IdentifyBit(aBit, targetByte, targetBit);
   114 
   115 	// And-in NOT(the relevant bit)
   116 	iBitSet[targetByte] &= ~(1 << targetBit);
   117 	}
   118 
   119 EXPORT_C TBool CBitset::IsSet(TInt aBit) const
   120 	{
   121 	__ASSERT_ALWAYS(ValidBit(aBit), User::Panic(KCafUtilsPanic, EInvalidBit));
   122 	
   123 	TInt targetByte;
   124 	TInt targetBit;
   125 	IdentifyBit(aBit, targetByte, targetBit);
   126 
   127 	// And-in the relevant bit and test for non-zero
   128 	if (iBitSet[targetByte] & (1 << targetBit))
   129 		{
   130 		return(ETrue);
   131 		}
   132 	else
   133 		{
   134 		return(EFalse);
   135 		}
   136 	}
   137 
   138 EXPORT_C void CBitset::Reset()
   139 	{
   140 	// Reset the array
   141 	Mem::FillZ(iBitSet, iWidth);
   142 	}
   143 
   144 EXPORT_C void CBitset::SetAll()
   145 	{
   146 	Mem::Fill(iBitSet, iWidth, 0xFF);
   147 	}
   148 
   149 void CBitset::ConstructL(TInt aMaxBits)
   150 	{
   151 	// Calculate the required number of bytes
   152 	iMaxBits = aMaxBits;
   153 	iWidth          = 1 + aMaxBits / 8;
   154 
   155 	iBitSet = new (ELeave) TUint8[iWidth];
   156 	Reset(); // Zero the set
   157     }
   158 
   159 void CBitset::ConstructL(const CBitset& aSource)
   160 	{
   161 	// Copy the bits of the source
   162 	iBitSet = new (ELeave) TUint8[iWidth];
   163 	Mem::Copy(iBitSet, aSource.iBitSet, iWidth);
   164     }
   165 
   166 EXPORT_C void CBitset::ExternalizeL(RWriteStream& aStream) const
   167 	{
   168 	// Write the max-Id
   169 	aStream.WriteInt32L(iMaxBits);
   170 	// Create a descriptor to encapsulate the set
   171 	TPtrC8 ptr(iBitSet, iWidth);
   172 	// Write the descriptor
   173 	aStream.WriteL(ptr);
   174 	}
   175 
   176 EXPORT_C void CBitset::InternalizeL(RReadStream& aStream)
   177 	{
   178 	// Delete the array 
   179 	delete [] iBitSet;
   180 	iBitSet = NULL; // safety
   181 	// Call the constructor with the max-Id value
   182 	ConstructL(aStream.ReadInt32L());
   183 	// Create a descriptor to encapsulate the set
   184 	TPtr8 ptr(iBitSet, 0, iWidth);
   185 	// Load into the descriptor
   186 	aStream.ReadL(ptr);
   187 	}
   188 
   189 //
   190 // NB: leavescan will complain that this method calls a leaving
   191 // function. The Leave is properly documented in the header
   192 // file. Other than that, not much can be done, especially this
   193 // class has already been deprecated.
   194 EXPORT_C CBitset& CBitset::operator=(const CBitset& aSource)
   195 	{
   196 	// avoid self-assignment
   197 	if (this == &aSource)
   198 		{
   199 		return *this;
   200 		}
   201 	
   202 	// check for source/dest size mismatch
   203 	if (aSource.iWidth != iWidth)
   204 		{
   205 		// always use largest width
   206 		TInt widest = iWidth; 
   207 		if (widest < aSource.iWidth)
   208 			{
   209 			widest = aSource.iWidth;
   210 			}
   211 		
   212 		// Malloc the desired width
   213 		TUint8 *temp = (TUint8 *) new (ELeave) TUint8[widest];
   214 		
   215 		// And zero
   216 		Mem::FillZ(temp, widest);
   217 			  
   218 		// Now it is safe to re-assign the set
   219 		delete [] iBitSet;
   220 		iBitSet = temp;
   221 		iWidth = widest;
   222 		}
   223 
   224 	// Width may be the same, but the Max-Id may not, so
   225 	// align these here
   226 	iMaxBits = aSource.iMaxBits;
   227 
   228 	// Based on the source-size, copy the bitset
   229 	Mem::Copy(iBitSet, aSource.iBitSet, aSource.iWidth);
   230 
   231 	return *this;
   232 	}
   233 
   234 EXPORT_C TBool CBitset::operator==(const CBitset& aSource) const
   235 	{
   236 	TBool result = ETrue;
   237 	if (this == &aSource)
   238 		{
   239 		return ETrue;
   240 		}
   241 	TInt narrowest = iMaxBits; 
   242 	if (narrowest > aSource.iMaxBits)
   243 		{
   244 		narrowest = aSource.iMaxBits;
   245 		}
   246 
   247 	// Compare the first "narrowest/8" bytes - if the result is non-zero,
   248 	// then they are not equal
   249 	if (Mem::Compare(iBitSet, narrowest/8, aSource.iBitSet, narrowest/8))
   250 		{
   251 		return EFalse;
   252 		}
   253 	
   254 	// Now, compare the last set of bits manually
   255 	for (TInt i = narrowest/8 * 8; i < narrowest; ++i)
   256 		{
   257 		// Comparison of booleans - compare their negations
   258 		if (!IsSet(i) != !aSource.IsSet(i))
   259 			{
   260 			return EFalse;
   261 			}
   262 		}
   263 		
   264 	return result;
   265 	}
   266 
   267 EXPORT_C void CBitset::SetListL(TInt aItems, ...)
   268 	{
   269 	va_list ap;
   270 	va_start(ap, aItems);
   271 
   272 	// Create a new set (of identical size)
   273 	CBitset *tempSet = CBitset::NewLC(iMaxBits);
   274 
   275 	// Duplicate this into temporary bitset
   276 	*tempSet = *this;
   277 	
   278 	for (TInt i = 0; i < aItems; i++)
   279 		{
   280 		TInt x = va_arg(ap, TInt);
   281 		tempSet->Set(x);
   282 		}
   283 	
   284 	// No failure, so copy back into this
   285 	*this = *tempSet;
   286 	va_end(ap);
   287 	
   288 	CleanupStack::Pop(tempSet);
   289 	delete tempSet;
   290 	}
   291 
   292 EXPORT_C TBool CBitset::IsSetList(TInt aItems, ...) const
   293 	{
   294 	va_list ap;
   295 	va_start(ap, aItems);
   296 
   297 	for (TInt i = 0; i < aItems; i++)
   298 		{
   299 		TInt x = va_arg(ap, TInt);
   300 		if (!IsSet(x))
   301 			{
   302 			return (EFalse);
   303 			}
   304 		}
   305 	va_end(ap);
   306 	return (ETrue);
   307 	}
   308 
   309 #endif // #ifndef REMOVE_CAF1