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 is derived 00023 // from a CProtocol class. 00024 // 00025 00026 00027 00028 #include "CommonFramework.h" 00029 00031 // 00032 // -----> CProtocol (definition) 00033 // 00034 // A protocol class for mixing in 00035 // 00037 class CProtocol : public CBase 00038 { 00039 public: 00040 virtual void HandleEvent(TInt aEventCode)=0; 00041 }; 00042 00043 00045 // 00046 // -----> CProtocolUser (definition) 00047 // 00048 // Define a protocol user which uses this protocol 00049 // 00051 class CProtocolUser : public CBase 00052 { 00053 public: 00054 // Construction 00055 static CProtocolUser* NewLC(); 00056 static CProtocolUser* NewL(); 00057 00058 // Destruction 00059 ~CProtocolUser(); 00060 00061 // Some function which uses a protocol 00062 void DoSomething(CProtocol* aProtocol); 00063 00064 protected: 00065 // Construction assistance 00066 void ConstructL(); 00067 }; 00068 00069 00071 // 00072 // -----> CProtocolProvider (definition) 00073 // 00074 // A simple class which uses the mixin 00075 // 00077 class CProtocolProvider : public CProtocol 00078 { 00079 public: 00080 // Construction 00081 static CProtocolProvider* NewLC(); 00082 00083 // Destruction 00084 ~CProtocolProvider(); 00085 00086 // Calls the protocol user 00087 void CallProtocolUser(); 00088 00089 // Implement the protocol (handles the protocol) 00090 void HandleEvent(TInt aEventCode); 00091 00092 protected: 00093 // Construction assistance 00094 void ConstructL(); 00095 00096 private: 00097 // data member defined by this class 00098 CProtocolUser* iProtocolUser; 00099 }; 00100 00101 00103 // 00104 // -----> CProtocolUser (implementation) 00105 // 00107 CProtocolUser* CProtocolUser::NewLC() 00108 { 00109 CProtocolUser* self=new(ELeave) CProtocolUser; 00110 CleanupStack::PushL(self); 00111 self->ConstructL(); 00112 return self; 00113 } 00114 00115 CProtocolUser* CProtocolUser::NewL() 00116 { 00117 CProtocolUser* self=NewLC(); 00118 CleanupStack::Pop(); 00119 return self; 00120 } 00121 00122 CProtocolUser::~CProtocolUser() 00123 { 00124 } 00125 00126 void CProtocolUser::ConstructL() 00127 { 00128 } 00129 00130 void CProtocolUser::DoSomething(CProtocol* aProtocol) 00131 { 00132 // Do something that requires a protocol 00133 _LIT(KTxtExtSystemDoing,"External system doing something\n"); 00134 console->Printf(KTxtExtSystemDoing); 00135 _LIT(KTxtInvokingProtocol,"invoking protocol - event 3\n"); 00136 console->Printf(KTxtInvokingProtocol); 00137 // Handle an event 00138 aProtocol->HandleEvent(3); 00139 } 00140 00141 00143 // 00144 // -----> CProtocolProvider (implementation) 00145 // 00147 CProtocolProvider* CProtocolProvider::NewLC() 00148 { 00149 CProtocolProvider* self=new(ELeave) CProtocolProvider; 00150 CleanupStack::PushL(self); 00151 self->ConstructL(); 00152 return self; 00153 }; 00154 00155 CProtocolProvider::~CProtocolProvider() 00156 { 00157 delete iProtocolUser; 00158 } 00159 00160 void CProtocolProvider::ConstructL() 00161 { 00162 iProtocolUser=CProtocolUser::NewL(); 00163 } 00164 00165 void CProtocolProvider::CallProtocolUser() 00166 { 00167 // Call the protocol user to do some work 00168 _LIT(KTxtCallProtUser,"CProtocolProvider calling protocol user\n"); 00169 console->Printf(KTxtCallProtUser); 00170 iProtocolUser->DoSomething(this); 00171 00172 // pass ourselves, disguised as our (unique) 00173 // base class, so the protocol can be called 00174 // back by the protocol user 00175 } 00176 00177 void CProtocolProvider::HandleEvent(TInt aEventCode) 00178 { 00179 // A concrete implementation of the abstract protocol. 00180 // Handle an event in the protocol user 00181 _LIT(KFormat1,"CProtocolProvider handling event %d\n"); 00182 console->Printf(KFormat1,aEventCode); 00183 } 00184 00185 00187 // 00188 // Do the example 00189 // 00191 LOCAL_C void doExampleL() 00192 { 00193 // show use of mixin with simple class 00194 CProtocolProvider* simpleProvider=CProtocolProvider::NewLC(); 00195 // call protocol user 00196 simpleProvider->CallProtocolUser(); 00197 // Remove simpleProvider from cleanup stack and destroy 00198 CleanupStack::PopAndDestroy(); 00199 }
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.