|
1 /* |
|
2 * Copyright (c) 1999-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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #ifndef JUTILS_H |
|
20 #define JUTILS_H |
|
21 |
|
22 #include <e32base.h> |
|
23 #include <badesca.h> |
|
24 #include "jni.h" |
|
25 |
|
26 //---------------------------------------------------------------------------- |
|
27 // RJString takes a Java JNI string and converts it to an |
|
28 // Epoc string. It retains the JNI environment and the string |
|
29 // in order to release the string resources during destruction |
|
30 class RJString : public TPtrC16 |
|
31 { |
|
32 public: |
|
33 IMPORT_C RJString(JNIEnv& aJni, jstring aString); |
|
34 IMPORT_C ~RJString(); |
|
35 |
|
36 private: |
|
37 // Prevent accidental copying because of the shared underlying Java |
|
38 // string |
|
39 RJString(const RJString&); |
|
40 RJString& operator=(const RJString&); |
|
41 |
|
42 private: |
|
43 JNIEnv& iJni; |
|
44 jstring iString; |
|
45 }; |
|
46 |
|
47 //---------------------------------------------------------------------------- |
|
48 |
|
49 IMPORT_C jstring CreateJavaString(JNIEnv& aJni, const TDesC16& aString); |
|
50 |
|
51 void AddToJavaStringArrayL(JNIEnv& aJni, jobjectArray& aContainer, TInt aPosition, const TDesC& aString); |
|
52 |
|
53 IMPORT_C jobjectArray CopyToNewJavaStringArrayL(JNIEnv& aJni, const CDesCArray& aNativeArray); |
|
54 |
|
55 |
|
56 |
|
57 class RJArray |
|
58 { |
|
59 public: |
|
60 RJArray(JNIEnv& aJni); |
|
61 ~RJArray(); |
|
62 void* GetPrimitiveArrayCriticalLC(jarray aJavaArray, TBool aMutable = EFalse); |
|
63 static void CleanupArrayAccess(TAny* aRJArray); |
|
64 |
|
65 private: |
|
66 void ReleasePrimitiveArrayCritical(); |
|
67 |
|
68 private: |
|
69 JNIEnv& iJni; |
|
70 jarray iJavaArray; |
|
71 TUint8* iArrayPtr; |
|
72 TBool iMutable; |
|
73 }; |
|
74 |
|
75 |
|
76 |
|
77 class ArrayUtils |
|
78 { |
|
79 public: |
|
80 static TInt CopyToNative(JNIEnv& aJni, jbyteArray aJavaBuffer, |
|
81 TInt aOffset, TInt aLength, TDes8& aNativeBuffer); |
|
82 IMPORT_C static TInt CopyToJava(JNIEnv& aJni, const TDesC8& aNativeBuffer, |
|
83 jbyteArray aJavaBuffer, TInt aOffset, TInt aLength); |
|
84 static jobjectArray CopyToNewJavaStringArray(JNIEnv& aJni, |
|
85 const RPointerArray<HBufC>& aNativeArray); |
|
86 |
|
87 }; |
|
88 |
|
89 |
|
90 |
|
91 //---------------------------------------------------------------------------- |
|
92 // Constants that define the date/time '00:00, 1 Jan 1970' when used to create a TTime object |
|
93 const TUint JavaUpperTimeFor1970 = 14474675; |
|
94 const TUint JavaLowerTimeFor1970 = 254771200; |
|
95 |
|
96 //---------------------------------------------------------------------------- |
|
97 // Used for converting between a Java jlong value and an Epoc |
|
98 // TTime and vice-versa. The jlong represents the number of |
|
99 // milliseconds since 00:00 1st Jan 1970. |
|
100 class JavaEpocTime |
|
101 { |
|
102 public: |
|
103 IMPORT_C static TTime CreateEpocTTime(jlong aJavaTime); |
|
104 IMPORT_C static jlong CreateJavaTime(TTime aEpocTime); |
|
105 }; |
|
106 |
|
107 //----------------------------------------------------------------------------- |
|
108 // Creating integer 'handles' from C++ objects for referencing them inside Java |
|
109 // The shift garauntees a positive integer, so object creation native methods |
|
110 // can overload the return value to be a handle or an error code |
|
111 // |
|
112 // Unhanding the integer requires the destination type to be known, so is |
|
113 // implemented as a template function, it should be invoked as |
|
114 // |
|
115 // CXyz* xyz=JavaUnhand(aHandle); |
|
116 // |
|
117 const TInt KJavaHandleShift=2; |
|
118 |
|
119 inline TInt JavaMakeHandle(const TAny* aObject) |
|
120 { |
|
121 return reinterpret_cast<TUint>(aObject)>>KJavaHandleShift; |
|
122 } |
|
123 |
|
124 template <typename T> |
|
125 inline T* JavaUnhand(TInt aHandle) |
|
126 { |
|
127 return reinterpret_cast<T*>(aHandle<<KJavaHandleShift); |
|
128 } |
|
129 |
|
130 |
|
131 //----------------------------------------------------------------------------- |
|
132 // Provide the handle/unhand pattern for CBase derived classes, to gain the |
|
133 // Handle(), Unhand(), New() and TConstructor members, you should derive from |
|
134 // this class using the following pattern: |
|
135 // |
|
136 // class CXyz : public CJavaPeer<CXyz> |
|
137 // {...}; |
|
138 // |
|
139 // The TConstructor member allows for more complex factory functions, |
|
140 // providing automatic use of the cleanup stack. Supposing the derived |
|
141 // class has a second phase constructor ConstructL(), the factory function |
|
142 // could be: |
|
143 // |
|
144 // TInt CXyz::New(...) |
|
145 // { |
|
146 // TRAPD(h,TConstructor c;c->ConstructL(...);h=c.GetHandle();) |
|
147 // return h; |
|
148 // } |
|
149 // |
|
150 template <class T> |
|
151 class CJavaPeer : public CBase |
|
152 { |
|
153 protected: |
|
154 class TConstructor |
|
155 { |
|
156 public: |
|
157 inline TConstructor(T* aObject) |
|
158 :iObject(aObject) |
|
159 { |
|
160 CleanupStack::PushL(aObject); |
|
161 } |
|
162 inline TConstructor() |
|
163 :iObject(new(ELeave) T) |
|
164 { |
|
165 CleanupStack::PushL(iObject); |
|
166 } |
|
167 inline T* operator->() const |
|
168 { |
|
169 return static_cast<T*>(iObject); |
|
170 } |
|
171 inline TInt GetHandle() |
|
172 { |
|
173 CleanupStack::Pop(); |
|
174 return JavaMakeHandle(iObject); |
|
175 } |
|
176 private: |
|
177 CBase* iObject; |
|
178 }; |
|
179 public: |
|
180 inline TInt Handle() const |
|
181 { |
|
182 return JavaMakeHandle(this); |
|
183 } |
|
184 inline static T& Unhand(TInt aHandle) |
|
185 { |
|
186 return *JavaUnhand<T>(aHandle); |
|
187 } |
|
188 static TInt New() |
|
189 { |
|
190 T* self=new T; |
|
191 return self ? self->Handle() : KErrNoMemory; |
|
192 } |
|
193 }; |
|
194 |
|
195 #endif // JUTILS_H |