examples/Basics/MClasses1/MClasses1.cpp

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: 
00028 Demonstrate use of M classes, or mixins - the
00029 only use of multiple inheritance that has been
00030 sanctioned by the E32 architecture team
00031 This example shows how mixins can be used to
00032 pass some protocol, and an associated object, from
00033 a protocol provider to an protocol user.  The user
00034 is not supposed to know everything about the provider,
00035 only about the protocol it's interested in.
00036 In this specific example, the provider is derived 
00037 from a CProtocol class.
00038 */
00039 
00040 
00041 
00042 
00043 #include "CommonFramework.h"
00044 
00046 //
00047 // -----> CProtocol (definition)
00048 //
00049 // A protocol class for mixing in
00050 //
00052 class CProtocol : public CBase
00053         {
00054 public:
00055         virtual void HandleEvent(TInt aEventCode)=0;
00056         };
00057 
00058 
00060 //
00061 // -----> CProtocolUser (definition)
00062 //
00063 // Define a protocol user which uses this protocol
00064 //
00066 class CProtocolUser : public CBase
00067         {
00068 public:
00069           // Construction
00070         static CProtocolUser* NewLC();
00071         static CProtocolUser* NewL();
00072 
00073           // Destruction
00074         ~CProtocolUser();
00075 
00076           // Some function which uses a protocol
00077         void DoSomething(CProtocol* aProtocol);
00078 
00079 protected:
00080           // Construction assistance
00081         void ConstructL();
00082         };
00083 
00084 
00086 //
00087 // -----> CProtocolProvider (definition)
00088 //
00089 // A simple class which uses the mixin
00090 //
00092 class CProtocolProvider : public CProtocol
00093         {
00094 public:
00095           // Construction
00096         static CProtocolProvider* NewLC();
00097 
00098           // Destruction
00099         ~CProtocolProvider();
00100 
00101           // Calls the protocol user
00102         void CallProtocolUser();
00103 
00104           // Implement the protocol (handles the protocol)
00105         void HandleEvent(TInt aEventCode);
00106 
00107 protected:
00108           // Construction assistance
00109         void ConstructL();
00110 
00111 private:
00112           // data member defined by this class
00113         CProtocolUser* iProtocolUser;
00114         };
00115 
00116 
00118 //
00119 // -----> CProtocolUser (implementation)
00120 //
00122 CProtocolUser* CProtocolUser::NewLC()
00123         {
00124         CProtocolUser* self=new(ELeave) CProtocolUser;
00125         CleanupStack::PushL(self);
00126         self->ConstructL();
00127         return self;
00128         }
00129 
00130 CProtocolUser* CProtocolUser::NewL()
00131         {
00132         CProtocolUser* self=NewLC();
00133         CleanupStack::Pop();
00134         return self;
00135         }
00136 
00137 CProtocolUser::~CProtocolUser()
00138         {
00139         }
00140 
00141 void CProtocolUser::ConstructL()
00142         {
00143         }
00144 
00145 void CProtocolUser::DoSomething(CProtocol* aProtocol)
00146         {
00147           // Do something that requires a protocol
00148         _LIT(KTxtExtSystemDoing,"External system doing something\n");
00149         console->Printf(KTxtExtSystemDoing);
00150         _LIT(KTxtInvokingProtocol,"invoking protocol - event 3\n");
00151         console->Printf(KTxtInvokingProtocol);
00152           // Handle an event
00153         aProtocol->HandleEvent(3);
00154         }
00155 
00156 
00158 //
00159 // -----> CProtocolProvider (implementation)
00160 //
00162 CProtocolProvider* CProtocolProvider::NewLC()
00163         {
00164         CProtocolProvider* self=new(ELeave) CProtocolProvider;
00165         CleanupStack::PushL(self);
00166         self->ConstructL();
00167         return self;
00168         };
00169 
00170 CProtocolProvider::~CProtocolProvider()
00171         {
00172         delete iProtocolUser;
00173         }
00174 
00175 void CProtocolProvider::ConstructL()
00176         {
00177         iProtocolUser=CProtocolUser::NewL();
00178         }
00179 
00180 void CProtocolProvider::CallProtocolUser()
00181         { 
00182           // Call the protocol user to do some work
00183         _LIT(KTxtCallProtUser,"CProtocolProvider calling protocol user\n");
00184         console->Printf(KTxtCallProtUser);
00185         iProtocolUser->DoSomething(this);
00186 
00187                         // pass ourselves, disguised as our (unique)
00188                         // base class, so the protocol can be called
00189                         // back by the protocol user
00190         }
00191 
00192 void CProtocolProvider::HandleEvent(TInt aEventCode)
00193         { 
00194           // A concrete implementation of the abstract protocol.
00195           // Handle an event in the protocol user
00196         _LIT(KFormat1,"CProtocolProvider handling event %d\n");
00197         console->Printf(KFormat1,aEventCode);
00198         }
00199 
00200 
00202 //
00203 // Do the example
00204 //
00206 LOCAL_C void doExampleL()
00207     {
00208           // show use of mixin with simple class
00209         CProtocolProvider* simpleProvider=CProtocolProvider::NewLC();
00210           // call protocol user
00211         simpleProvider->CallProtocolUser();
00212           // Remove simpleProvider from cleanup stack and destroy
00213         CleanupStack::PopAndDestroy();
00214         }

Generated by  doxygen 1.6.2