PECengine/PresenceManager2/SrcCommon/RGenericObserverArrayBase.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2004 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:  Generic observer array.
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <E32std.h>
       
    20 #include "RGenericObserverArrayBase.h"
       
    21 
       
    22 
       
    23 // ================= MEMBER FUNCTIONS =======================
       
    24 // C++ default constructor can NOT contain any code, that
       
    25 // might leave.
       
    26 //
       
    27 RGenericObserverArrayBase::RGenericObserverArrayBase()
       
    28         : iNotifyLoopRunning( EFalse )
       
    29     {
       
    30     }
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // RGenericObserverArrayBase::Close()
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 void RGenericObserverArrayBase::Close()
       
    37     {
       
    38     RPointerArrayBase::Close();
       
    39     }
       
    40 
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // RGenericObserverArrayBase::AddObserver()
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 TInt RGenericObserverArrayBase::AddObserver( const TAny* aObserver )
       
    47     {
       
    48     if ( !aObserver )
       
    49         {
       
    50         return KErrArgument;
       
    51         }
       
    52 
       
    53     return RPointerArrayBase::Append( aObserver );
       
    54     }
       
    55 
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // RGenericObserverArrayBase::RemoveObserver()
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 TInt RGenericObserverArrayBase::RemoveObserver( const TAny* aObserver )
       
    62     {
       
    63     const TInt obsCount = RPointerArrayBase::Count();
       
    64     for ( TInt ii( 0 ); ii < obsCount; ii++ )
       
    65         {
       
    66         if ( At( ii ) == aObserver )
       
    67             {
       
    68             //remove / blank the first found and return
       
    69             if ( iNotifyLoopRunning )
       
    70                 {
       
    71                 RPointerArrayBase::At( ii ) = NULL;
       
    72                 }
       
    73             else
       
    74                 {
       
    75                 RPointerArrayBase::Remove( ii );
       
    76                 }
       
    77 
       
    78             return KErrNone;
       
    79             }
       
    80         }
       
    81 
       
    82     return KErrNotFound;
       
    83     }
       
    84 
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // RGenericObserverArrayBase::Count()
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 TInt RGenericObserverArrayBase::Count()
       
    91     {
       
    92     return RPointerArrayBase::Count();
       
    93     }
       
    94 
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // RGenericObserverArrayBase::NotifyObservers()
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void RGenericObserverArrayBase::NotifyObservers( MGenObserverNotifyMediatorBase& aNotifyMediator )
       
   101     {
       
   102     //Protect the observer handling against the observer
       
   103     //remove / adding during the notify run.
       
   104     iNotifyLoopRunning = ETrue;
       
   105 
       
   106     //Observers might be added during notify loop
       
   107     //==> use dynamic count
       
   108     TInt ii;
       
   109     for ( ii = 0; ii < Count(); ii++ )
       
   110         {
       
   111         TAny* obs = RPointerArrayBase::At( ii );
       
   112         if ( obs )
       
   113             {
       
   114             //observer exist, notify it
       
   115             TRAPD( err, aNotifyMediator.MediateNotifyL( obs ) );
       
   116             if ( ( err != KErrNone ) && RPointerArrayBase::At( ii ) )
       
   117                 {
       
   118                 //handle notify caused an error and the observer is still there
       
   119                 //==>report a error to it
       
   120                 aNotifyMediator.MediateNotifyError( obs, err );
       
   121                 }
       
   122             }
       
   123         }
       
   124 
       
   125     FinalizeObsArrayAfterNotifyLoop();
       
   126     iNotifyLoopRunning = EFalse;
       
   127     }
       
   128 
       
   129 
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // RGenericObserverArrayBase::NotifyErrorObservers()
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 void RGenericObserverArrayBase::NotifyErrorObservers( MGenObserverNotifyMediatorBase& aNotifyMediator,
       
   136                                                       TInt aError )
       
   137 
       
   138     {
       
   139     //Protect the observer handling against the observer
       
   140     //remove / adding during the notify run.
       
   141     iNotifyLoopRunning = ETrue;
       
   142 
       
   143     //Observers might be added during notify loop
       
   144     //==> use dynamic count
       
   145     TInt ii;
       
   146     for ( ii = 0; ii < Count(); ii++ )
       
   147         {
       
   148         TAny* obs = RPointerArrayBase::At( ii );
       
   149         if ( obs )
       
   150             {
       
   151             aNotifyMediator.MediateNotifyError( obs, aError );
       
   152             }
       
   153         }
       
   154 
       
   155     FinalizeObsArrayAfterNotifyLoop();
       
   156     iNotifyLoopRunning = EFalse;
       
   157     }
       
   158 
       
   159 
       
   160 
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // RGenericObserverArrayBase::IsNotifyLoopRunning()
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 TBool RGenericObserverArrayBase::IsNotifyLoopRunning() const
       
   167     {
       
   168     return iNotifyLoopRunning;
       
   169     }
       
   170 
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // RGenericObserverArrayBase::FinalizeObsArrayAfterNotifyLoop()
       
   174 // -----------------------------------------------------------------------------
       
   175 //
       
   176 void RGenericObserverArrayBase::FinalizeObsArrayAfterNotifyLoop()
       
   177     {
       
   178     //finalize the array
       
   179     for ( TInt ii = ( RPointerArrayBase::Count() - 1 ); ii >= 0; ii-- )
       
   180         {
       
   181         if ( RPointerArrayBase::At( ii ) == NULL )  // CSI: 64 #
       
   182             {
       
   183             RPointerArrayBase::Remove( ii );
       
   184             }
       
   185         }
       
   186     }
       
   187 
       
   188 
       
   189 //  End of File
       
   190