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 // Demonstrate use of M classes, or mixins - the 00015 // only use of multiple inheritance that has been 00016 // sanctioned by the E32 architecture team 00017 // This example shows how mixins can be used to 00018 // pass some protocol, and an associated object, from 00019 // a protocol provider to an protocol user. The user 00020 // is not supposed to know everything about the provider, 00021 // only about the protocol it's interested in. 00022 // In this specific example, the provider contains a pointer 00023 // to the _real provider_: the provider is derived 00024 // from the protocol base class, but the real provider is not. 00025 // The real provider may thus honour many protocols. 00026 // 00027 00028 00029 00030 #include "CommonFramework.h" 00031 00033 // 00034 // -----> CProtocol (definition) 00035 // 00036 // A protocol class for mixing in 00037 // 00039 class TProtocol 00040 { 00041 public: 00042 virtual void HandleEvent(TInt aEventCode)=0; 00043 }; 00044 00045 00047 // 00048 // -----> CProtocolUser (definition) 00049 // 00050 // Define a protocol user which uses this protocol 00051 // 00053 class CProtocolUser : public CBase 00054 { 00055 public: 00056 // Construction 00057 static CProtocolUser* NewLC(); 00058 static CProtocolUser* NewL(); 00059 00060 // Destruction 00061 ~CProtocolUser(); 00062 00063 // Some function which uses a protocol 00064 void DoSomething(TProtocol* aProtocol); 00065 00066 protected: 00067 // Construction assistance 00068 void ConstructL(); 00069 }; 00070 00071 00073 // 00074 // -----> CProtocolProvider (definition) 00075 // 00076 // A simple class which uses the mixin 00077 // 00079 class TProtocolProvider; 00080 class CProtocolProvider : public CBase 00081 { 00082 public: 00083 // Construction 00084 static CProtocolProvider* NewLC(); 00085 00086 // Destruction 00087 ~CProtocolProvider(); 00088 00089 // Calls the protocol user 00090 void CallProtocolUser(); 00091 00092 // Implement the protocol (handles the protocol) 00093 void HandleEvent(TInt aEventCode); 00094 00095 protected: 00096 // Construction assistance 00097 void ConstructL(); 00098 00099 private: 00100 // data members defined by this class 00101 CProtocolUser* iProtocolUser; 00102 TProtocolProvider* iProviderProtocol; 00103 }; 00104 00105 00107 // 00108 // -----> TProtocolProvider (definition) 00109 // 00110 // Define protocol implementation which passes on the implementation 00111 // to a real protocol provider 00112 // 00114 class TProtocolProvider : public TProtocol 00115 { 00116 public: 00117 // Construction 00118 TProtocolProvider(CProtocolProvider* aProvider); 00119 00120 // The protocol itself 00121 void HandleEvent(TInt aEventCode); 00122 00123 private: 00124 // The real provider 00125 CProtocolProvider* iProvider; 00126 }; 00127 00128 00130 // 00131 // -----> CProtocolUser (implementation) 00132 // 00134 CProtocolUser* CProtocolUser::NewLC() 00135 { 00136 CProtocolUser* self=new(ELeave) CProtocolUser; 00137 CleanupStack::PushL(self); 00138 self->ConstructL(); 00139 return self; 00140 } 00141 00142 CProtocolUser* CProtocolUser::NewL() 00143 { 00144 CProtocolUser* self=NewLC(); 00145 CleanupStack::Pop(); 00146 return self; 00147 } 00148 00149 CProtocolUser::~CProtocolUser() 00150 { 00151 } 00152 00153 void CProtocolUser::ConstructL() 00154 { 00155 } 00156 00157 void CProtocolUser::DoSomething(TProtocol* aProtocol) 00158 { 00159 // Do something that requires a protocol 00160 _LIT(KTxtExtSystemDoing,"External system doing something\n"); 00161 console->Printf(KTxtExtSystemDoing); 00162 _LIT(KTxtInvokingProtocol,"invoking protocol - event 3\n"); 00163 console->Printf(KTxtInvokingProtocol); 00164 // Handle an event 00165 aProtocol->HandleEvent(3); 00166 } 00167 00168 00170 // 00171 // -----> TProtocolProvider (implementation) 00172 // 00174 TProtocolProvider::TProtocolProvider(CProtocolProvider* aProvider) 00175 : iProvider(aProvider) 00176 { 00177 } 00178 00179 // see later for definition of HandleEvent() 00180 00181 00183 // 00184 // -----> CProtocolProvider (implementation) 00185 // 00187 CProtocolProvider* CProtocolProvider::NewLC() 00188 { 00189 CProtocolProvider* self=new(ELeave) CProtocolProvider; 00190 CleanupStack::PushL(self); 00191 self->ConstructL(); 00192 return self; 00193 }; 00194 00195 CProtocolProvider::~CProtocolProvider() 00196 { 00197 delete iProtocolUser; 00198 delete iProviderProtocol; 00199 } 00200 00201 void CProtocolProvider::ConstructL() 00202 { 00203 iProtocolUser=CProtocolUser::NewL(); 00204 iProviderProtocol=new(ELeave) TProtocolProvider(this); 00205 } 00206 00207 void CProtocolProvider::CallProtocolUser() 00208 { 00209 // Call the protocol user to do some work 00210 _LIT(KTxtCallProtUser,"CProtocolProvider calling protocol user\n"); 00211 console->Printf(KTxtCallProtUser); 00212 iProtocolUser->DoSomething(iProviderProtocol); 00213 // pass the intermediary, which implements the 00214 // protocol by passing it on to us, 00215 // to the protocol user 00216 } 00217 00218 void CProtocolProvider::HandleEvent(TInt aEventCode) 00219 { 00220 // A concrete implementation of the abstract protocol. 00221 // Handle an event in the protocol user 00222 _LIT(KFormat1,"CProtocolProvider handling event %d\n"); 00223 console->Printf(KFormat1,aEventCode); 00224 } 00225 00226 void TProtocolProvider::HandleEvent(TInt aEventCode) 00227 { 00228 // A concrete definition of TProtocol::HandleEvent() 00229 _LIT(KTxtHandling,"Handling through intermediary\n"); 00230 console->Printf(KTxtHandling); 00231 iProvider->HandleEvent(aEventCode); 00232 } 00233 00234 00236 // 00237 // Do the example 00238 // 00240 LOCAL_C void doExampleL() 00241 { 00242 // show use of mixin with simple class 00243 CProtocolProvider* simpleProvider=CProtocolProvider::NewLC(); 00244 // call protocol user 00245 simpleProvider->CallProtocolUser(); 00246 // Remove simpleProvider from cleanup stack and destroy 00247 CleanupStack::PopAndDestroy(); 00248 }
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.