|
1 /* |
|
2 * Copyright (c) 2005-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: General definitions |
|
15 * |
|
16 */ |
|
17 |
|
18 #ifndef M2GGENERAL_H |
|
19 #define M2GGENERAL_H |
|
20 |
|
21 #include <e32base.h> |
|
22 #include <jutils.h> |
|
23 #include <swtfunctor.h> |
|
24 |
|
25 #if defined ( _DEBUG ) |
|
26 #if defined ( __WINSCW__ ) |
|
27 // #define M2G_DEBUG |
|
28 #endif // __WINSCW__ |
|
29 #endif // _DEBUG |
|
30 |
|
31 #ifdef M2G_DEBUG |
|
32 // #define M2G_INFO_POPUP |
|
33 #define M2G_STATISTIC_DEBUG |
|
34 #include <flogger.h> |
|
35 #include <jdebug.h> |
|
36 #ifdef M2G_STATISTIC_DEBUG |
|
37 #include <HAL.h> |
|
38 #include <hal_data.h> |
|
39 #endif // M2G_STATISTIC_DEBUG |
|
40 #endif // M2G_DEBUG |
|
41 |
|
42 #include "JcfAutoPtr.h"// cfcommon::auto_ptr<> |
|
43 #include "M2GNamespace.h" |
|
44 |
|
45 //#if defined( __cplusplus ) |
|
46 //extern "C" { |
|
47 //#endif |
|
48 |
|
49 M2G_NS_START |
|
50 |
|
51 // CONSTANTS |
|
52 // == ALPHA BLEND CONSTANTS == |
|
53 const TUint8 KFullOpaque = 0xFF; |
|
54 const TUint8 KFullTransparent = 0x00; |
|
55 |
|
56 // == ERROR TEXT CONSTANTS == |
|
57 _LIT8(KM2GEmptyString8, ""); |
|
58 _LIT(KM2GEmptyString, ""); |
|
59 _LIT(KGraphicsContextErrText, "Invalid M2G graphics context"); |
|
60 _LIT(KOffscreenErrText, "Invalid M2G offscreen bitmap"); |
|
61 _LIT(KSvgProxyPointerErrText, "Invalid SVG proxy pointer"); |
|
62 _LIT(KSvgEnginePointerErrText, "Invalid SVG engine pointer"); |
|
63 _LIT(KSvgTargetGraphicsPointerErrText, "Invalid target graphics pointer"); |
|
64 _LIT(KNotSupportedErrText, "Not supported"); |
|
65 |
|
66 // == ERROR CODE CONSTANTS == |
|
67 const TInt KM2GOk = KErrNone; |
|
68 const TInt KM2GNotOk = KErrGeneral; |
|
69 const TInt KM2GHandleNotOk = KErrBadHandle; |
|
70 const TInt KM2GArgNotOk = KErrArgument; |
|
71 const TInt KM2GSupportedNotOk = KErrNotSupported; |
|
72 const TInt KM2GMemoryNotOk = KErrNoMemory; |
|
73 |
|
74 // Own error codes |
|
75 const TInt KM2GIOException = -10; |
|
76 |
|
77 // Java exception class names |
|
78 _LIT8(KJavaExceptionClass, "java/lang/Exception"); |
|
79 _LIT8(KJavaIOExceptionClass, "java/io/IOException"); |
|
80 _LIT8(KJavaOutOfMemoryErrorClass, "java/lang/OutOfMemoryError"); |
|
81 _LIT8(KJavaNullPointerException, "java/lang/NullPointerException"); |
|
82 |
|
83 // == SVG CONSTANTS == |
|
84 const TInt KM2GRestartAttribute = 114; // M2GSVGConstants.AT_RESTART |
|
85 const TInt KM2GRestartNever = 471; // M2GSVGConstants.RESTART_NEVER |
|
86 const TInt KM2GRestartWhenNotActive = 472; // M2GSVGConstants.RESTART_WHENNOTACTIVE |
|
87 |
|
88 // == HANDLE MACROS == |
|
89 #define M2G_INVALID_HANDLE /**/ 0 |
|
90 |
|
91 // FORWARD DECLARATIONS |
|
92 class RWriteStream; |
|
93 |
|
94 // FUNCTION PROTOTYPES |
|
95 |
|
96 // CLASS DECLARATION |
|
97 /** |
|
98 * General static methods |
|
99 * @lib - |
|
100 * @series Series S60 3.0 |
|
101 */ |
|
102 class M2GGeneral |
|
103 { |
|
104 public: // STATIC METHODS |
|
105 /** |
|
106 * Check if a handle is valid |
|
107 * @since Series S60 3.0 |
|
108 * @param aHandle |
|
109 * @return ETrue if a handle is valid. |
|
110 */ |
|
111 template<typename T> |
|
112 inline static TBool IsHandleValid(const T& aHandle) |
|
113 { |
|
114 return ((aHandle == M2G_INVALID_HANDLE) ? EFalse : ETrue); |
|
115 } |
|
116 |
|
117 /** |
|
118 * Returns min |
|
119 * @since Series S60 3.0 |
|
120 * @param a Left parameter |
|
121 * @param b Right parameter |
|
122 * @return Returns min |
|
123 */ |
|
124 template<typename T> |
|
125 inline static T Min(const T& a, const T& b) |
|
126 { |
|
127 return ((a) <= (b) ? (a) : (b)); |
|
128 } |
|
129 |
|
130 |
|
131 /** |
|
132 * Returns max |
|
133 * @since Series S60 3.0 |
|
134 * @param a Left parameter |
|
135 * @param b Right parameter |
|
136 * @return Returns max |
|
137 */ |
|
138 template<typename T> |
|
139 inline static T Max(const T& a, const T& b) |
|
140 { |
|
141 return ((a) >= (b) ? (a) : (b)); |
|
142 } |
|
143 /** |
|
144 * Instantiates an Java exception object with the message specified by |
|
145 * message, and causes that exception to be thrown in Java side. A thrown |
|
146 * exception will be pending in the current thread, but does not |
|
147 * immediately disrupt native code execution. |
|
148 * @param aEnv pointer to JNI environment |
|
149 * @param aName The name of a java.lang.Throwable class. |
|
150 * @param aMsg The message used to construct the java.lang.Throwable object. |
|
151 */ |
|
152 static void RaiseException( |
|
153 JNIEnv* aEnv, |
|
154 const TDesC8& aName, |
|
155 const TUint8* aMsg = (STATIC_CAST(const TDesC8&, KM2GEmptyString8)).Ptr()); |
|
156 |
|
157 /** |
|
158 * Instantiates an Java exception object specified by error code, |
|
159 * and causes that exception to be thrown in Java side. |
|
160 * @param aEnv pointer to JNI environment |
|
161 * @param aErrorCode Error code. |
|
162 * @return Value passed in is returned if not an error. |
|
163 */ |
|
164 static TInt CheckErrorCode(JNIEnv* aEnv, TInt aErrorCode); |
|
165 |
|
166 /** |
|
167 * If handle is invalid instantiates an Java exception object, |
|
168 * and causes that exception to be thrown in Java side. |
|
169 * @param aEnv pointer to JNI environment |
|
170 * @param aHandle Handle. |
|
171 * @return Handle passed in is returned if it is not invalid. |
|
172 */ |
|
173 static TInt CheckHandle(JNIEnv* aEnv, TInt aHandle); |
|
174 |
|
175 /** |
|
176 * If handle is invalid or error code matches instantiates an |
|
177 * Java exception object, and causes that exception to be thrown in Java side. |
|
178 * @param aEnv pointer to JNI environment |
|
179 * @param aErrorCode |
|
180 * @param aHandle Handle. |
|
181 * @param aErrorResult Result that is returned if error. |
|
182 * @return Handle passed in is returned if no error otherwise the |
|
183 * .aErrorResult is returned. |
|
184 */ |
|
185 static TInt CheckErrorCodeAndHandle( |
|
186 JNIEnv* aEnv, TInt aErrorCode, TInt aHandle, TInt aErrorResult); |
|
187 |
|
188 /** |
|
189 * Shows pop up dialog on the emulator |
|
190 * @since Series S60 3.1 |
|
191 * @param aLine1 |
|
192 * @param aLine2 |
|
193 */ |
|
194 #ifdef M2G_INFO_POPUP |
|
195 static void PopupInfo( |
|
196 const TDesC& aLine1, |
|
197 const TDesC& aLine2); |
|
198 #endif // M2G_INFO_POPUP |
|
199 |
|
200 |
|
201 /** |
|
202 * Data array wrapper. |
|
203 * @series Series S60 3.1 |
|
204 */ |
|
205 template <typename T> |
|
206 class TDataArrayPtr |
|
207 { |
|
208 |
|
209 public: // METHODS |
|
210 /** |
|
211 * Ctor |
|
212 */ |
|
213 TDataArrayPtr(T* aData = 0, TInt aSize = 0) : iData(aData), iSize(aSize) {} |
|
214 |
|
215 /** |
|
216 * Return pointer to a data |
|
217 * @return Pointer |
|
218 */ |
|
219 inline T* Begin() |
|
220 { |
|
221 return iData; |
|
222 } |
|
223 |
|
224 /** |
|
225 * Check if data is valid |
|
226 * @return ETrue if ok |
|
227 */ |
|
228 inline TBool IsValid() |
|
229 { |
|
230 return (((iData != NULL) && (iSize > 0)) ? ETrue : EFalse); |
|
231 } |
|
232 |
|
233 /** |
|
234 * Return data size |
|
235 * @return Length |
|
236 */ |
|
237 inline TInt Length() |
|
238 { |
|
239 return iSize; |
|
240 } |
|
241 |
|
242 /** |
|
243 * Operator [] const |
|
244 */ |
|
245 inline const T& operator[](TInt aIndex) const |
|
246 { |
|
247 return iData[ aIndex ]; |
|
248 } |
|
249 |
|
250 /** |
|
251 * Operator [] |
|
252 */ |
|
253 inline T& operator[](TInt aIndex) |
|
254 { |
|
255 return iData[ aIndex ]; |
|
256 } |
|
257 |
|
258 private: // METHODS |
|
259 |
|
260 /** |
|
261 * Copy ctor |
|
262 * @param aRhs Right side array |
|
263 */ |
|
264 TDataArrayPtr(const TDataArrayPtr& aRhs); |
|
265 |
|
266 /** |
|
267 * Assignment operator |
|
268 * @param aRhs Right side array |
|
269 * @return Array object |
|
270 */ |
|
271 TDataArrayPtr& operator=(const TDataArrayPtr& aRhs); |
|
272 |
|
273 public: // VARIABLES |
|
274 T* iData; |
|
275 TInt iSize; |
|
276 }; |
|
277 |
|
278 |
|
279 /** |
|
280 * Statistic info |
|
281 * @series Series S60 3.1 |
|
282 */ |
|
283 #ifdef M2G_STATISTIC_DEBUG |
|
284 class TM2GStatistic |
|
285 { |
|
286 public: // METHODS |
|
287 /** |
|
288 * Ctor |
|
289 */ |
|
290 TM2GStatistic(); |
|
291 |
|
292 /** |
|
293 * Reset |
|
294 */ |
|
295 void Reset(); |
|
296 |
|
297 /** |
|
298 * Print |
|
299 * @param aPrintElapsedTime Print also en elapsed time |
|
300 */ |
|
301 void Print(TBool aPrintElapsedTime = EFalse); |
|
302 |
|
303 /** |
|
304 * Print |
|
305 * @param aStream |
|
306 */ |
|
307 void Print(RWriteStream& aStream); |
|
308 |
|
309 public: // STATIC METHODS |
|
310 static void Log(); |
|
311 |
|
312 private: // METHODS |
|
313 /** |
|
314 * Copy ctor |
|
315 */ |
|
316 TM2GStatistic(const TM2GStatistic&); |
|
317 |
|
318 /** |
|
319 * Assignment operator |
|
320 */ |
|
321 TM2GStatistic& operator=(const TM2GStatistic&); |
|
322 |
|
323 private: // VARIABLES |
|
324 TTime iStartTime; |
|
325 TTime iEndTime; |
|
326 TThreadId iId; |
|
327 TName iThreadName; |
|
328 }; |
|
329 #endif // M2G_STATISTIC_DEBUG |
|
330 |
|
331 private: // METHODS |
|
332 /** |
|
333 * Default constructor |
|
334 */ |
|
335 M2GGeneral() {}; |
|
336 |
|
337 /** |
|
338 * Copy constructor |
|
339 */ |
|
340 M2GGeneral(const M2GGeneral&); |
|
341 |
|
342 /** |
|
343 * Assignment operator |
|
344 */ |
|
345 M2GGeneral& operator=(const M2GGeneral&); |
|
346 }; |
|
347 template<class A1> |
|
348 class _TSwtM2gMethodWrapper1 : public MSwtFunctor |
|
349 { |
|
350 public: |
|
351 typedef void (*TMethod)(A1); |
|
352 inline _TSwtM2gMethodWrapper1(TMethod aMethod, A1 aArg1) |
|
353 : iMethod(aMethod), |
|
354 iArg1(aArg1) |
|
355 {} |
|
356 void operator()() const |
|
357 { |
|
358 (*iMethod)(iArg1); |
|
359 } |
|
360 private: |
|
361 const TMethod iMethod; |
|
362 A1 iArg1; |
|
363 }; |
|
364 template<class A1> |
|
365 class TSwtM2gMethodWrapper1 : public MSwtFunctor |
|
366 { |
|
367 public: |
|
368 typedef void (*TMethod)(A1*); |
|
369 inline TSwtM2gMethodWrapper1(TMethod aMethod, A1* aArg1) |
|
370 : iMethod(aMethod), |
|
371 iArg1(aArg1) |
|
372 {} |
|
373 void operator()() const |
|
374 { |
|
375 (*iMethod)(iArg1); |
|
376 } |
|
377 private: |
|
378 const TMethod iMethod; |
|
379 A1* iArg1; |
|
380 }; |
|
381 template<class A1,class A2> |
|
382 class TSwtM2gMethodWrapper2 : public MSwtFunctor |
|
383 { |
|
384 public: |
|
385 typedef void (*TMethod)(A1*,A2*); |
|
386 inline TSwtM2gMethodWrapper2(TMethod aMethod, A1* aArg1,A2* aArg2) |
|
387 : iMethod(aMethod), |
|
388 iArg1(aArg1), |
|
389 iArg2(aArg2) |
|
390 {} |
|
391 void operator()() const |
|
392 { |
|
393 (*iMethod)(iArg1,iArg2); |
|
394 } |
|
395 private: |
|
396 const TMethod iMethod; |
|
397 A1* iArg1; |
|
398 A2* iArg2; |
|
399 }; |
|
400 |
|
401 // MACROS |
|
402 // == TOOL MACROS == |
|
403 #define M2G_MIN( a, b ) ( ( a ) <= ( b ) ? ( a ) : ( b ) ) |
|
404 #define M2G_MAX( a, b ) ( ( a ) >= ( b ) ? ( a ) : ( b ) ) |
|
405 |
|
406 // == SWITCHES == |
|
407 #define M2G_NO_EXCEPTION_SWITCH |
|
408 // #define M2G_NO_NULL_POINTER_EXCEPTION_SWITCH |
|
409 |
|
410 #if defined( M2G_DEBUG ) |
|
411 #define M2G_TRACE |
|
412 #endif |
|
413 |
|
414 // == EXCEPTION MACROS == |
|
415 // Common exception handling |
|
416 #ifndef M2G_NO_EXCEPTION_SWITCH |
|
417 #define M2G_THROW( ERR_CODE ) /**/ User::Leave( ( ERR_CODE ) ) |
|
418 #define M2G_THROW2( ERR_TEXT, ERR_CODE ) /**/ M2G_DEBUG_0( ERR_TEXT ); User::Leave( ( ERR_CODE ) ) |
|
419 #else |
|
420 #define M2G_THROW( ERR_CODE ) /**/ |
|
421 #define M2G_THROW2( ERR_TEXT, ERR_CODE ) /**/ |
|
422 #endif // !M2G_NO_EXCEPTION_SWITCH |
|
423 |
|
424 // Null pointer exception handling |
|
425 #ifndef M2G_NO_NULL_POINTER_EXCEPTION_SWITCH |
|
426 #define M2G_CHECK_NULL_POINTER( JNI, POINTER ) /**/ M2GGeneral::CheckHandle( ( JNI, POINTER ) ) |
|
427 #else |
|
428 #define M2G_CHECK_NULL_POINTER( JNI, POINTER ) /**/ |
|
429 #endif // !M2G_NO_NULL_POINTER_EXCEPTION_SWITCH |
|
430 |
|
431 |
|
432 // == STATIC LOG MACROS == |
|
433 #if defined( M2G_STATISTIC_DEBUG ) |
|
434 #define M2G_STATISTIC_LOG /**/ M2GGeneral::TM2GStatistic::Log() |
|
435 #else // M2G_STATISTIC_DEBUG |
|
436 #define M2G_STATISTIC_LOG /**/ |
|
437 #endif // !M2G_STATISTIC_DEBUG |
|
438 |
|
439 // == POPUP MACROS == |
|
440 #if defined( M2G_INFO_POPUP ) |
|
441 #define M2G_POPUP( LINE1, LINE2 ) /**/ M2GGeneral::PopupInfo( LINE1, LINE2 ); |
|
442 #else // !M2G_INFO_POPUP |
|
443 #define M2G_POPUP( LINE1, LINE2 ) /**/ |
|
444 #endif // M2G_INFO_POPUP |
|
445 |
|
446 // == TRACE MACROS == |
|
447 #if defined( M2G_TRACE ) |
|
448 #define M2G_DEBUG_0( TEXT ) /**/ \ |
|
449 RFileLogger::Write( KLogDirectory, KLogFileName, EFileLoggingModeAppend, _L( TEXT ) ) |
|
450 |
|
451 #define M2G_DEBUG_1( TEXT, VAL1 ) /**/ \ |
|
452 RFileLogger::WriteFormat( KLogDirectory, KLogFileName, EFileLoggingModeAppend, /**/ \ |
|
453 _L( TEXT ), VAL1 ) |
|
454 |
|
455 #define M2G_DEBUG_2( TEXT, VAL1, VAL2 ) /**/ \ |
|
456 RFileLogger::WriteFormat( KLogDirectory, KLogFileName, EFileLoggingModeAppend, /**/ \ |
|
457 _L( TEXT ), VAL1, VAL2 ) |
|
458 |
|
459 #define M2G_DEBUG_3( TEXT, VAL1, VAL2, VAL3 ) /**/ \ |
|
460 RFileLogger::WriteFormat( KLogDirectory, KLogFileName, EFileLoggingModeAppend, /**/ \ |
|
461 _L( TEXT ), VAL1, VAL2, VAL3 ) |
|
462 |
|
463 #define M2G_DEBUG_4( TEXT, VAL1, VAL2, VAL3, VAL4 ) /**/ \ |
|
464 RFileLogger::WriteFormat( KLogDirectory, KLogFileName, EFileLoggingModeAppend, /**/ \ |
|
465 _L( TEXT ), VAL1, VAL2, VAL3, VAL4 ) |
|
466 |
|
467 #define M2G_DEBUG_5( TEXT, VAL1, VAL2, VAL3, VAL4, VAL5 ) /**/ \ |
|
468 RFileLogger::WriteFormat( KLogDirectory, KLogFileName, EFileLoggingModeAppend, /**/ \ |
|
469 _L( TEXT ), VAL1, VAL2, VAL3, VAL4, VAL5 ) |
|
470 |
|
471 #define M2G_DEBUG_6( TEXT, VAL1, VAL2, VAL3, VAL4, VAL5, VAL6 ) /**/ \ |
|
472 RFileLogger::WriteFormat( KLogDirectory, KLogFileName, EFileLoggingModeAppend, /**/ \ |
|
473 _L( TEXT ), VAL1, VAL2, VAL3, VAL4, VAL5, VAL6 ) |
|
474 |
|
475 #define M2G_DEBUG_7( TEXT, VAL1, VAL2, VAL3, VAL4, VAL5, VAL6, VAL7 ) /**/ \ |
|
476 RFileLogger::WriteFormat( KLogDirectory, KLogFileName, EFileLoggingModeAppend, /**/ \ |
|
477 _L( TEXT ), VAL1, VAL2, VAL3, VAL4, VAL5, VAL6, VAL7 ) |
|
478 #else // !M2G_TRACE |
|
479 #define M2G_POPUP( LINE1, LINE2 ) /**/ |
|
480 |
|
481 #define M2G_DEBUG_0( TEXT ) /**/ |
|
482 #define M2G_DEBUG_1( TEXT, VAL1 ) /**/ |
|
483 #define M2G_DEBUG_2( TEXT, VAL1, VAL2 ) /**/ |
|
484 #define M2G_DEBUG_3( TEXT, VAL1, VAL2, VAL3 ) /**/ |
|
485 #define M2G_DEBUG_4( TEXT, VAL1, VAL2, VAL3, VAL4 ) /**/ |
|
486 #define M2G_DEBUG_5( TEXT, VAL1, VAL2, VAL3, VAL4, VAL5 ) /**/ |
|
487 #define M2G_DEBUG_6( TEXT, VAL1, VAL2, VAL3, VAL4, VAL5, VAL6 ) /**/ |
|
488 #define M2G_DEBUG_7( TEXT, VAL1, VAL2, VAL3, VAL4, VAL5, VAL6, VAL7 ) /**/ |
|
489 #endif // M2G_TRACE |
|
490 |
|
491 // DATA TYPES |
|
492 typedef TInt TM2GSvgEngineHandle; |
|
493 typedef TInt TM2GSvgDocumentHandle; |
|
494 typedef TInt TM2GSvgElementHandle; |
|
495 typedef TInt TM2GBitmapHandle; |
|
496 typedef TInt TM2GSvgPathHandle; |
|
497 typedef TInt16 TM2GSvgAttrType; |
|
498 typedef TUint8 TM2GPixelComponent; |
|
499 |
|
500 typedef TFixedArray<TInt, 3> TM2GColorData; |
|
501 typedef TFixedArray<TReal32, 6> TM2GMatrixData; |
|
502 typedef M2GGeneral::TDataArrayPtr<TReal32> TM2GMatrixData2; |
|
503 typedef TFixedArray<TReal32, 4> TM2GRectData; |
|
504 typedef TFixedArray<TReal32, 4> TM2GScreenBBoxData; |
|
505 typedef TFixedArray<TReal32, 6> TM2GPathCurveData; |
|
506 |
|
507 |
|
508 M2G_NS_END |
|
509 |
|
510 //#if defined( __cplusplus ) |
|
511 //} // extern "C" |
|
512 //#endif |
|
513 |
|
514 #endif // M2GGENERAL_H |