00001 // Copyright (c) 1997-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 is derived from 00023 // an appropriate base class, and a mixin class representing 00024 // the protocol. The benefits of the method shown in example EUTYPEM2 00025 // are thus gained, without the inconvenience of intermediary 00026 // classes. 00027 // 00028 00029 00030 00031 #include "CommonFramework.h" 00032 00034 // 00035 // -----> CProtocol (definition) 00036 // 00037 // A protocol class for mixing in 00038 // 00040 class MProtocol 00041 { 00042 public: 00043 virtual void HandleEvent(TInt aEventCode)=0; 00044 }; 00045 00046 00048 // 00049 // -----> CProtocolUser (definition) 00050 // 00051 // Define a protocol user which uses this protocol 00052 // 00054 class CProtocolUser : public CBase 00055 { 00056 public: 00057 // Construction 00058 static CProtocolUser* NewLC(); 00059 static CProtocolUser* NewL(); 00060 00061 // Destruction 00062 ~CProtocolUser(); 00063 00064 // Some function which uses a protocol 00065 void DoSomething(MProtocol* aProtocol); 00066 00067 protected: 00068 // Construction assistance 00069 void ConstructL(); 00070 }; 00071 00072 00074 // 00075 // -----> CProtocolProvider (definition) 00076 // 00077 // A simple class which uses the mixin 00078 // 00080 class CProtocolProvider : public CBase, public MProtocol 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 }; 00103 00104 00106 // 00107 // -----> CProtocolUser (implementation) 00108 // 00110 CProtocolUser* CProtocolUser::NewLC() 00111 { 00112 CProtocolUser* self=new(ELeave) CProtocolUser; 00113 CleanupStack::PushL(self); 00114 self->ConstructL(); 00115 return self; 00116 } 00117 00118 CProtocolUser* CProtocolUser::NewL() 00119 { 00120 CProtocolUser* self=NewLC(); 00121 CleanupStack::Pop(); 00122 return self; 00123 } 00124 00125 CProtocolUser::~CProtocolUser() 00126 { 00127 } 00128 00129 void CProtocolUser::ConstructL() 00130 { 00131 } 00132 00133 void CProtocolUser::DoSomething(MProtocol* aProtocol) 00134 { 00135 // Do something that requires a protocol 00136 _LIT(KTxtExtSystemDoing,"External system doing something\n"); 00137 console->Printf(KTxtExtSystemDoing); 00138 _LIT(KTxtInvokingProtocol,"invoking protocol - event 3\n"); 00139 console->Printf(KTxtInvokingProtocol); 00140 // Handle an event 00141 aProtocol->HandleEvent(3); 00142 } 00143 00144 00146 // 00147 // -----> CProtocolProvider (implementation) 00148 // 00150 CProtocolProvider* CProtocolProvider::NewLC() 00151 { 00152 CProtocolProvider* self=new(ELeave) CProtocolProvider; 00153 CleanupStack::PushL(self); 00154 self->ConstructL(); 00155 return self; 00156 }; 00157 00158 CProtocolProvider::~CProtocolProvider() 00159 { 00160 delete iProtocolUser; 00161 } 00162 00163 void CProtocolProvider::ConstructL() 00164 { 00165 iProtocolUser=CProtocolUser::NewL(); 00166 } 00167 00168 void CProtocolProvider::CallProtocolUser() 00169 { 00170 // Call the protocol user to do some work 00171 _LIT(KTxtCallProtUser,"CProtocolProvider calling protocol user\n"); 00172 console->Printf(KTxtCallProtUser); 00173 iProtocolUser->DoSomething(this); 00174 // pass ourselves, disguised as our mixin 00175 // protocol base, to the protocol user 00176 } 00177 00178 void CProtocolProvider::HandleEvent(TInt aEventCode) 00179 { 00180 // A concrete implementation of the abstract protocol. 00181 // Handle an event in the protocol user 00182 _LIT(KFormat1,"CProtocolProvider handling event %d\n"); 00183 console->Printf(KFormat1,aEventCode); 00184 } 00185 00186 00188 // 00189 // Do the example 00190 // 00192 LOCAL_C void doExampleL() 00193 { 00194 // show use of mixin with simple class 00195 CProtocolProvider* simpleProvider=CProtocolProvider::NewLC(); 00196 // call protocol user 00197 simpleProvider->CallProtocolUser(); 00198 // Remove simpleProvider from cleanup stack and destroy 00199 CleanupStack::PopAndDestroy(); 00200 }
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.