phonebookengines/VirtualPhonebook/inc/VPbkSendEventUtility.h
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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 
       
    19 #ifndef VPBKENGUTILS_VPBKSENDEVENTUTILITY_H
       
    20 #define VPBKENGUTILS_VPBKSENDEVENTUTILITY_H
       
    21 
       
    22 
       
    23 // INCLUDES
       
    24 #include <e32cmn.h>
       
    25 
       
    26 // FORWARD DECLARATIONS
       
    27 
       
    28 namespace VPbkEng {
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // SendEventToObservers
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 template<typename RefParam1, typename Observer, typename NotifyFunc>
       
    35 void SendEventToObservers( RefParam1& aRefParam1,
       
    36         RPointerArray<Observer>& aObservers, NotifyFunc aNotifyFunc )
       
    37     {
       
    38     Observer* obs = NULL;
       
    39     for ( TInt i = aObservers.Count() - 1; i >= 0; --i )
       
    40         {
       
    41         if ( i < aObservers.Count() && obs != aObservers[i] )
       
    42             {
       
    43             obs = aObservers[i];
       
    44             (obs->*aNotifyFunc)( aRefParam1 );
       
    45             }
       
    46         }
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // SendEventToObservers
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 template<typename RefParam1, typename ValParam2, typename RefParam3,
       
    54     typename Observer, typename NotifyFunc>
       
    55 void SendEventToObservers( RefParam1& aRefParam1, ValParam2 aValParam2,
       
    56         RefParam3& aRefParam3, RPointerArray<Observer>& aObservers,
       
    57         NotifyFunc aNotifyFunc )
       
    58     {
       
    59     Observer* obs = NULL;
       
    60     for ( TInt i = aObservers.Count() - 1; i >= 0; --i )
       
    61         {
       
    62         if ( i < aObservers.Count() && obs != aObservers[i] )
       
    63             {
       
    64             obs = aObservers[i];
       
    65             (obs->*aNotifyFunc)( aRefParam1, aValParam2,
       
    66                     aRefParam3 );
       
    67             }
       
    68         }
       
    69     }
       
    70 
       
    71 template<typename RefParam1, typename ValParam2, typename RefParam3,
       
    72     typename Observer, typename NotifyFunc>
       
    73 void SendEventToObserversWhenNotDestroyed( RefParam1& aRefParam1, ValParam2 aValParam2,
       
    74         RefParam3& aRefParam3, RPointerArray<Observer>& aObservers,
       
    75         NotifyFunc aNotifyFunc, TBool& aDestroy )
       
    76     {
       
    77     Observer* obs = NULL;
       
    78     for ( TInt i = aObservers.Count() - 1; !aDestroy && i >= 0; --i )
       
    79         {
       
    80         if ( i < aObservers.Count() && obs != aObservers[i] )
       
    81             {
       
    82             obs = aObservers[i];
       
    83             (obs->*aNotifyFunc)( aRefParam1, aValParam2,
       
    84                     aRefParam3 );
       
    85             }
       
    86         }
       
    87     }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CopyPointerArrayL
       
    91 // -----------------------------------------------------------------------------
       
    92 //
       
    93 template<typename ObjectTypeName>
       
    94 void CopyPointerArrayL( RPointerArray<ObjectTypeName>& aSource,
       
    95         RPointerArray<ObjectTypeName>& aTarget )
       
    96     {
       
    97     const TInt count = aSource.Count();
       
    98     for ( TInt i = 0; i < count; ++i )
       
    99         {
       
   100         aTarget.AppendL( aSource[i] );
       
   101         }
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // SendViewEventToObserversL
       
   106 // See and use SendViewEventToObservers if possible
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 template<typename NotifyFunc, typename Observer, typename RefParam1>
       
   110 void SendViewEventToObserversL( RefParam1& aRefParam1,
       
   111         RPointerArray<Observer>& aObservers, NotifyFunc aNotifyFunc )
       
   112     {
       
   113     RPointerArray<Observer> tmp;
       
   114     CleanupClosePushL( tmp );
       
   115 
       
   116     CopyPointerArrayL( aObservers, tmp );
       
   117 
       
   118     const TInt count = tmp.Count();
       
   119     for ( TInt i = count - 1; i >= 0; --i )
       
   120         {
       
   121         if ( aObservers.Find( tmp[i] ) != KErrNotFound )
       
   122             {
       
   123             (tmp[i]->*aNotifyFunc)( aRefParam1 );
       
   124             }
       
   125         }
       
   126 
       
   127     CleanupStack::PopAndDestroy(); // tmp
       
   128     }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // SendViewEventToObserversL
       
   132 // See and use SendViewEventToObservers if possible
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 
       
   136 template<typename NotifyFunc, typename Observer, typename RefParam1,
       
   137     typename ValParam2, typename RefParam3>
       
   138 void SendViewEventToObserversL( RefParam1& aRefParam1, ValParam2 aValParam2,
       
   139         RefParam3& aRefParam3, RPointerArray<Observer>& aObservers,
       
   140         NotifyFunc aNotifyFunc )
       
   141     {
       
   142     RPointerArray<Observer> tmp;
       
   143     CleanupClosePushL( tmp );
       
   144 
       
   145     CopyPointerArrayL( aObservers, tmp );
       
   146 
       
   147     const TInt count = tmp.Count();
       
   148     for ( TInt i = count - 1; i >= 0; --i )
       
   149         {
       
   150         if ( aObservers.Find( tmp[i] ) != KErrNotFound )
       
   151             {
       
   152             (tmp[i]->*aNotifyFunc)( aRefParam1, aValParam2, aRefParam3 );
       
   153             }
       
   154         }
       
   155 
       
   156     CleanupStack::PopAndDestroy(); // tmp
       
   157     }
       
   158 
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // SendViewEventToObservers
       
   162 //
       
   163 // Usage:
       
   164 // CMyView::HandleNativeViewReady()
       
   165 //     {
       
   166 //     ...
       
   167 //     SendViewEventToObservers( *this, iObservers,
       
   168 //          &MVPbkContactViewObserver::ContactViewReady,
       
   169 //          &MVPbkContactViewObserver::ContactViewError )
       
   170 //     }
       
   171 //
       
   172 // The function slightly bound to MVPbkContactViewBase::ContactViewError for
       
   173 // error handling. If that doesn't suite your needs then use
       
   174 // SendViewEventToObserversL and handle error yourself.
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 template<typename NotifyFunc, typename ErrorFunc, typename Observer,
       
   178     typename RefParam1>
       
   179 void SendViewEventToObservers( RefParam1& aRefParam1,
       
   180         RPointerArray<Observer>& aObservers, NotifyFunc aNotifyFunc,
       
   181         ErrorFunc aErrorFunc )
       
   182     {
       
   183     TRAPD( result,
       
   184         SendViewEventToObserversL( aRefParam1, aObservers, aNotifyFunc ) );
       
   185     if ( result != KErrNone )
       
   186         {
       
   187         TBool errorNotified = EFalse;
       
   188         SendEventToObservers( aRefParam1, result, errorNotified,
       
   189             aObservers, aErrorFunc );
       
   190         }
       
   191     }
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 // SendViewEventToObservers
       
   195 //
       
   196 // Usage:
       
   197 // CMyView::HandleNativeViewContactAdded(TInt aIndex, TContactId aId)
       
   198 //     {
       
   199 //     ...
       
   200 //     SendViewEventToObservers( *this, aIndex, *iLink, iObservers,
       
   201 //          &MVPbkContactViewObserver::ContactAddedToView,
       
   202 //          &MVPbkContactViewObserver::ContactViewError )
       
   203 //     }
       
   204 //
       
   205 // The function slightly bound to MVPbkContactViewBase::ContactViewError for
       
   206 // error handling. If that doesn't suite your needs then use
       
   207 // SendViewEventToObserversL and handle error yourself.
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 template<typename NotifyFunc, typename ErrorFunc, typename Observer,
       
   211     typename RefParam1, typename ValParam2, typename RefParam3>
       
   212 void SendViewEventToObservers( RefParam1& aRefParam1, ValParam2 aValParam2,
       
   213         RefParam3& aRefParam3, RPointerArray<Observer>& aObservers,
       
   214         NotifyFunc aNotifyFunc, ErrorFunc aErrorFunc )
       
   215     {
       
   216     TRAPD( result, SendViewEventToObserversL( aRefParam1, aValParam2,
       
   217             aRefParam3, aObservers, aNotifyFunc ) );
       
   218     if ( result != KErrNone )
       
   219         {
       
   220         TBool errorNotified = EFalse;
       
   221         SendEventToObservers( aRefParam1, result, errorNotified,
       
   222             aObservers, aErrorFunc );
       
   223         }
       
   224     }
       
   225 } // namespace VPbkEng
       
   226 
       
   227 
       
   228 #endif  // VPBKENGUTILS_VPBKSENDEVENTUTILITY_H
       
   229 // End of file
       
   230 
       
   231