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 // 00015 00016 #include "CommonFramework.h" 00017 00018 // start of real example 00019 00020 // #include specific files 00021 00022 // class with single-phase construction, NewL and NewLC 00023 00024 class COnePhaser : public CBase { 00025 public: // data 00026 TInt iInt1; // one integer 00027 TInt iInt2; // another integer 00028 public: // functions 00029 // construct/destruct 00030 static COnePhaser* NewL() 00031 { // construct and leave if error 00032 return new (ELeave) COnePhaser; // new COnePhaser, C++ constructor 00033 }; 00034 static COnePhaser* NewLC() 00035 { // construct, leave if error, else push to clean-up stack 00036 COnePhaser* self=COnePhaser::NewL(); // construct 00037 CleanupStack::PushL(self); // push to clean-up stack 00038 return self; // and return new object 00039 }; 00040 // other functions 00041 void Print() 00042 { 00043 _LIT(KFormat1,"COnePhaser {TInt %d, TInt %d}"); 00044 console->Printf(KFormat1, iInt1, iInt2); 00045 } 00046 protected: // functions 00047 // construct 00048 COnePhaser() 00049 { // standard C++ constructor, because it cannot leave 00050 iInt1=3; // set first int to 3 00051 // iInt2=0; // unnecessary 00052 } 00053 }; 00054 00055 // class with two-phase construction 00056 00057 class CTwoPhaser : public CBase { 00058 public: // data 00059 TInt iInt1; // one integer 00060 TInt iInt2; // another integer 00061 RTimer iTimer; // a resource 00062 COnePhaser* iOnePhaser; // another CBase object 00063 public: // functions 00064 // construct/destruct 00065 static CTwoPhaser* NewLC(TInt aInt1) 00066 { // construct and leave on the clean-up stack 00067 CTwoPhaser* self=new (ELeave) CTwoPhaser; // new CTwoPhaser, initialized to binary zeroes 00068 CleanupStack::PushL(self); // push to clean-up stack 00069 self->ConstructL(aInt1); // construct 00070 return self; // return new object 00071 }; 00072 static CTwoPhaser* NewL(TInt aInt1) 00073 { // construct and don't leave on clean-up stack 00074 CTwoPhaser* self=CTwoPhaser::NewLC(aInt1); // construct and leave on clean-up stack 00075 CleanupStack::Pop(); // pop from clean-up stack 00076 return self; // return new object 00077 }; 00078 virtual ~CTwoPhaser() 00079 { 00080 delete iOnePhaser; // just destroy anything we point to 00081 iTimer.Close(); // close timer 00082 } 00083 // other functions 00084 void Print() 00085 { 00086 _LIT(KFormat2,"CTwoPhaser {TInt %d, TInt %d, RTimer, "); 00087 console->Printf(KFormat2, iInt1, iInt2); 00088 iOnePhaser->Print(); 00089 _LIT(KtxtCloseCurly,"}"); 00090 console->Printf(KtxtCloseCurly); 00091 } 00092 protected: // functions 00093 // construct support 00094 void ConstructL(TInt aInt1) 00095 { // do the work involved in construction 00096 iInt1=aInt1; // assign iInt1 from argument 00097 User::LeaveIfError(iTimer.CreateLocal()); // initialize timer 00098 iOnePhaser=COnePhaser::NewL(); // make a new one-phaser 00099 } 00100 }; 00101 00102 // abstract class derived from CBase 00103 00104 class CAbstract : public CBase 00105 { 00106 public: // data 00107 CTwoPhaser* iTwoPhaser; // a contained object 00108 public: // functions 00109 void SomeFunction(TInt aInt) 00110 { // request some function 00111 _LIT(KTxtbeginSomeFunction,"beginning to do SomeFunction()\n"); 00112 console->Printf(KTxtbeginSomeFunction); 00113 DoSomeFunction(aInt); // invoke virtual function 00114 _LIT(KTxtfinishSomeFunction,"finished doing SomeFunction()\n"); 00115 console->Printf(KTxtfinishSomeFunction); 00116 } 00117 virtual ~CAbstract() 00118 { 00119 delete iTwoPhaser; 00120 } 00121 protected: 00122 virtual void DoSomeFunction(TInt aInt) const =0; 00123 // implement heart of some function, in derived class 00124 void ConstructL(TInt aInt) 00125 { // second phase of constructor 00126 iTwoPhaser=CTwoPhaser::NewL(aInt); 00127 // construct embedded object 00128 } 00129 }; 00130 00131 // class derived from above 00132 00133 class CConcrete : public CAbstract 00134 { 00135 public: // data 00136 COnePhaser* iOnePhaser; // some extra data 00137 public: // functions 00138 // construct/destruct 00139 static CConcrete* NewLC(TInt aInt) 00140 { // construct and leave on the clean-up stack 00141 CConcrete* self=new (ELeave) CConcrete; // new object, C++ constructor 00142 CleanupStack::PushL(self); // push to clean-up stack 00143 self->ConstructL(aInt); // CBase constructor 00144 return self; // return new object 00145 }; 00146 static CConcrete* NewL(TInt aInt) 00147 { // construct and don't leave on clean-up stack 00148 CConcrete* self=CConcrete::NewLC(aInt); // construct and leave on clean-up stack 00149 CleanupStack::Pop(); // pop from clean-up stack 00150 return self; // return new object 00151 }; 00152 virtual ~CConcrete() 00153 { 00154 delete iOnePhaser; // destroy member 00155 } 00156 // protocol 00157 virtual void DoSomeFunction(TInt aInt) const 00158 { // implementation of base class's pure-virtual function 00159 _LIT(KFormat3,"CConcrete::DoSomething(%d)\n"); 00160 console->Printf(KFormat3, aInt); 00161 } 00162 protected: 00163 // construct/destruct 00164 void ConstructL(TInt aInt) 00165 { // second phase of construction 00166 CAbstract::ConstructL(aInt); // base class constructor 00167 iOnePhaser=COnePhaser::NewL(); // contained member constructor 00168 } 00169 }; 00170 00171 // do the example 00172 LOCAL_C void doExampleL() 00173 { 00174 // two-phase object 00175 CTwoPhaser* twoPhaser=CTwoPhaser::NewLC(5); 00176 // new two-phase object 00177 twoPhaser->Print(); // print it 00178 _LIT(KTxtNewLine,"\n"); 00179 console->Printf(KTxtNewLine); // with trailing new-line 00180 CleanupStack::PopAndDestroy(); // two-phase object 00181 // concrete instance of an abstract class 00182 CAbstract* abstract=CConcrete::NewLC(9); 00183 // new concrete object, but refer to it with abstract pointer 00184 abstract->SomeFunction(11); // do something with it 00185 CleanupStack::PopAndDestroy(); // abstract object 00186 }
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.