os/security/contentmgmt/contentaccessfwfordrm/source/cafutils/bitset.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/contentmgmt/contentaccessfwfordrm/source/cafutils/bitset.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,309 @@
     1.4 +/*
     1.5 +* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#ifndef REMOVE_CAF1
    1.23 +
    1.24 +#include <e32std.h>
    1.25 +#include <s32strm.h>
    1.26 +#include <stdarg.h>
    1.27 +#include <caf/caftypes.h>
    1.28 +#include <caf/bitset.h>
    1.29 +
    1.30 +using namespace ContentAccess;
    1.31 +
    1.32 +EXPORT_C CBitset* CBitset::NewLC(TInt aMaxBits)
    1.33 +	{
    1.34 +	CBitset* self = new(ELeave) CBitset();
    1.35 +	CleanupStack::PushL(self);
    1.36 +	self->ConstructL(aMaxBits);
    1.37 +	return self;
    1.38 +	}
    1.39 +
    1.40 +EXPORT_C CBitset* CBitset::NewL(TInt aMaxBits)
    1.41 +	{
    1.42 +	CBitset* self=CBitset::NewLC(aMaxBits);
    1.43 +	CleanupStack::Pop(self);
    1.44 +	return self;
    1.45 +	}
    1.46 +
    1.47 +EXPORT_C CBitset* CBitset::NewLC(const CBitset& aSource)
    1.48 +	{
    1.49 +	CBitset* self = new (ELeave) CBitset(aSource);
    1.50 +	CleanupStack::PushL(self);
    1.51 +	self->ConstructL(aSource);
    1.52 +	return self;
    1.53 +	}
    1.54 +
    1.55 +EXPORT_C CBitset* CBitset::NewL(const CBitset& aSource)
    1.56 +	{
    1.57 +	CBitset* self=CBitset::NewLC(aSource);
    1.58 +	CleanupStack::Pop(self);
    1.59 +	return self;
    1.60 +	}
    1.61 +
    1.62 +CBitset::~CBitset() 
    1.63 +	{ 
    1.64 +	delete [] iBitSet;
    1.65 +	}
    1.66 +
    1.67 +CBitset::CBitset()
    1.68 +	{
    1.69 +	}
    1.70 +
    1.71 +CBitset::CBitset(const CBitset& aSource)
    1.72 +	: iWidth(aSource.iWidth), iMaxBits(aSource.iMaxBits)
    1.73 +	{
    1.74 +	}
    1.75 +
    1.76 +void CBitset::IdentifyBit(TInt aBitId, // in
    1.77 +						  TInt &aByte, // out
    1.78 +						  TInt &aBitInByte) const // out
    1.79 +	{
    1.80 +	// Identify the byte & bit corresponding to the Id
    1.81 +	aByte = aBitId / 8;
    1.82 +	aBitInByte  = aBitId % 8;
    1.83 +	}
    1.84 +
    1.85 +EXPORT_C void CBitset::Invert()
    1.86 +	{
    1.87 +	// Invert array
    1.88 +	for (TInt j = 0; j < iWidth ; j++)
    1.89 +		{
    1.90 +		iBitSet[j] = static_cast<TUint8> (~iBitSet[j]);
    1.91 +		}
    1.92 +	}
    1.93 +
    1.94 +EXPORT_C TInt CBitset::MaxBits() const
    1.95 +	{
    1.96 +	return iMaxBits;
    1.97 +	}
    1.98 +
    1.99 +EXPORT_C void CBitset::Set(TInt aBit)
   1.100 +	{
   1.101 +	__ASSERT_ALWAYS(ValidBit(aBit), User::Panic(KCafUtilsPanic, EInvalidBit));
   1.102 +	
   1.103 +	TInt targetByte;
   1.104 +	TInt targetBit;
   1.105 +	IdentifyBit(aBit, targetByte, targetBit);
   1.106 +
   1.107 +	// Or-in the relevant bit
   1.108 +	iBitSet[targetByte] |= (1 << targetBit);
   1.109 +	}
   1.110 +
   1.111 +EXPORT_C void CBitset::Unset(TInt aBit)
   1.112 +	{
   1.113 +	__ASSERT_ALWAYS(ValidBit(aBit), User::Panic(KCafUtilsPanic, EInvalidBit));
   1.114 +	TInt targetByte;
   1.115 +	TInt targetBit;
   1.116 +	IdentifyBit(aBit, targetByte, targetBit);
   1.117 +
   1.118 +	// And-in NOT(the relevant bit)
   1.119 +	iBitSet[targetByte] &= ~(1 << targetBit);
   1.120 +	}
   1.121 +
   1.122 +EXPORT_C TBool CBitset::IsSet(TInt aBit) const
   1.123 +	{
   1.124 +	__ASSERT_ALWAYS(ValidBit(aBit), User::Panic(KCafUtilsPanic, EInvalidBit));
   1.125 +	
   1.126 +	TInt targetByte;
   1.127 +	TInt targetBit;
   1.128 +	IdentifyBit(aBit, targetByte, targetBit);
   1.129 +
   1.130 +	// And-in the relevant bit and test for non-zero
   1.131 +	if (iBitSet[targetByte] & (1 << targetBit))
   1.132 +		{
   1.133 +		return(ETrue);
   1.134 +		}
   1.135 +	else
   1.136 +		{
   1.137 +		return(EFalse);
   1.138 +		}
   1.139 +	}
   1.140 +
   1.141 +EXPORT_C void CBitset::Reset()
   1.142 +	{
   1.143 +	// Reset the array
   1.144 +	Mem::FillZ(iBitSet, iWidth);
   1.145 +	}
   1.146 +
   1.147 +EXPORT_C void CBitset::SetAll()
   1.148 +	{
   1.149 +	Mem::Fill(iBitSet, iWidth, 0xFF);
   1.150 +	}
   1.151 +
   1.152 +void CBitset::ConstructL(TInt aMaxBits)
   1.153 +	{
   1.154 +	// Calculate the required number of bytes
   1.155 +	iMaxBits = aMaxBits;
   1.156 +	iWidth          = 1 + aMaxBits / 8;
   1.157 +
   1.158 +	iBitSet = new (ELeave) TUint8[iWidth];
   1.159 +	Reset(); // Zero the set
   1.160 +    }
   1.161 +
   1.162 +void CBitset::ConstructL(const CBitset& aSource)
   1.163 +	{
   1.164 +	// Copy the bits of the source
   1.165 +	iBitSet = new (ELeave) TUint8[iWidth];
   1.166 +	Mem::Copy(iBitSet, aSource.iBitSet, iWidth);
   1.167 +    }
   1.168 +
   1.169 +EXPORT_C void CBitset::ExternalizeL(RWriteStream& aStream) const
   1.170 +	{
   1.171 +	// Write the max-Id
   1.172 +	aStream.WriteInt32L(iMaxBits);
   1.173 +	// Create a descriptor to encapsulate the set
   1.174 +	TPtrC8 ptr(iBitSet, iWidth);
   1.175 +	// Write the descriptor
   1.176 +	aStream.WriteL(ptr);
   1.177 +	}
   1.178 +
   1.179 +EXPORT_C void CBitset::InternalizeL(RReadStream& aStream)
   1.180 +	{
   1.181 +	// Delete the array 
   1.182 +	delete [] iBitSet;
   1.183 +	iBitSet = NULL; // safety
   1.184 +	// Call the constructor with the max-Id value
   1.185 +	ConstructL(aStream.ReadInt32L());
   1.186 +	// Create a descriptor to encapsulate the set
   1.187 +	TPtr8 ptr(iBitSet, 0, iWidth);
   1.188 +	// Load into the descriptor
   1.189 +	aStream.ReadL(ptr);
   1.190 +	}
   1.191 +
   1.192 +//
   1.193 +// NB: leavescan will complain that this method calls a leaving
   1.194 +// function. The Leave is properly documented in the header
   1.195 +// file. Other than that, not much can be done, especially this
   1.196 +// class has already been deprecated.
   1.197 +EXPORT_C CBitset& CBitset::operator=(const CBitset& aSource)
   1.198 +	{
   1.199 +	// avoid self-assignment
   1.200 +	if (this == &aSource)
   1.201 +		{
   1.202 +		return *this;
   1.203 +		}
   1.204 +	
   1.205 +	// check for source/dest size mismatch
   1.206 +	if (aSource.iWidth != iWidth)
   1.207 +		{
   1.208 +		// always use largest width
   1.209 +		TInt widest = iWidth; 
   1.210 +		if (widest < aSource.iWidth)
   1.211 +			{
   1.212 +			widest = aSource.iWidth;
   1.213 +			}
   1.214 +		
   1.215 +		// Malloc the desired width
   1.216 +		TUint8 *temp = (TUint8 *) new (ELeave) TUint8[widest];
   1.217 +		
   1.218 +		// And zero
   1.219 +		Mem::FillZ(temp, widest);
   1.220 +			  
   1.221 +		// Now it is safe to re-assign the set
   1.222 +		delete [] iBitSet;
   1.223 +		iBitSet = temp;
   1.224 +		iWidth = widest;
   1.225 +		}
   1.226 +
   1.227 +	// Width may be the same, but the Max-Id may not, so
   1.228 +	// align these here
   1.229 +	iMaxBits = aSource.iMaxBits;
   1.230 +
   1.231 +	// Based on the source-size, copy the bitset
   1.232 +	Mem::Copy(iBitSet, aSource.iBitSet, aSource.iWidth);
   1.233 +
   1.234 +	return *this;
   1.235 +	}
   1.236 +
   1.237 +EXPORT_C TBool CBitset::operator==(const CBitset& aSource) const
   1.238 +	{
   1.239 +	TBool result = ETrue;
   1.240 +	if (this == &aSource)
   1.241 +		{
   1.242 +		return ETrue;
   1.243 +		}
   1.244 +	TInt narrowest = iMaxBits; 
   1.245 +	if (narrowest > aSource.iMaxBits)
   1.246 +		{
   1.247 +		narrowest = aSource.iMaxBits;
   1.248 +		}
   1.249 +
   1.250 +	// Compare the first "narrowest/8" bytes - if the result is non-zero,
   1.251 +	// then they are not equal
   1.252 +	if (Mem::Compare(iBitSet, narrowest/8, aSource.iBitSet, narrowest/8))
   1.253 +		{
   1.254 +		return EFalse;
   1.255 +		}
   1.256 +	
   1.257 +	// Now, compare the last set of bits manually
   1.258 +	for (TInt i = narrowest/8 * 8; i < narrowest; ++i)
   1.259 +		{
   1.260 +		// Comparison of booleans - compare their negations
   1.261 +		if (!IsSet(i) != !aSource.IsSet(i))
   1.262 +			{
   1.263 +			return EFalse;
   1.264 +			}
   1.265 +		}
   1.266 +		
   1.267 +	return result;
   1.268 +	}
   1.269 +
   1.270 +EXPORT_C void CBitset::SetListL(TInt aItems, ...)
   1.271 +	{
   1.272 +	va_list ap;
   1.273 +	va_start(ap, aItems);
   1.274 +
   1.275 +	// Create a new set (of identical size)
   1.276 +	CBitset *tempSet = CBitset::NewLC(iMaxBits);
   1.277 +
   1.278 +	// Duplicate this into temporary bitset
   1.279 +	*tempSet = *this;
   1.280 +	
   1.281 +	for (TInt i = 0; i < aItems; i++)
   1.282 +		{
   1.283 +		TInt x = va_arg(ap, TInt);
   1.284 +		tempSet->Set(x);
   1.285 +		}
   1.286 +	
   1.287 +	// No failure, so copy back into this
   1.288 +	*this = *tempSet;
   1.289 +	va_end(ap);
   1.290 +	
   1.291 +	CleanupStack::Pop(tempSet);
   1.292 +	delete tempSet;
   1.293 +	}
   1.294 +
   1.295 +EXPORT_C TBool CBitset::IsSetList(TInt aItems, ...) const
   1.296 +	{
   1.297 +	va_list ap;
   1.298 +	va_start(ap, aItems);
   1.299 +
   1.300 +	for (TInt i = 0; i < aItems; i++)
   1.301 +		{
   1.302 +		TInt x = va_arg(ap, TInt);
   1.303 +		if (!IsSet(x))
   1.304 +			{
   1.305 +			return (EFalse);
   1.306 +			}
   1.307 +		}
   1.308 +	va_end(ap);
   1.309 +	return (ETrue);
   1.310 +	}
   1.311 +
   1.312 +#endif // #ifndef REMOVE_CAF1