startupservices/Startup/syserrcmd/tsrc/syserrcmdtest/inc/asyncrequesthandler.h
branchRCL_3
changeset 20 c2c61fdca848
parent 0 2e3d3ce01487
equal deleted inserted replaced
19:924385140d98 20:c2c61fdca848
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  * 
       
    14  * Description:
       
    15  *
       
    16  */
       
    17 
       
    18 #ifndef ASYNCREQUESTHANDLER_H
       
    19 #define ASYNCREQUESTHANDLER_H
       
    20 
       
    21 // SYSTEM INCLUDE FILES
       
    22 #include <e32base.h>
       
    23 
       
    24 // DATA TYPES
       
    25 _LIT( KPanicCat, "ASYNCREQUESTHANDLER" );
       
    26 
       
    27 /** Panic codes */
       
    28 enum TArhPanicCodes
       
    29     {
       
    30     EArhNone,
       
    31     EArhRequestPending
       
    32     };
       
    33 
       
    34 // CLASS DECLARATION
       
    35 
       
    36 /**
       
    37  * A template class for handling asynchronous requests.
       
    38  *
       
    39  * @lib None.
       
    40  * @since S60 TB9.2
       
    41  */
       
    42 template <class T>
       
    43 NONSHARABLE_CLASS( CAsyncRequestHandler ): public CActive
       
    44     {
       
    45 public:
       
    46     
       
    47     // TYPE DEFINTIONS
       
    48     
       
    49     /** HandleIssueRequest callback */
       
    50     typedef void ( T::*HandleIssueRequest )( TRequestStatus& );
       
    51     
       
    52     /** HandleRunL callback */
       
    53     typedef void ( T::*HandleRunL )( TInt );
       
    54     
       
    55     /** HandleRunError callback */
       
    56     typedef TInt ( T::*HandleRunError )( TInt );
       
    57     
       
    58     /** HandleDoCancel callback */
       
    59     typedef void ( T::*HandleDoCancel )();
       
    60     
       
    61     // DATA TYPES
       
    62     
       
    63     /** Request type */
       
    64     enum TAsyncRequestType
       
    65         {
       
    66         ERequestOneShot,
       
    67         ERequestContinuous
       
    68         };
       
    69     
       
    70 public:
       
    71 
       
    72     /**
       
    73      * Symbian two phased constructor.
       
    74      * 
       
    75      * @since S60 TB9.2
       
    76      * @param None.
       
    77      * @return CAsyncRequestHandler*
       
    78      */
       
    79     static CAsyncRequestHandler* NewL( T& aPtr,
       
    80         HandleIssueRequest aHandleIssueRequest,
       
    81         HandleRunL aHandleRunL,
       
    82         HandleRunError aHandleRunError,
       
    83         HandleDoCancel aHandleDoCancel,
       
    84         TAsyncRequestType aType = ERequestContinuous )
       
    85         {
       
    86         CAsyncRequestHandler* self = CAsyncRequestHandler::NewLC( aPtr,
       
    87             aHandleIssueRequest,
       
    88             aHandleRunL,
       
    89             aHandleRunError,
       
    90             aHandleDoCancel,
       
    91             aType );
       
    92         CleanupStack::Pop( self );
       
    93         return self;
       
    94         }
       
    95     
       
    96     /**
       
    97      * Symbian two phased constructor.
       
    98      * Instance is left in the cleanup stack.
       
    99      * 
       
   100      * @since S60 TB9.2
       
   101      * @param None.
       
   102      * @return CAsyncRequestHandler*
       
   103      */
       
   104     static CAsyncRequestHandler* NewLC( T& aPtr,
       
   105         HandleIssueRequest aHandleIssueRequest,
       
   106         HandleRunL aHandleRunL,
       
   107         HandleRunError aHandleRunError,
       
   108         HandleDoCancel aHandleDoCancel,
       
   109         TAsyncRequestType aType = ERequestContinuous )
       
   110         {
       
   111         CAsyncRequestHandler* self = new CAsyncRequestHandler( aPtr,
       
   112             aHandleIssueRequest,
       
   113             aHandleRunL,
       
   114             aHandleRunError,
       
   115             aHandleDoCancel,
       
   116             aType );
       
   117         CleanupStack::PushL( self );
       
   118         return self;
       
   119         }
       
   120 
       
   121     /**
       
   122      * C++ destructor.
       
   123      */
       
   124     virtual ~CAsyncRequestHandler()
       
   125         {
       
   126         Cancel();
       
   127         }
       
   128     
       
   129 public: // New methods
       
   130     
       
   131     /**
       
   132      * Issues a new request.
       
   133      * Panic will occur if there already is a request pending.
       
   134      * 
       
   135      * @since TB9.2
       
   136      * @param None.
       
   137      * @return None.
       
   138      */
       
   139     void IssueRequest()
       
   140         {
       
   141         __ASSERT_DEBUG( !IsActive(),
       
   142             User::Panic( KPanicCat, EArhRequestPending ) );
       
   143         
       
   144         // Call the HandleIssueRequest from the template class and set active
       
   145         ( iPtr.*iHandleIssueRequest )( iStatus );
       
   146         SetActive();
       
   147         }
       
   148 
       
   149 protected: // From base classes
       
   150     
       
   151     // @see CActive
       
   152     void RunL()
       
   153         {
       
   154         // Check result and issue request again
       
   155         TInt status = iStatus.Int();
       
   156         if( iType == ERequestContinuous )
       
   157             {
       
   158             IssueRequest();
       
   159             }
       
   160         
       
   161         // Call the HandleRunL from the template class
       
   162         ( iPtr.*iHandleRunL )( status );
       
   163         }
       
   164 
       
   165     // @see CActive
       
   166     TInt RunError( TInt aError )
       
   167         {
       
   168         // Call the HandleRunError from the template class
       
   169         TInt err = ( iPtr.*iHandleRunError )( aError );
       
   170         return err;
       
   171         }
       
   172 
       
   173     // @see CActive
       
   174     void DoCancel()
       
   175         {
       
   176         // Call the HandleDoCancel from the template class
       
   177         ( iPtr.*iHandleDoCancel )();
       
   178         }
       
   179 
       
   180 private:
       
   181 
       
   182     CAsyncRequestHandler( T& aPtr,
       
   183         HandleIssueRequest aHandleIssueRequest,
       
   184         HandleRunL aHandleRunL,
       
   185         HandleRunError aHandleRunError,
       
   186         HandleDoCancel aHandleDoCancel,
       
   187         TAsyncRequestType aType = ERequestContinuous ):
       
   188         CActive( CActive::EPriorityStandard ),
       
   189         iPtr( aPtr ),
       
   190         iHandleIssueRequest( aHandleIssueRequest ),
       
   191         iHandleRunL( aHandleRunL ),
       
   192         iHandleRunError( aHandleRunError ),
       
   193         iHandleDoCancel( aHandleDoCancel ),
       
   194         iType( aType )
       
   195         {
       
   196         CActiveScheduler::Add( this );
       
   197         }
       
   198 
       
   199 private: // Data
       
   200     
       
   201     /** Pointer to the template class */
       
   202     T& iPtr;
       
   203     
       
   204     /** HandleIssueRequest function pointer */
       
   205     HandleIssueRequest iHandleIssueRequest;
       
   206     
       
   207     /** HandleRunL function pointer */
       
   208     HandleRunL iHandleRunL;
       
   209     
       
   210     /** HandleRunError function pointer */
       
   211     HandleRunError iHandleRunError;
       
   212     
       
   213     /** HandleDoCancel function pointer */
       
   214     HandleDoCancel iHandleDoCancel;
       
   215     
       
   216     /** Request type */
       
   217     TAsyncRequestType iType;
       
   218     };
       
   219 
       
   220 #endif // ASYNCREQUESTHANDLER_H