First public contribution.
2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
24 #include <caf/caftypes.h>
25 #include <caf/bitset.h>
27 using namespace ContentAccess;
29 EXPORT_C CBitset* CBitset::NewLC(TInt aMaxBits)
31 CBitset* self = new(ELeave) CBitset();
32 CleanupStack::PushL(self);
33 self->ConstructL(aMaxBits);
37 EXPORT_C CBitset* CBitset::NewL(TInt aMaxBits)
39 CBitset* self=CBitset::NewLC(aMaxBits);
40 CleanupStack::Pop(self);
44 EXPORT_C CBitset* CBitset::NewLC(const CBitset& aSource)
46 CBitset* self = new (ELeave) CBitset(aSource);
47 CleanupStack::PushL(self);
48 self->ConstructL(aSource);
52 EXPORT_C CBitset* CBitset::NewL(const CBitset& aSource)
54 CBitset* self=CBitset::NewLC(aSource);
55 CleanupStack::Pop(self);
68 CBitset::CBitset(const CBitset& aSource)
69 : iWidth(aSource.iWidth), iMaxBits(aSource.iMaxBits)
73 void CBitset::IdentifyBit(TInt aBitId, // in
75 TInt &aBitInByte) const // out
77 // Identify the byte & bit corresponding to the Id
79 aBitInByte = aBitId % 8;
82 EXPORT_C void CBitset::Invert()
85 for (TInt j = 0; j < iWidth ; j++)
87 iBitSet[j] = static_cast<TUint8> (~iBitSet[j]);
91 EXPORT_C TInt CBitset::MaxBits() const
96 EXPORT_C void CBitset::Set(TInt aBit)
98 __ASSERT_ALWAYS(ValidBit(aBit), User::Panic(KCafUtilsPanic, EInvalidBit));
102 IdentifyBit(aBit, targetByte, targetBit);
104 // Or-in the relevant bit
105 iBitSet[targetByte] |= (1 << targetBit);
108 EXPORT_C void CBitset::Unset(TInt aBit)
110 __ASSERT_ALWAYS(ValidBit(aBit), User::Panic(KCafUtilsPanic, EInvalidBit));
113 IdentifyBit(aBit, targetByte, targetBit);
115 // And-in NOT(the relevant bit)
116 iBitSet[targetByte] &= ~(1 << targetBit);
119 EXPORT_C TBool CBitset::IsSet(TInt aBit) const
121 __ASSERT_ALWAYS(ValidBit(aBit), User::Panic(KCafUtilsPanic, EInvalidBit));
125 IdentifyBit(aBit, targetByte, targetBit);
127 // And-in the relevant bit and test for non-zero
128 if (iBitSet[targetByte] & (1 << targetBit))
138 EXPORT_C void CBitset::Reset()
141 Mem::FillZ(iBitSet, iWidth);
144 EXPORT_C void CBitset::SetAll()
146 Mem::Fill(iBitSet, iWidth, 0xFF);
149 void CBitset::ConstructL(TInt aMaxBits)
151 // Calculate the required number of bytes
153 iWidth = 1 + aMaxBits / 8;
155 iBitSet = new (ELeave) TUint8[iWidth];
156 Reset(); // Zero the set
159 void CBitset::ConstructL(const CBitset& aSource)
161 // Copy the bits of the source
162 iBitSet = new (ELeave) TUint8[iWidth];
163 Mem::Copy(iBitSet, aSource.iBitSet, iWidth);
166 EXPORT_C void CBitset::ExternalizeL(RWriteStream& aStream) const
169 aStream.WriteInt32L(iMaxBits);
170 // Create a descriptor to encapsulate the set
171 TPtrC8 ptr(iBitSet, iWidth);
172 // Write the descriptor
176 EXPORT_C void CBitset::InternalizeL(RReadStream& aStream)
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
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)
196 // avoid self-assignment
197 if (this == &aSource)
202 // check for source/dest size mismatch
203 if (aSource.iWidth != iWidth)
205 // always use largest width
206 TInt widest = iWidth;
207 if (widest < aSource.iWidth)
209 widest = aSource.iWidth;
212 // Malloc the desired width
213 TUint8 *temp = (TUint8 *) new (ELeave) TUint8[widest];
216 Mem::FillZ(temp, widest);
218 // Now it is safe to re-assign the set
224 // Width may be the same, but the Max-Id may not, so
226 iMaxBits = aSource.iMaxBits;
228 // Based on the source-size, copy the bitset
229 Mem::Copy(iBitSet, aSource.iBitSet, aSource.iWidth);
234 EXPORT_C TBool CBitset::operator==(const CBitset& aSource) const
236 TBool result = ETrue;
237 if (this == &aSource)
241 TInt narrowest = iMaxBits;
242 if (narrowest > aSource.iMaxBits)
244 narrowest = aSource.iMaxBits;
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))
254 // Now, compare the last set of bits manually
255 for (TInt i = narrowest/8 * 8; i < narrowest; ++i)
257 // Comparison of booleans - compare their negations
258 if (!IsSet(i) != !aSource.IsSet(i))
267 EXPORT_C void CBitset::SetListL(TInt aItems, ...)
270 va_start(ap, aItems);
272 // Create a new set (of identical size)
273 CBitset *tempSet = CBitset::NewLC(iMaxBits);
275 // Duplicate this into temporary bitset
278 for (TInt i = 0; i < aItems; i++)
280 TInt x = va_arg(ap, TInt);
284 // No failure, so copy back into this
288 CleanupStack::Pop(tempSet);
292 EXPORT_C TBool CBitset::IsSetList(TInt aItems, ...) const
295 va_start(ap, aItems);
297 for (TInt i = 0; i < aItems; i++)
299 TInt x = va_arg(ap, TInt);
309 #endif // #ifndef REMOVE_CAF1