|
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 TAny* aNotifyData ) |
|
102 { |
|
103 //Protect the observer handling against the observer |
|
104 //remove / adding during the notify run. |
|
105 iNotifyLoopRunning = ETrue; |
|
106 |
|
107 //Observers might be added during notify loop |
|
108 //==> use dynamic count |
|
109 TInt ii; |
|
110 for ( ii = 0; ii < Count(); ii++ ) |
|
111 { |
|
112 TAny* obs = RPointerArrayBase::At( ii ); |
|
113 if ( obs ) |
|
114 { |
|
115 //observer exist, notify it |
|
116 TRAPD( err, aNotifyMediator.MediateNotifyL( obs, aNotifyData ) ); |
|
117 if ( ( err != KErrNone ) && RPointerArrayBase::At( ii ) ) |
|
118 { |
|
119 //handle notify caused an error and the observer is still there |
|
120 //==>report a error to it |
|
121 aNotifyMediator.MediateNotifyError( obs, err ); |
|
122 } |
|
123 } |
|
124 } |
|
125 |
|
126 FinalizeObsArrayAfterNotifyLoop(); |
|
127 iNotifyLoopRunning = EFalse; |
|
128 } |
|
129 |
|
130 |
|
131 // ----------------------------------------------------------------------------- |
|
132 // RGenericObserverArrayBase::NotifyObserversFromError() |
|
133 // ----------------------------------------------------------------------------- |
|
134 // |
|
135 void RGenericObserverArrayBase::NotifyObserversFromError( |
|
136 MGenObserverNotifyMediatorBase& aNotifyMediator, |
|
137 TInt aError ) |
|
138 |
|
139 { |
|
140 //Protect the observer handling against the observer |
|
141 //remove / adding during the notify run. |
|
142 iNotifyLoopRunning = ETrue; |
|
143 |
|
144 //Observers might be added during notify loop |
|
145 //==> use dynamic count |
|
146 TInt ii; |
|
147 for ( ii = 0; ii < Count(); ii++ ) |
|
148 { |
|
149 TAny* obs = RPointerArrayBase::At( ii ); |
|
150 if ( obs ) |
|
151 { |
|
152 //observer exist, notify it |
|
153 aNotifyMediator.MediateError( obs, aError ); |
|
154 } |
|
155 } |
|
156 |
|
157 FinalizeObsArrayAfterNotifyLoop(); |
|
158 iNotifyLoopRunning = EFalse; |
|
159 } |
|
160 |
|
161 |
|
162 // ----------------------------------------------------------------------------- |
|
163 // RGenericObserverArrayBase::FinalizeObsArrayAfterNotifyLoop() |
|
164 // ----------------------------------------------------------------------------- |
|
165 // |
|
166 void RGenericObserverArrayBase::FinalizeObsArrayAfterNotifyLoop() |
|
167 { |
|
168 //finalize the array |
|
169 for ( TInt ii = ( RPointerArrayBase::Count() - 1 ); ii >= 0; ii-- ) |
|
170 { |
|
171 if ( !RPointerArrayBase::At( ii ) ) |
|
172 { |
|
173 RPointerArrayBase::Remove( ii ); |
|
174 } |
|
175 } |
|
176 } |
|
177 |
|
178 |
|
179 // End of File |
|
180 |