|
1 /* |
|
2 * Copyright (c) 1999-2001 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 MEVENTS_H |
|
20 #define MEVENTS_H |
|
21 |
|
22 #include <jutils.h> |
|
23 |
|
24 #include "logger.h" |
|
25 |
|
26 /** |
|
27 * Callback interface for event server thread. |
|
28 */ |
|
29 class MEventServer |
|
30 { |
|
31 public: |
|
32 |
|
33 /** |
|
34 * Event server started. |
|
35 */ |
|
36 virtual void Started(TInt aError, RServer2 server) = 0; |
|
37 |
|
38 /** |
|
39 * Argument for event server thread. |
|
40 */ |
|
41 virtual TAny* Arg() const = 0; |
|
42 }; |
|
43 |
|
44 class CJavaEventServer; |
|
45 class TJavaEventServer |
|
46 { |
|
47 friend class CJavaEventServer; |
|
48 public: |
|
49 IMPORT_C static TJavaEventServer NewL(const TDesC& aName, TThreadFunction aServerThread=NULL, TAny* aServerArg=NULL); |
|
50 IMPORT_C static TJavaEventServer NewL(const TDesC& aName, TThreadFunction aServerThread, TAny* aServerArg, TUint aStackSize, RAllocator* aHeap = 0); |
|
51 |
|
52 inline TJavaEventServer() |
|
53 {} |
|
54 inline TJavaEventServer(TInt aServer) |
|
55 :iServer(JavaUnhand<CJavaEventServer>(aServer)) |
|
56 {} |
|
57 inline CJavaEventServer* operator->() const |
|
58 { |
|
59 return iServer; |
|
60 } |
|
61 static inline TInt Handle(CJavaEventServer* aServer) |
|
62 { |
|
63 return JavaMakeHandle(aServer); |
|
64 } |
|
65 inline TInt Handle() const |
|
66 { |
|
67 return Handle(iServer); |
|
68 } |
|
69 IMPORT_C void Shutdown(); |
|
70 private: |
|
71 inline TJavaEventServer(CJavaEventServer* aServer); |
|
72 private: |
|
73 CJavaEventServer* iServer; |
|
74 }; |
|
75 |
|
76 #pragma warning(disable: 4786) // Truncated browser info for long mangled names |
|
77 |
|
78 class CJavaEventBase; |
|
79 class CJavaEventSourceBase : public CBase |
|
80 { |
|
81 enum {KMaxExecuteArgumentSize=32}; |
|
82 static inline void CallCheck(TInt aSize) |
|
83 { // fault at run time for invalid JpServer calls |
|
84 #pragma warning(disable: 4127) |
|
85 if (aSize>KMaxExecuteArgumentSize) Panic(aSize); |
|
86 #pragma warning(default: 4127) |
|
87 } |
|
88 public: |
|
89 inline TInt Handle() const |
|
90 { |
|
91 return JavaMakeHandle(this); |
|
92 } |
|
93 inline jweak Peer() const |
|
94 { |
|
95 return iPeer; |
|
96 } |
|
97 inline TJavaEventServer Server() const |
|
98 { |
|
99 return iServer; |
|
100 } |
|
101 inline void Dispose(JNIEnv& aJni) |
|
102 { |
|
103 iDead=ETrue; |
|
104 Close(aJni); |
|
105 } |
|
106 inline TBool IsDisposed() const |
|
107 { |
|
108 return iDead; |
|
109 } |
|
110 TBool Open(); |
|
111 IMPORT_C void Close(JNIEnv& aJni); |
|
112 |
|
113 |
|
114 void OnVmClose(); |
|
115 |
|
116 template <class R,class T1,class T2,class T3,class T4,class T5,class T6,class T7,class T8> |
|
117 inline R Execute(R(*aFuncL)(T1,T2,T3,T4,T5,T6,T7,T8),T1 aT1,T2 aT2,T3 aT3,T4 aT4,T5 aT5,T6 aT6,T7 aT7,T8 aT8) |
|
118 { |
|
119 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)+sizeof(T4)+sizeof(T5)+sizeof(T6)+sizeof(T7)+sizeof(T8)); |
|
120 return (R)DoExecute((TAny*)aFuncL,aT1,aT2,aT3,aT4,aT5,aT6,aT7,aT8); |
|
121 } |
|
122 template <class R,class T1,class T2,class T3,class T4,class T5,class T6,class T7> |
|
123 inline R Execute(R(*aFuncL)(T1,T2,T3,T4,T5,T6,T7),T1 aT1,T2 aT2,T3 aT3,T4 aT4,T5 aT5,T6 aT6,T7 aT7) |
|
124 { |
|
125 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)+sizeof(T4)+sizeof(T5)+sizeof(T6)+sizeof(T7)); |
|
126 return (R)DoExecute((TAny*)aFuncL,aT1,aT2,aT3,aT4,aT5,aT6,aT7); |
|
127 } |
|
128 template <class R,class T1,class T2,class T3,class T4,class T5,class T6> |
|
129 inline R Execute(R(*aFuncL)(T1,T2,T3,T4,T5,T6),T1 aT1,T2 aT2,T3 aT3,T4 aT4,T5 aT5,T6 aT6) |
|
130 { |
|
131 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)+sizeof(T4)+sizeof(T5)+sizeof(T6)); |
|
132 return (R)DoExecute((TAny*)aFuncL,aT1,aT2,aT3,aT4,aT5,aT6); |
|
133 } |
|
134 template <class R,class T1,class T2,class T3,class T4,class T5> |
|
135 inline R Execute(R(*aFuncL)(T1,T2,T3,T4,T5),T1 aT1,T2 aT2,T3 aT3,T4 aT4,T5 aT5) |
|
136 { |
|
137 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)+sizeof(T4)+sizeof(T5)); |
|
138 return (R)DoExecute((TAny*)aFuncL,aT1,aT2,aT3,aT4,aT5); |
|
139 } |
|
140 template <class R,class T1,class T2,class T3,class T4> |
|
141 inline R Execute(R(*aFunc)(T1,T2,T3,T4),T1 aT1,T2 aT2,T3 aT3,T4 aT4) |
|
142 { |
|
143 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)+sizeof(T4)); |
|
144 return (R)DoExecute((TAny*)aFunc,aT1,aT2,aT3,aT4); |
|
145 } |
|
146 template <class R,class T1,class T2,class T3> |
|
147 inline R Execute(R(*aFunc)(T1,T2,T3),T1 aT1,T2 aT2,T3 aT3) |
|
148 { |
|
149 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)); |
|
150 return (R)DoExecute((TAny*)aFunc,aT1,aT2,aT3); |
|
151 } |
|
152 template <class R,class T1,class T2> |
|
153 inline R Execute(R(*aFunc)(T1,T2),T1 aT1,T2 aT2) |
|
154 { |
|
155 CallCheck(sizeof(T1)+sizeof(T2)); |
|
156 return (R)DoExecute((TAny*)aFunc,aT1,aT2); |
|
157 } |
|
158 template <class R,class T1> |
|
159 inline R Execute(R(*aFunc)(T1),T1 aT1) |
|
160 { |
|
161 CallCheck(sizeof(T1)); |
|
162 return (R)DoExecute((TAny*)aFunc,aT1); |
|
163 } |
|
164 template <class R> |
|
165 inline R Execute(R(*aFunc)()) |
|
166 { |
|
167 return (R)DoExecute((TAny*)aFunc); |
|
168 } |
|
169 |
|
170 template <class T1,class T2,class T3,class T4,class T5,class T6,class T7> |
|
171 inline void ExecuteV(void (*aFuncL)(T1,T2,T3,T4,T5,T6,T7),T1 aT1,T2 aT2,T3 aT3,T4 aT4,T5 aT5,T6 aT6,T7 aT7) |
|
172 { |
|
173 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)+sizeof(T4)+sizeof(T5)+sizeof(T6)+sizeof(T7)); |
|
174 DoExecute((TAny*)aFuncL,aT1,aT2,aT3,aT4,aT5,aT6,aT7); |
|
175 } |
|
176 template <class T1,class T2,class T3,class T4,class T5,class T6> |
|
177 inline void ExecuteV(void (*aFuncL)(T1,T2,T3,T4,T5,T6),T1 aT1,T2 aT2,T3 aT3,T4 aT4,T5 aT5,T6 aT6) |
|
178 { |
|
179 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)+sizeof(T4)+sizeof(T5)+sizeof(T6)); |
|
180 DoExecute((TAny*)aFuncL,aT1,aT2,aT3,aT4,aT5,aT6); |
|
181 } |
|
182 template <class T1,class T2,class T3,class T4,class T5> |
|
183 inline void ExecuteV(void (*aFuncL)(T1,T2,T3,T4,T5),T1 aT1,T2 aT2,T3 aT3,T4 aT4,T5 aT5) |
|
184 { |
|
185 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)+sizeof(T4)+sizeof(T5)); |
|
186 DoExecute((TAny*)aFuncL,aT1,aT2,aT3,aT4,aT5); |
|
187 } |
|
188 template <class T1,class T2,class T3,class T4> |
|
189 inline void ExecuteV(void (*aFunc)(T1,T2,T3,T4),T1 aT1,T2 aT2,T3 aT3,T4 aT4) |
|
190 { |
|
191 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)+sizeof(T4)); |
|
192 DoExecute((TAny*)aFunc,aT1,aT2,aT3,aT4); |
|
193 } |
|
194 template <class T1,class T2,class T3> |
|
195 inline void ExecuteV(void (*aFunc)(T1,T2,T3),T1 aT1,T2 aT2,T3 aT3) |
|
196 { |
|
197 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)); |
|
198 DoExecute((TAny*)aFunc,aT1,aT2,aT3); |
|
199 } |
|
200 template <class T1,class T2> |
|
201 inline void ExecuteV(void (*aFunc)(T1,T2),T1 aT1,T2 aT2) |
|
202 { |
|
203 CallCheck(sizeof(T1)+sizeof(T2)); |
|
204 DoExecute((TAny*)aFunc,aT1,aT2); |
|
205 } |
|
206 template <class T1> |
|
207 inline void ExecuteV(void (*aFunc)(T1),T1 aT1) |
|
208 { |
|
209 CallCheck(sizeof(T1)); |
|
210 DoExecute((TAny*)aFunc,aT1); |
|
211 } |
|
212 inline void ExecuteV(void (*aFunc)()) |
|
213 { |
|
214 DoExecute((TAny*)aFunc); |
|
215 } |
|
216 |
|
217 template <class T1,class T2,class T3,class T4,class T5,class T6,class T7,class T8,class T9> |
|
218 inline TInt ExecuteTrap(void (*aFuncL)(T1,T2,T3,T4,T5,T6,T7,T8,T9),T1 aT1,T2 aT2,T3 aT3,T4 aT4,T5 aT5,T6 aT6,T7 aT7,T8 aT8,T9 aT9) |
|
219 { |
|
220 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)+sizeof(T4)+sizeof(T5)+sizeof(T6)+sizeof(T7)+sizeof(T8)+sizeof(T9)); |
|
221 return DoExecuteTrap((TAny*)aFuncL,aT1,aT2,aT3,aT4,aT5,aT6,aT7,aT8,aT9); |
|
222 } |
|
223 template <class T1,class T2,class T3,class T4,class T5,class T6,class T7,class T8> |
|
224 inline TInt ExecuteTrap(void (*aFuncL)(T1,T2,T3,T4,T5,T6,T7,T8),T1 aT1,T2 aT2,T3 aT3,T4 aT4,T5 aT5,T6 aT6,T7 aT7,T8 aT8) |
|
225 { |
|
226 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)+sizeof(T4)+sizeof(T5)+sizeof(T6)+sizeof(T7)+sizeof(T8)); |
|
227 return DoExecuteTrap((TAny*)aFuncL,aT1,aT2,aT3,aT4,aT5,aT6,aT7,aT8); |
|
228 } |
|
229 template <class T1,class T2,class T3,class T4,class T5,class T6,class T7> |
|
230 inline TInt ExecuteTrap(void (*aFuncL)(T1,T2,T3,T4,T5,T6,T7),T1 aT1,T2 aT2,T3 aT3,T4 aT4,T5 aT5,T6 aT6,T7 aT7) |
|
231 { |
|
232 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)+sizeof(T4)+sizeof(T5)+sizeof(T6)+sizeof(T7)); |
|
233 return DoExecuteTrap((TAny*)aFuncL,aT1,aT2,aT3,aT4,aT5,aT6,aT7); |
|
234 } |
|
235 template <class T1,class T2,class T3,class T4,class T5,class T6> |
|
236 inline TInt ExecuteTrap(void (*aFuncL)(T1,T2,T3,T4,T5,T6),T1 aT1,T2 aT2,T3 aT3,T4 aT4,T5 aT5,T6 aT6) |
|
237 { |
|
238 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)+sizeof(T4)+sizeof(T5)+sizeof(T6)); |
|
239 return DoExecuteTrap((TAny*)aFuncL,aT1,aT2,aT3,aT4,aT5,aT6); |
|
240 } |
|
241 template <class T1,class T2,class T3,class T4,class T5> |
|
242 inline TInt ExecuteTrap(void (*aFuncL)(T1,T2,T3,T4,T5),T1 aT1,T2 aT2,T3 aT3,T4 aT4,T5 aT5) |
|
243 { |
|
244 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)+sizeof(T4)+sizeof(T5)); |
|
245 return DoExecuteTrap((TAny*)aFuncL,aT1,aT2,aT3,aT4,aT5); |
|
246 } |
|
247 template <class T1,class T2,class T3,class T4> |
|
248 inline TInt ExecuteTrap(void (*aFuncL)(T1,T2,T3,T4),T1 aT1,T2 aT2,T3 aT3,T4 aT4) |
|
249 { |
|
250 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)+sizeof(T4)); |
|
251 return DoExecuteTrap((TAny*)aFuncL,aT1,aT2,aT3,aT4); |
|
252 } |
|
253 template <class T1,class T2,class T3> |
|
254 inline TInt ExecuteTrap(void (*aFuncL)(T1,T2,T3),T1 aT1,T2 aT2,T3 aT3) |
|
255 { |
|
256 CallCheck(sizeof(T1)+sizeof(T2)+sizeof(T3)); |
|
257 return DoExecuteTrap((TAny*)aFuncL,aT1,aT2,aT3); |
|
258 } |
|
259 template <class T1,class T2> |
|
260 inline TInt ExecuteTrap(void (*aFuncL)(T1,T2),T1 aT1,T2 aT2) |
|
261 { |
|
262 CallCheck(sizeof(T1)+sizeof(T2)); |
|
263 return DoExecuteTrap((TAny*)aFuncL,aT1,aT2); |
|
264 } |
|
265 template <class T1> |
|
266 inline TInt ExecuteTrap(void (*aFuncL)(T1),T1 aT1) |
|
267 { |
|
268 CallCheck(sizeof(T1)); |
|
269 return DoExecuteTrap((TAny*)aFuncL,aT1); |
|
270 } |
|
271 inline int ExecuteTrap(void (*aFuncL)()) |
|
272 { |
|
273 return DoExecuteTrap((TAny*)aFuncL); |
|
274 } |
|
275 protected: |
|
276 IMPORT_C CJavaEventSourceBase(); |
|
277 IMPORT_C ~CJavaEventSourceBase(); |
|
278 IMPORT_C void ConstructL(JNIEnv& aJni, jobject aPeer,TJavaEventServer aServer); |
|
279 |
|
280 IMPORT_C TBool DoPostEvent(CJavaEventBase* aEvent, TInt aPriority); |
|
281 |
|
282 IMPORT_C virtual TBool CheckEvent(CJavaEventBase* aEvent); |
|
283 private: |
|
284 IMPORT_C virtual void FinalizeSvr(); |
|
285 |
|
286 IMPORT_C virtual void FinalizeJni(JNIEnv& aJni); |
|
287 |
|
288 IMPORT_C TInt DoExecute(TAny* aFunc,...); |
|
289 IMPORT_C TInt DoExecuteTrap(TAny* aFuncL,...); |
|
290 static void DoFinalize(CJavaEventSourceBase* aThis); |
|
291 |
|
292 IMPORT_C static void Panic(TInt aPanic); |
|
293 public: |
|
294 class TConstructorBase |
|
295 { |
|
296 public: |
|
297 inline TInt GetHandle() |
|
298 { |
|
299 CleanupStack::Pop(); |
|
300 return iObject->Handle(); |
|
301 } |
|
302 protected: |
|
303 IMPORT_C TConstructorBase(CJavaEventSourceBase* aObject, JNIEnv& aJni); |
|
304 private: |
|
305 static void Disposer(TAny* aThis); |
|
306 protected: |
|
307 CJavaEventSourceBase* iObject; |
|
308 JNIEnv& iJni; |
|
309 }; |
|
310 private: |
|
311 jweak iPeer; |
|
312 TJavaEventServer iServer; |
|
313 TInt iRef; |
|
314 TBool iDead; |
|
315 }; |
|
316 |
|
317 #pragma warning(default: 4786) |
|
318 |
|
319 class CJavaEventBase : public CBase |
|
320 { |
|
321 friend class CJavaEventSourceBase; |
|
322 friend class CJavaEventServer; |
|
323 |
|
324 public: |
|
325 enum {EEventPriority = 0, ENotifyPriority = 1, ELastPriority = 1}; |
|
326 enum TDisposability { EDisposableEvent, EReusableEvent }; |
|
327 public: |
|
328 inline TBool IsDisposable() const |
|
329 { |
|
330 return iDisposableState == EDisposableEvent; |
|
331 } |
|
332 protected: |
|
333 CJavaEventBase(TDisposability aDisposable) |
|
334 : iDisposableState(aDisposable) {} |
|
335 |
|
336 public: |
|
337 inline CJavaEventSourceBase& Object() const |
|
338 { |
|
339 return *iObject; |
|
340 } |
|
341 |
|
342 void Run(JNIEnv& aJni); |
|
343 |
|
344 private: |
|
345 virtual void Dispatch(JNIEnv& aJni) =0; |
|
346 TSglQueLink iLink; |
|
347 CJavaEventSourceBase* iObject; |
|
348 TDisposability iDisposableState; |
|
349 }; |
|
350 |
|
351 template <class T> |
|
352 class CJavaEvent : public CJavaEventBase |
|
353 { |
|
354 protected: |
|
355 CJavaEvent(TDisposability aDisposable = EDisposableEvent) |
|
356 : CJavaEventBase(aDisposable) {} |
|
357 inline T& Object() const |
|
358 { |
|
359 return static_cast<T&>(CJavaEventBase::Object()); |
|
360 } |
|
361 }; |
|
362 |
|
363 template <class T> |
|
364 class CJavaEventSource : public CJavaEventSourceBase |
|
365 { |
|
366 protected: |
|
367 class TConstructor : public CJavaEventSourceBase::TConstructorBase |
|
368 { |
|
369 public: |
|
370 inline TConstructor(T* aObject,JNIEnv& aJni) |
|
371 :TConstructorBase(aObject,aJni) |
|
372 {} |
|
373 inline TConstructor(JNIEnv& aJni) |
|
374 :TConstructorBase(new(ELeave) T,aJni) |
|
375 {} |
|
376 inline T* operator->() const |
|
377 { |
|
378 return static_cast<T*>(iObject); |
|
379 } |
|
380 }; |
|
381 public: |
|
382 inline static T& Unhand(TInt aHandle) |
|
383 { |
|
384 return *JavaUnhand<T>(aHandle); |
|
385 } |
|
386 inline TBool PostEvent(CJavaEvent<T>* aEvent, TInt aPriority = CJavaEventBase::EEventPriority) |
|
387 { |
|
388 return DoPostEvent(aEvent, aPriority); |
|
389 } |
|
390 static TInt New(JNIEnv& aJni,jobject aPeer,TJavaEventServer aServer) |
|
391 { |
|
392 TRAPD(h,TConstructor self(aJni); self->ConstructL(aJni,aPeer,aServer); h=self.GetHandle()); |
|
393 return h; |
|
394 } |
|
395 }; |
|
396 |
|
397 #endif // MEVENTS_H |