|
1 /* |
|
2 * Copyright (c) 2004,2006 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: Active object wrapper |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #ifndef C_CAOASYNCWRAPPER_H |
|
21 #define C_CAOASYNCWRAPPER_H |
|
22 |
|
23 // INCLUDE FILES |
|
24 #include <e32base.h> |
|
25 |
|
26 // LOCAL DEFINITIONS |
|
27 |
|
28 /** |
|
29 * Panic category for AsyncWrapper |
|
30 */ |
|
31 _LIT( KPanicCategory, "AsyncWrapper" ); |
|
32 |
|
33 /** |
|
34 * Panic reason for AsyncWrapper |
|
35 */ |
|
36 const TInt KReason = 0; |
|
37 |
|
38 // CLASS DEFINITION |
|
39 |
|
40 /** |
|
41 * Active object wrapper for asynchronous requests. |
|
42 * |
|
43 * @lib PDPContextManager2.lib |
|
44 * @since S60 v3.1 |
|
45 */ |
|
46 template <class T> |
|
47 NONSHARABLE_CLASS( CAOAsyncWrapper ): public CActive |
|
48 { |
|
49 public: // Construction |
|
50 |
|
51 /** |
|
52 * Create a CAOAsyncWrapper object. |
|
53 * |
|
54 * @since S60 v3.1 |
|
55 * @param aPt2Object A ponter to object, for which the following |
|
56 * function pointers are called. |
|
57 * @param aRequestFpt A pointer to Request member function. |
|
58 * @param aRunLFpt A pointer to RunL member function. |
|
59 * @param aDoCancelFpt A pointer to DoCancel member function. |
|
60 * @param aRunErrorFpt A pointer to RunError member function. Note: |
|
61 * aRunErrorFpt is allowed to be NULL, |
|
62 in contrast to other parameters! |
|
63 * @return a pointer to the created instance of CAOAsyncWrapper. |
|
64 */ |
|
65 static CAOAsyncWrapper* NewL( T* aPt2Object, |
|
66 void ( T::*aRequestFpt )( TRequestStatus& ), |
|
67 void ( T::*aRunLFpt )( TInt ), |
|
68 void ( T::*aDoCancelFpt )(), |
|
69 TInt ( T::*aRunErrorFpt )( TInt ) ) |
|
70 { |
|
71 CAOAsyncWrapper* self = NewLC( aPt2Object, aRequestFpt, |
|
72 aRunLFpt, aDoCancelFpt, aRunErrorFpt ); |
|
73 CleanupStack::Pop(self); |
|
74 return self; |
|
75 }; |
|
76 |
|
77 /** |
|
78 * Create a CAOAsyncWrapper object. |
|
79 * Created instance is put on the CleanupStack. |
|
80 * |
|
81 * @since S60 v3.1 |
|
82 * @param aPt2Object A ponter to object, for which the following |
|
83 * function pointers are called. |
|
84 * @param aRequestFpt A pointer to Request member function. |
|
85 * @param aRunLFpt A pointer to RunL member function. |
|
86 * @param aDoCancelFpt A pointer to DoCancel member function. |
|
87 * @param aRunErrorFpt A pointer to RunError member function. Note: |
|
88 * aRunErrorFpt is allowed to be NULL, |
|
89 * in contrast to other parameters! |
|
90 * @return a pointer to the created instance of CAOAsyncWrapper. |
|
91 */ |
|
92 static CAOAsyncWrapper* NewLC( T* aPt2Object, |
|
93 void ( T::*aRequestFpt )( TRequestStatus& ), |
|
94 void ( T::*aRunLFpt )( TInt ), |
|
95 void ( T::*aDoCancelFpt )(), |
|
96 TInt ( T::*aRunErrorFpt )( TInt ) ) |
|
97 { |
|
98 CAOAsyncWrapper* self = new ( ELeave ) CAOAsyncWrapper( aPt2Object, |
|
99 aRequestFpt, |
|
100 aRunLFpt, |
|
101 aDoCancelFpt, |
|
102 aRunErrorFpt ); |
|
103 CleanupStack::PushL( self ); |
|
104 self->ConstructL(); |
|
105 return self; |
|
106 }; |
|
107 |
|
108 /** |
|
109 * Destructor. Destroy the object and release all memory objects. |
|
110 * Active object is also canceled. |
|
111 * |
|
112 * @since S60 v3.1 |
|
113 */ |
|
114 virtual ~CAOAsyncWrapper() |
|
115 { |
|
116 Cancel(); |
|
117 }; |
|
118 |
|
119 |
|
120 public: // New methods |
|
121 |
|
122 /** |
|
123 * Issue asynchronous request. |
|
124 * Calls in construction given "request" function pointer, if |
|
125 * AO is not already active. |
|
126 * Note: IssueRequestL may leave only if iRequestFpt may leave! |
|
127 * |
|
128 * @since S60 v3.1 |
|
129 */ |
|
130 void IssueRequestL() |
|
131 { |
|
132 if ( !IsActive() ) |
|
133 { |
|
134 (*iPt2Object.*iRequestFpt)( iStatus ); |
|
135 SetActive(); |
|
136 } |
|
137 }; |
|
138 |
|
139 /** |
|
140 * Issue asynchronous request. |
|
141 * Calls in construction given "request" function pointer, if |
|
142 * AO is not already active. |
|
143 * Note: Non-leavable method, should be used only if |
|
144 * issue request method is also non-leavable. |
|
145 * |
|
146 * @since S60 v3.1 |
|
147 */ |
|
148 void IssueRequest() |
|
149 { |
|
150 if ( !IsActive() ) |
|
151 { |
|
152 (*iPt2Object.*iRequestFpt)( iStatus ); |
|
153 SetActive(); |
|
154 } |
|
155 }; |
|
156 |
|
157 |
|
158 private: // Construction |
|
159 |
|
160 /** |
|
161 * Constructor |
|
162 * |
|
163 * @since S60 v3.1 |
|
164 * @param aPt2Object A ponter to object, for which the following |
|
165 * function pointers are called. |
|
166 * @param aRequestFpt A pointer to Request member function. |
|
167 * @param aRunLFpt A pointer to RunL member function. |
|
168 * @param aDoCancelFpt A pointer to DoCancel member function. |
|
169 * @param aRunErrorFpt A pointer to RunError member function. Note: |
|
170 * aRunErrorFpt is allowed to be NULL, |
|
171 * in contrast to other parameters! |
|
172 */ |
|
173 CAOAsyncWrapper( T* aPt2Object, |
|
174 void ( T::*aRequestFpt )( TRequestStatus& ), |
|
175 void ( T::*aRunLFpt )( TInt ), |
|
176 void ( T::*aDoCancelFpt )(), |
|
177 TInt ( T::*aRunErrorFpt )( TInt ) ) : |
|
178 CActive( CActive::EPriorityStandard ), |
|
179 iPt2Object( aPt2Object ), |
|
180 iRequestFpt( aRequestFpt ), |
|
181 iRunLFpt( aRunLFpt ), |
|
182 iDoCancelFpt( aDoCancelFpt ), |
|
183 iRunErrorFpt( aRunErrorFpt ) |
|
184 { |
|
185 __ASSERT_ALWAYS( iPt2Object, User::Panic( KPanicCategory, KReason ) ); |
|
186 __ASSERT_ALWAYS( iRequestFpt,User::Panic( KPanicCategory, KReason ) ); |
|
187 __ASSERT_ALWAYS( iRunLFpt, User::Panic( KPanicCategory, KReason ) ); |
|
188 __ASSERT_ALWAYS( iDoCancelFpt, |
|
189 User::Panic( KPanicCategory, KReason ) ); |
|
190 CActiveScheduler::Add( this ); |
|
191 }; |
|
192 |
|
193 |
|
194 /** |
|
195 * Performs second phase construction of this object |
|
196 * |
|
197 * @since S60 v3.1 |
|
198 */ |
|
199 void ConstructL() |
|
200 { |
|
201 // Empty |
|
202 }; |
|
203 |
|
204 |
|
205 private: |
|
206 |
|
207 // from CActive |
|
208 |
|
209 /** |
|
210 * From CActive |
|
211 * Cancel outstanding request |
|
212 * |
|
213 * @since S60 v3.1 |
|
214 */ |
|
215 void DoCancel() |
|
216 { |
|
217 (*iPt2Object.*iDoCancelFpt)(); |
|
218 }; |
|
219 |
|
220 /** |
|
221 * From CActive |
|
222 * Process the completed request. |
|
223 * |
|
224 * @since S60 v3.1 |
|
225 */ |
|
226 void RunL() |
|
227 { |
|
228 (*iPt2Object.*iRunLFpt)( iStatus.Int() ); |
|
229 }; |
|
230 |
|
231 /** |
|
232 * From CActive |
|
233 * Handle a leave occurred in the RunL(). |
|
234 * Note: If RunError function pointer is not given in construction, |
|
235 * the leave error is to be propagated back to the active scheduler! |
|
236 * |
|
237 * @since S60 v3.1 |
|
238 * @param aError the error code of leave |
|
239 */ |
|
240 TInt RunError( TInt aError ) |
|
241 { |
|
242 if ( iRunErrorFpt ) |
|
243 { |
|
244 return (*iPt2Object.*iRunErrorFpt)( aError ); |
|
245 } |
|
246 else |
|
247 { |
|
248 return aError; |
|
249 } |
|
250 } |
|
251 |
|
252 |
|
253 private: // Member variables |
|
254 |
|
255 /** |
|
256 * ?description_of_pointer_member |
|
257 * Not own. |
|
258 */ |
|
259 T* iPt2Object; |
|
260 |
|
261 /** |
|
262 * ?description_of_pointer_member |
|
263 * Not own. |
|
264 */ |
|
265 void ( T::*iRequestFpt )( TRequestStatus& ); |
|
266 |
|
267 /** |
|
268 * ?description_of_pointer_member |
|
269 * Not own. |
|
270 */ |
|
271 void ( T::*iRunLFpt )( TInt ); |
|
272 |
|
273 /** |
|
274 * ?description_of_pointer_member |
|
275 * Not own. |
|
276 */ |
|
277 void ( T::*iDoCancelFpt )(); |
|
278 |
|
279 /** |
|
280 * ?description_of_pointer_member |
|
281 * Not own. |
|
282 */ |
|
283 TInt ( T::*iRunErrorFpt )( TInt ); // May be NULL |
|
284 |
|
285 }; |
|
286 |
|
287 #endif // C_CAOASYNCWRAPPER_H |