videofeeds/utils/inc/mtvobserverutils.inl
changeset 0 96612d01cf9f
equal deleted inserted replaced
-1:000000000000 0:96612d01cf9f
       
     1 /*
       
     2 * Copyright (c) 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 the License "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:    Helper functions and macros for handling multiple observers.*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 #ifndef _MTV_OBSERVER_UTILS
       
    21 #define _MTV_OBSERVER_UTILS
       
    22 
       
    23 namespace MtvObserverUtils
       
    24 {
       
    25     
       
    26 /**
       
    27  *  Struct for holding a bunch of observers and a semaphore for controlling observer removal.
       
    28  *
       
    29  *  @since Live TV UI v1.0
       
    30  */
       
    31 template <class ObserverType, template<class> class ContainerType = RPointerArray, class PointerType = ObserverType>
       
    32 struct RObserverData
       
    33     {
       
    34     // constructor for initializing the semaphore
       
    35     RObserverData() : iSemaphore(0), iShutdown(EFalse)
       
    36         {
       
    37         }
       
    38     /**
       
    39      * Initializes the container.
       
    40      * This function is currently just a placeholder, later
       
    41      * it can be used to add support for CBase-derived containers,
       
    42      * assuming that all clients call OpenL and Close when using
       
    43      * this class.
       
    44      */
       
    45     void OpenL()
       
    46         {
       
    47         }
       
    48     /**
       
    49      * Closes the container.
       
    50      */
       
    51     void Close()
       
    52         {
       
    53         iContainer.Close();
       
    54         iShutdown = ETrue;
       
    55         }
       
    56     // Container can be RPointerArray, RArray.. 
       
    57     // At the moment CBase-derived containers are not supported.
       
    58     ContainerType<ObserverType> iContainer;
       
    59     // the semaphore is used so that when it has a value greater than zero, observers
       
    60     // are not removed, but nulled in the container
       
    61     TInt iSemaphore;
       
    62     // pointer type member helps code using this object, the using code does not
       
    63     // need to know the actual observer type
       
    64     PointerType* iObserver;
       
    65     // Set to ETrue when the provider of notifications is shut down.
       
    66     TBool iShutdown;
       
    67     };
       
    68 
       
    69 /**
       
    70  * Removes NULL observers from a container.
       
    71  * @param aExistsNull Tells whether there are any NULL observers.
       
    72  * Checking this parameter inside this function makes invoking code simpler.
       
    73  * @param aObserverData Observer data object (usually a RObserverData).
       
    74  * This data object must contain a container of observers and a semaphore.
       
    75  */
       
    76 template <class ObserverDataType>
       
    77 static void RemoveNullObservers(TBool aExistsNull, ObserverDataType& aObserverData)
       
    78     {
       
    79     // semaphore must always be decremented 
       
    80     --aObserverData.iSemaphore;    
       
    81     if ( !aObserverData.iSemaphore && aExistsNull )
       
    82         {
       
    83         for (TInt i = aObserverData.iContainer.Count() - 1; i >= 0; --i)
       
    84             {
       
    85             if (aObserverData.iContainer[i] == NULL)
       
    86                 {
       
    87                 aObserverData.iContainer.Remove(i);
       
    88                 }
       
    89             }
       
    90         }
       
    91     }
       
    92     
       
    93 /**
       
    94  * Macro that invokes an observer function for all registered observers.
       
    95  * @param aNotification Observer function to call.
       
    96  * @param aObserverData Observer data object (usually a RObserverData).
       
    97  * This data object must contain a container of observers and a semaphore.
       
    98  */
       
    99 #define RUN_NOTIFY_LOOP(aNotification, aObserverData) \
       
   100 do \
       
   101     { \
       
   102     if ( !aObserverData.iShutdown )\
       
   103         {\
       
   104         ++aObserverData.iSemaphore; \
       
   105         TBool existsNull = EFalse; \
       
   106         const TInt count = aObserverData.iContainer.Count(); \
       
   107         for ( TInt i = 0; i < count; ++i ) \
       
   108             { \
       
   109             aObserverData.iObserver = aObserverData.iContainer[ i ]; \
       
   110             if ( aObserverData.iObserver ) \
       
   111                 { \
       
   112                 TRAP_IGNORE( aObserverData.iObserver->aNotification ); \
       
   113                 } \
       
   114             else \
       
   115                 { \
       
   116                 existsNull = ETrue; \
       
   117                 } \
       
   118             } \
       
   119         RemoveNullObservers(existsNull, \
       
   120             aObserverData); \
       
   121         }\
       
   122     } while (EFalse) \
       
   123 
       
   124 /**
       
   125  * Macro that invokes an observer function for all registered observers.
       
   126  * @param aNotification Observer function to call.
       
   127  * @param aObserverData Observer data object (usually a RObserverData).
       
   128  * @param aError On return, contains the possible first error code that happened.
       
   129  * This data object must contain a container of observers and a semaphore.
       
   130  */
       
   131 #define RUN_NOTIFY_LOOP_L(aNotification, aObserverData, aError) \
       
   132 do \
       
   133     { \
       
   134     aError = KErrNone; \
       
   135     TInt ___mtvObserverUtilsNotifyLoopError = KErrNone; \
       
   136     if ( !aObserverData.iShutdown )\
       
   137         {\
       
   138         ++aObserverData.iSemaphore; \
       
   139         TBool existsNull = EFalse; \
       
   140         const TInt count = aObserverData.iContainer.Count(); \
       
   141         for ( TInt i = 0; i < count; ++i ) \
       
   142             { \
       
   143             aObserverData.iObserver = aObserverData.iContainer[ i ]; \
       
   144             if ( aObserverData.iObserver ) \
       
   145                 { \
       
   146                 TRAP( ___mtvObserverUtilsNotifyLoopError, aObserverData.iObserver->aNotification ); \
       
   147                 if ( aError == KErrNone ) \
       
   148                     { \
       
   149                     aError = ___mtvObserverUtilsNotifyLoopError; \
       
   150                     } \
       
   151                 } \
       
   152             else \
       
   153                 { \
       
   154                 existsNull = ETrue; \
       
   155                 } \
       
   156             } \
       
   157         RemoveNullObservers(existsNull, \
       
   158             aObserverData); \
       
   159         }\
       
   160     } while (EFalse) \
       
   161 
       
   162 /**
       
   163  * Adds an observer to an observer data object.
       
   164  * @param aObserverData Data object to which an observer is added.
       
   165  * @param aObserver Observer to add.
       
   166  * @param aError Error code to use for Leave if adding fails.
       
   167  */
       
   168 template <class ObserverType, class ContainerType>
       
   169 static void AddObserverL(ContainerType& aObserverData, ObserverType& aObserver, TInt aError)
       
   170     {
       
   171     if (aObserverData.iShutdown)
       
   172         {
       
   173         return;
       
   174         }
       
   175     TInt pos = aObserverData.iContainer.Find( &aObserver );
       
   176     if (KErrNotFound != pos)
       
   177         {
       
   178         return;
       
   179         }
       
   180     TInt error = aObserverData.iContainer.Append( &aObserver );
       
   181     if (KErrNone != error)
       
   182         {
       
   183         User::Leave(aError); 
       
   184         }
       
   185     }
       
   186 
       
   187 /**
       
   188  * Removes an observer from an observer data object.
       
   189  * @param aObserverData Data object from which an observer is removed.
       
   190  * @param aObserver Observer to remove.
       
   191  */
       
   192 template <class ObserverType, class ContainerType>
       
   193 static void RemoveObserver( ContainerType& aObserverData, ObserverType& aObserver )
       
   194     {
       
   195     TInt pos = aObserverData.iContainer.Find( &aObserver );
       
   196     if ( KErrNotFound == pos  )
       
   197         {
       
   198         return;
       
   199         }
       
   200     if ( !aObserverData.iSemaphore )
       
   201         {
       
   202         // NotifyEventL is not part of call stack. Just remove.
       
   203         aObserverData.iContainer.Remove( pos );
       
   204         }
       
   205     else
       
   206         {
       
   207         // Simply set to NULL. NotifyEventL will remove observers.
       
   208         aObserverData.iContainer[ pos ] = NULL;
       
   209         }
       
   210     }
       
   211 
       
   212 } // end namespace MtvObserverUtils
       
   213 
       
   214 #endif //_MTV_OBSERVER_UTILS