|
1 /******************************************************************************* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. This program and the accompanying materials |
|
4 * are made available under the terms of the Eclipse Public License v1.0 |
|
5 * which accompanies this distribution, and is available at |
|
6 * http://www.eclipse.org/legal/epl-v10.html |
|
7 * |
|
8 * Contributors: |
|
9 * Nokia Corporation - initial implementation |
|
10 *******************************************************************************/ |
|
11 |
|
12 #ifndef SWTAUTORELEASE_H |
|
13 #define SWTAUTORELEASE_H |
|
14 |
|
15 #include "swtapplication.h" |
|
16 #include "jniutils.h" |
|
17 |
|
18 namespace Java { namespace eSWT { |
|
19 |
|
20 /** |
|
21 * Helper class for auto_ptr like resource management. |
|
22 * Allocate these objects on stack and let them release the resources |
|
23 * when the object goes out of scope. |
|
24 */ |
|
25 template<class T> |
|
26 class AutoRelease |
|
27 { |
|
28 public: |
|
29 AutoRelease( T* aPointer, bool isArray = false ) |
|
30 : mPointer( aPointer ), mArray( isArray ) |
|
31 { |
|
32 } |
|
33 virtual ~AutoRelease() |
|
34 { |
|
35 release(); |
|
36 } |
|
37 T* get() |
|
38 { |
|
39 return mPointer; |
|
40 } |
|
41 void reset(T* aPointer) |
|
42 { |
|
43 release(); |
|
44 mPointer = aPointer; |
|
45 } |
|
46 private: |
|
47 void release() |
|
48 { |
|
49 if( mArray ) |
|
50 { |
|
51 delete[] mPointer; |
|
52 } |
|
53 else |
|
54 { |
|
55 delete mPointer; |
|
56 } |
|
57 mPointer = NULL; |
|
58 } |
|
59 private: |
|
60 T* mPointer; |
|
61 bool mArray; |
|
62 }; |
|
63 |
|
64 |
|
65 /** |
|
66 * Helper class to automatically call ReleaseStringChars for a Java |
|
67 * string chars object obtained from a call to GetStringChars. |
|
68 */ |
|
69 class AutoReleaseStringChars |
|
70 { |
|
71 public: |
|
72 AutoReleaseStringChars( JNIEnv* aJniEnv, jstring aString, const jchar* aPointer ) |
|
73 : mJniEnv( aJniEnv ), mString( aString ), mPointer( aPointer ) |
|
74 {} |
|
75 virtual ~AutoReleaseStringChars() |
|
76 { |
|
77 mJniEnv->ReleaseStringChars( mString, mPointer ); |
|
78 } |
|
79 protected: |
|
80 JNIEnv* mJniEnv; |
|
81 jstring mString; |
|
82 const jchar* mPointer; |
|
83 }; |
|
84 |
|
85 |
|
86 /** |
|
87 * Helper class to automatically call DeleteLocalRef to free the |
|
88 * given Java class reference. |
|
89 */ |
|
90 class AutoDeleteLocalClassRef |
|
91 { |
|
92 public: |
|
93 AutoDeleteLocalClassRef( JNIEnv* aJniEnv, jclass aClazz ) |
|
94 : mJniEnv( aJniEnv ), mClazz( aClazz ) |
|
95 {} |
|
96 virtual ~AutoDeleteLocalClassRef() |
|
97 { |
|
98 mJniEnv->DeleteLocalRef( mClazz ); |
|
99 } |
|
100 protected: |
|
101 JNIEnv* mJniEnv; |
|
102 jclass mClazz; |
|
103 }; |
|
104 |
|
105 /** |
|
106 * Helper class to automatically free ExecExceptionHandlers. |
|
107 */ |
|
108 class AutoPopExecStack |
|
109 { |
|
110 public: |
|
111 AutoPopExecStack(QObject* aObject) |
|
112 { |
|
113 swtApp->jniUtils().enterExec(aObject); |
|
114 } |
|
115 virtual ~AutoPopExecStack() |
|
116 { |
|
117 swtApp->jniUtils().leaveExec(); |
|
118 } |
|
119 }; |
|
120 |
|
121 }} |
|
122 |
|
123 #endif // SWTAUTORELEASE_H |
|
124 |