Update contrib.
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.
22 #include "../common/inlines.h"
28 * This function performs a shallow copy,
29 * i.e. the memory holding the integer is not copied.
31 EXPORT_C RInteger::RInteger(const RInteger& aInteger)
39 * This function performs a shallow copy,
40 * i.e. the memory holding the integer is not copied.
42 EXPORT_C RInteger& RInteger::operator=(const RInteger& aInteger)
44 iSize = aInteger.iSize;
51 * Creates a new integer representing 0.
53 * @return An RInteger by value.
55 EXPORT_C RInteger RInteger::NewL(void)
57 return NewL(TInteger::Zero());
61 * Creates a new integer object representing a specified value.
63 * @param aValue A descriptor containing the big-endian binary
64 * representation of the value.
65 * @return An RInteger object representing the value.
67 EXPORT_C RInteger RInteger::NewL(const TDesC8& aValue)
70 //Construct zero's memory beyond the size of aValue after construction
71 self.CreateNewL(BytesToWords(aValue.Size()));
72 self.Construct(aValue);
77 * Creates an exact copy of an \c aInteger object.
79 * @param aInteger The integer you wish to copy
80 * @return An RInteger object representing an exact copy of
83 EXPORT_C RInteger RInteger::NewL(const TInteger& aInteger)
86 //don't need to CleanNewL as we'll copy straight from aInteger
87 self.CreateNewL(aInteger.Size());
88 self.Construct(aInteger);
93 * Creates a random integer uniformly distributed over [0, 2^aBits].
95 * @param aBits The number of bits you wish to randomly select.
96 * @param aAttr Enum specifying whether specific bits in the random number should
97 * be set. See TRandomAttribute for more information.
98 * @return A random RInteger object in the range specified.
100 EXPORT_C RInteger RInteger::NewRandomL(TUint aBits, TRandomAttribute aAttr)
103 self.CleanNewL(BitsToWords(aBits));
104 CleanupStack::PushL(self);
105 self.RandomizeL(aBits, aAttr);
106 CleanupStack::Pop(&self);
111 * Creates a random integer uniformly distributed over [x | min <= x <= max].
113 * @param aMin The smallest possible value for the random integer (inclusive).
114 * @param aMax The largest possible value for the random integer (inclusive).
115 * @return A random RInteger object in the range specified.
117 EXPORT_C RInteger RInteger::NewRandomL(const TInteger& aMin,
118 const TInteger& aMax)
121 self.CleanNewL(aMax.Size());
122 CleanupStack::PushL(self);
123 self.RandomizeL(aMin, aMax);
124 CleanupStack::Pop(&self);
129 * Finds a random prime integer in the range of [2, 2^aBits].
131 * This is done by picking a random integer and using that as a starting point
132 * for a sequential search for a prime. To verify the primality of number,
133 * this algorithm uses a probablistic primality test. This means that it is
134 * possible, although extremely improbable, that the number returned is a pseudoprime.
136 * @param aBits The number of bits you wish to randomly select your prime from.
137 * @param aAttr Enum specifying whether specific bits in the random number should
138 * be set. See TRandomAttribute for more information.
139 * @return A random RInteger representing a probable prime (with very high
140 * probablity) in the range specified.
142 EXPORT_C RInteger RInteger::NewPrimeL(TUint aBits, TRandomAttribute aAttr)
145 self.CleanNewL(BitsToWords(aBits));
146 CleanupStack::PushL(self);
147 self.PrimeRandomizeL(aBits, aAttr);
148 CleanupStack::Pop(&self);
153 * Creates a new integer from the value represented by \c aInteger.
155 * @param aInteger A signed word sized integer.
156 * @return An RInteger representation of aInteger by value.
158 EXPORT_C RInteger RInteger::NewL(TInt aInteger)
162 self.Construct(aInteger);
167 * Creates a new integer with a preallocated internal storage of \c aNumWords all
168 * initialised to zero.
170 * The resulting RInteger object is logically equivalent to RInteger::NewL(0).
171 * The only difference is that the internal storage requirements have been
172 * specified to be larger than the default. This is useful if you are about
173 * to perform an operation on this integer, that you know the resulting size
174 * requirements of, and wish to avoid a heap resize.
176 * @param aNumWords The number of words for to preallocated and zero fill.
177 * @return An RInteger object representing 0 with a preallocated
178 * zero-filled internal storage of aNumWords.
180 EXPORT_C RInteger RInteger::NewEmptyL(TUint aNumWords)
183 self.CleanNewL(aNumWords);
184 //There's no construct as there isn't anything to do
189 * Creates an RInteger object with no associated internal (heap) storage.
191 * All data members are initialised to zero. It is safe (although not strictly necessary)
192 * to push such an RInteger object onto the CleanupStack. This is useful, for example, if
193 * you want to pass an RInteger object by reference into a function and have it create
194 * the representation of the actual integer for you.
196 * Note that performing any operation on such an RInteger object other than the default
197 * assignment operator or copy constructor will panic your code.
199 * @return A stack based class that has no associated internal storage and thus
200 * does not represent any number.
202 EXPORT_C RInteger::RInteger(void)
207 * An overloaded TCleanupItem() allowing the RIntegers to be pushed,
208 * popped, and destroyed via the CleanupStack like any other CBase derived object.
210 EXPORT_C RInteger::operator TCleanupItem(void)
212 return TCleanupItem(&RInteger::CallClose, this);
216 * Helper function registered with the cleanup stack that just calls Close() for
217 * this RInteger object.
219 * @param aPtr A pointer to the object for which clean-up is to be performed.
221 EXPORT_C void RInteger::CallClose(TAny* aPtr)
223 ((RInteger*)aPtr)->Close();
227 * Zeros and then frees any memory owned by this RInteger object.
229 * An RInteger object that has been closed can safely fall off the stack.
231 EXPORT_C void RInteger::Close(void)
235 Mem::FillZ(Ptr(), Size()*4);
242 // Method is excluded from coverage due to the problem with BullsEye on ONB.
243 // Manually verified that this method is functionally covered.
244 #ifdef _BullseyeCoverage
245 #pragma suppress_warnings on
246 #pragma BullseyeCoverage off
247 #pragma suppress_warnings off
251 * Creates a new integer from the value represented by \c aInteger.
253 * @param aInteger An unsigned word sized integer.
254 * @return An RInteger representation of aInteger by value.
256 EXPORT_C RInteger RInteger::NewL(TUint aInteger)
260 self.Construct(aInteger);