diff -r f345bda72bc4 -r 43e37759235e Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/_range_checking_8cpp-source.html --- a/Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/_range_checking_8cpp-source.html Tue Mar 30 11:56:28 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,255 +0,0 @@ - -
-00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies). -00002 // All rights reserved. -00003 // This component and the accompanying materials are made available -00004 // under the terms of "Eclipse Public License v1.0" -00005 // which accompanies this distribution, and is available -00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html". -00007 // -00008 // Initial Contributors: -00009 // Nokia Corporation - initial contribution. -00010 // -00011 // Contributors: -00012 // -00013 // Description: -00014 // Code to demonstrate the range checking wrapper class -00015 // for C++ arrays; i.e. the TFixedArray<class T,TInt S>. -00016 // -00017 -00018 #include "CommonFramework.h" -00019 -00020 // -00021 // Definition of the TTest class -00022 // -00023 class TTest -00024 { -00025 public: -00026 TTest(); -00027 TTest(TInt aValue); -00028 void SetValue(TInt aValue); -00029 TInt GetValue() const; -00030 private : -00031 TInt iX; -00032 TBuf<8> iPadding; // never used in this example -00033 }; -00034 -00035 TTest::TTest() -00036 : iX(1) -00037 {} -00038 -00039 TTest::TTest(TInt aValue) -00040 : iX(aValue) -00041 {} -00042 -00043 TInt TTest::GetValue() const -00044 { -00045 return iX; -00046 } -00047 -00048 void TTest::SetValue(TInt aValue) -00049 { -00050 iX = aValue; -00051 } -00052 -00053 // -00054 // Definition of the CTest class -00055 // -00056 class CTest : public CBase -00057 { -00058 public: -00059 static CTest* NewL(const TDesC& aText); -00060 ~CTest(); -00061 void ConstructL(const TDesC& aText); -00062 private : -00063 HBufC* iSomeText; -00064 }; -00065 -00066 CTest* CTest::NewL(const TDesC& aText) -00067 { -00068 CTest* self=new (ELeave) CTest; -00069 CleanupStack::PushL(self); -00070 self->ConstructL(aText); -00071 CleanupStack::Pop(); -00072 return self; -00073 } -00074 -00075 void CTest::ConstructL(const TDesC& aText) -00076 { -00077 iSomeText = aText.AllocL(); -00078 } -00079 -00080 CTest::~CTest() -00081 { -00082 delete iSomeText; -00083 } -00084 -00085 -00086 -00087 // Some source data -00088 const TTest data[] = {TTest(1),TTest(2),TTest(3),TTest(4)}; -00089 -00090 // -00091 // main body of the example -00092 // -00093 static void doExampleL() -00094 { -00095 -00096 // A C++ array of 4 elements of type class TTest. -00097 // -00098 // Effectively the same as if we had explicitly -00099 // declared: TTest array[4]; -00100 // -00101 TFixedArray<TTest,4> array; -00102 -00103 -00104 // -00105 // Copy 4 elements into the array. -00106 // -00107 // We could have constructed the array by: -00108 // TFixedArray<TTest,4> array(&data[0],4); -00109 array.Copy(&data[0],4); -00110 -00111 // -00112 // Trying to execute the following call would result -00113 // in a USER 133 panic in a debug build. The range checking -00114 // stops us from trying to write to a 5th element which -00115 // does not (legitimately) exist. -00116 // -00117 //array.Copy(&array[0],5); -00118 -00119 // -00120 // Count() should return the number of array elements, i.e. 4 -00121 // -00122 _LIT(KTxtFormat1,"Array size is %d\n"); -00123 console->Printf(KTxtFormat1,array.Count()); -00124 -00125 // -00126 // Length() should return the size of an array element; here -00127 // that should be the size of a TTest object. -00128 // -00129 _LIT(KTxtFormat2,"Length of an array element is %d\n\n"); -00130 console->Printf(KTxtFormat2,array.Length()); -00131 -00132 // -00133 // Pointers to the beginning and end of the array -00134 // -00135 _LIT(KTxtFormat3,"Array ends at ---> 0x%x\n starts at ---> 0x%x\n"); -00136 _LIT(KTxtFormat4,"Difference is decimal %d bytes (hopefully 4 times the size of TTest)\n\n"); -00137 console->Printf(KTxtFormat3,array.End(),array.Begin()); -00138 console->Printf(KTxtFormat4,((TUint8*)array.End() - (TUint8*)array.Begin())); -00139 -00140 // -00141 // Access the second element of the array and change it -00142 // using At(). -00143 // -00144 array.At(1).SetValue(100); -00145 -00146 // -00147 // Access the second element of the array and change it -00148 // using the [] operator. -00149 // -00150 array[1].SetValue(99); -00151 -00152 // The following call using At() will panic in both a -00153 // release and a debug build. -00154 //array.At(4).SetValue(100); -00155 -00156 // The following call using the [] operator will only panic -00157 // in a debug build. In a release build, this code -00158 // will go undetected and cause damage. -00159 //array[4].SetValue(99); -00160 -00161 // In this example we can also replace an entire element; -00162 // here we replace the third element. -00163 // In more complex cases, the TTest class could have -00164 // a suitable operator= defined -00165 _LIT(KTxtFormat5,"The new 3rd element has value %d\n\n"); -00166 TTest x(256); -00167 array.At(2) = x; -00168 console->Printf(KTxtFormat5,array.At(2).GetValue()); -00169 -00170 // -00171 // Construct a generic array. -00172 // -00173 // Once done, we can use the standard TArray functions -00174 // Count() and operator[] -00175 // Remember that a TArray cannot be used to change data in the -00176 // original array. -00177 // -00178 TArray<TTest> generic = array.Array(); -00179 -00180 _LIT(KTxtFormat6,"Array size as reported by TArray's Count() is %d\n\n"); -00181 console->Printf(KTxtFormat6,generic.Count()); -00182 -00183 // The value contained in the 2nd TTest element of the array -00184 // using the generic array's operator[] -00185 _LIT(KTxtFormat7,"Value contained in the 2nd TTest element of\nthe array using TArray's operator[] is %d\n\n"); -00186 console->Printf(KTxtFormat7,generic[1].GetValue()); -00187 -00188 // the following line will NOT compile -00189 //generic[1].SetValue(101); -00190 -00191 -00192 // This is A C++ array of 4 elements to contain -00193 // pointers to CTest type objects. -00194 // This is equivalent to -00195 // the naked declaration: -00196 // CTest* ptrarray[4]; -00197 // -00198 // -00199 TFixedArray<CTest*,4> ptrarray; -00200 -00201 -00202 // Allocate 4 CTest objects and put their pointers -00203 // into the array. -00204 // -00205 // We ignore cleanup issues arising from possible -00206 // failure of 'new'; this would need to be -00207 // considered in a real application. -00208 -00209 _LIT(KTxtDefault,"Default text"); -00210 _LIT(KTxtState1,"The 4 elements are: "); -00211 _LIT(KTxtState2,"After Reset() they are: "); -00212 _LIT(KTxtNewLine,"\n"); -00213 _LIT(KTxtFormat8,"0x%08x "); -00214 -00215 CTest* ptr; -00216 TInt ix; -00217 for (ix = 0; ix<4; ix++) -00218 { -00219 ptr = CTest::NewL(KTxtDefault); -00220 ptrarray.At(ix) = ptr; -00221 } -00222 -00223 console->Printf(KTxtState1); -00224 for (ix = 0; ix<4; ix++) -00225 { -00226 console->Printf(KTxtFormat8,ptrarray[ix]); -00227 } -00228 console->Printf(KTxtNewLine); -00229 -00230 // Now delete all the CTest objects -00231 ptrarray.DeleteAll(); -00232 -00233 // Set the array's data members to NULL -00234 ptrarray.Reset(); -00235 -00236 console->Printf(KTxtState2); -00237 for (ix = 0; ix<4; ix++) -00238 { -00239 console->Printf(KTxtFormat8,ptrarray[ix]); -00240 } -00241 console->Printf(KTxtNewLine); -00242 -00243 } -