PyLucene 3.4.0-1 import
[pylucene.git] / jcc / jcc / sources / JCCEnv.h
1 /*
2  *   Copyright (c) 2007-2008 Open Source Applications Foundation
3  *
4  *   Licensed under the Apache License, Version 2.0 (the "License");
5  *   you may not use this file except in compliance with the License.
6  *   You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *   Unless required by applicable law or agreed to in writing, software
11  *   distributed under the License is distributed on an "AS IS" BASIS,
12  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *   See the License for the specific language governing permissions and
14  *   limitations under the License.
15  */
16
17 #ifndef _JCCEnv_H
18 #define _JCCEnv_H
19
20 #include <stdarg.h>
21 #if defined(_MSC_VER) || defined(__WIN32)
22 #define _DLL_IMPORT __declspec(dllimport)
23 #define _DLL_EXPORT __declspec(dllexport)
24 #include <windows.h>
25 #undef MAX_PRIORITY
26 #undef MIN_PRIORITY
27 #else
28 #include <pthread.h>
29 #define _DLL_IMPORT
30 #define _DLL_EXPORT
31 #endif
32
33 #ifdef __SUNPRO_CC
34 #undef DEFAULT_TYPE
35 #endif
36
37 #ifdef TRUE
38 #undef TRUE
39 #endif
40 #ifdef FALSE
41 #undef FALSE
42 #endif
43
44 #include <map>
45
46 #ifdef PYTHON
47 #include <Python.h>
48 #endif
49
50 #undef EOF
51
52 class JCCEnv;
53
54 #if defined(_MSC_VER) || defined(__WIN32)
55
56 #ifdef _jcc_shared
57 _DLL_IMPORT extern JCCEnv *env;
58 _DLL_IMPORT extern DWORD VM_ENV;
59 #else
60 _DLL_EXPORT extern JCCEnv *env;
61 _DLL_EXPORT extern DWORD VM_ENV;
62 #endif
63
64 #else
65
66 extern JCCEnv *env;
67
68 #endif
69
70 #define _EXC_PYTHON ((int) 0)
71 #define _EXC_JAVA   ((int) 1)
72
73 typedef jclass (*getclassfn)(void);
74
75 class countedRef {
76 public:
77     jobject global;
78     int count;
79 };
80
81 class _DLL_EXPORT JCCEnv {
82 protected:
83     jclass _sys, _obj, _thr;
84     jmethodID *_mids;
85
86     enum {
87         mid_sys_identityHashCode,
88         mid_sys_setProperty,
89         mid_sys_getProperty,
90         mid_obj_toString,
91         mid_obj_hashCode,
92         mid_obj_getClass,
93         mid_iterator,
94         mid_iterator_next,
95         mid_enumeration_nextElement,
96         mid_Boolean_booleanValue,
97         mid_Byte_byteValue,
98         mid_Character_charValue,
99         mid_Double_doubleValue,
100         mid_Float_floatValue,
101         mid_Integer_intValue,
102         mid_Long_longValue,
103         mid_Short_shortValue,
104         max_mid
105     };
106
107 public:
108     JavaVM *vm;
109     std::multimap<int, countedRef> refs;
110     int handlers;
111
112     explicit JCCEnv(JavaVM *vm, JNIEnv *env);
113
114 #if defined(_MSC_VER) || defined(__WIN32)
115     inline JNIEnv *get_vm_env() const
116     {
117         return (JNIEnv *) TlsGetValue(VM_ENV);
118     }
119 #else
120     static pthread_key_t VM_ENV;
121
122     inline JNIEnv *get_vm_env() const
123     {
124         return (JNIEnv *) pthread_getspecific(VM_ENV);
125     }
126 #endif
127     void set_vm(JavaVM *vm, JNIEnv *vm_env);
128     void set_vm_env(JNIEnv *vm_env);
129     int attachCurrentThread(char *name, int asDaemon);
130
131     jint getJNIVersion() const;
132     jstring getJavaVersion() const;
133
134     jclass findClass(const char *className) const;
135     jboolean isInstanceOf(jobject obj, getclassfn initializeClass) const;
136
137     void registerNatives(jclass cls, JNINativeMethod *methods, int n) const;
138
139     jobject iterator(jobject obj) const;
140     jobject iteratorNext(jobject obj) const;
141     jobject enumerationNext(jobject obj) const;
142
143     jobject newGlobalRef(jobject obj, int id);
144     jobject deleteGlobalRef(jobject obj, int id);
145
146     jobject newObject(getclassfn initializeClass, jmethodID **mids, int m, ...);
147
148     jobjectArray newObjectArray(jclass cls, int size);
149     void setObjectArrayElement(jobjectArray a, int n,
150                                        jobject obj) const;
151     jobject getObjectArrayElement(jobjectArray a, int n) const;
152     int getArrayLength(jarray a) const;
153
154     void reportException() const;
155
156     jobject callObjectMethod(jobject obj, jmethodID mid, ...) const;
157     jboolean callBooleanMethod(jobject obj, jmethodID mid, ...) const;
158     jbyte callByteMethod(jobject obj, jmethodID mid, ...) const;
159     jchar callCharMethod(jobject obj, jmethodID mid, ...) const;
160     jdouble callDoubleMethod(jobject obj, jmethodID mid, ...) const;
161     jfloat callFloatMethod(jobject obj, jmethodID mid, ...) const;
162     jint callIntMethod(jobject obj, jmethodID mid, ...) const;
163     jlong callLongMethod(jobject obj, jmethodID mid, ...) const;
164     jshort callShortMethod(jobject obj, jmethodID mid, ...) const;
165     void callVoidMethod(jobject obj, jmethodID mid, ...) const;
166
167     jobject callNonvirtualObjectMethod(jobject obj, jclass cls,
168                                        jmethodID mid, ...) const;
169     jboolean callNonvirtualBooleanMethod(jobject obj, jclass cls,
170                                          jmethodID mid, ...) const;
171     jbyte callNonvirtualByteMethod(jobject obj, jclass cls,
172                                    jmethodID mid, ...) const;
173     jchar callNonvirtualCharMethod(jobject obj, jclass cls,
174                                    jmethodID mid, ...) const;
175     jdouble callNonvirtualDoubleMethod(jobject obj, jclass cls,
176                                        jmethodID mid, ...) const;
177     jfloat callNonvirtualFloatMethod(jobject obj, jclass cls,
178                                      jmethodID mid, ...) const;
179     jint callNonvirtualIntMethod(jobject obj, jclass cls,
180                                  jmethodID mid, ...) const;
181     jlong callNonvirtualLongMethod(jobject obj, jclass cls,
182                                    jmethodID mid, ...) const;
183     jshort callNonvirtualShortMethod(jobject obj, jclass cls,
184                                      jmethodID mid, ...) const;
185     void callNonvirtualVoidMethod(jobject obj, jclass cls,
186                                   jmethodID mid, ...) const;
187
188     jobject callStaticObjectMethod(jclass cls, jmethodID mid, ...) const;
189     jboolean callStaticBooleanMethod(jclass cls, jmethodID mid, ...) const;
190     jbyte callStaticByteMethod(jclass cls, jmethodID mid, ...) const;
191     jchar callStaticCharMethod(jclass cls, jmethodID mid, ...) const;
192     jdouble callStaticDoubleMethod(jclass cls, jmethodID mid, ...) const;
193     jfloat callStaticFloatMethod(jclass cls, jmethodID mid, ...) const;
194     jint callStaticIntMethod(jclass cls, jmethodID mid, ...) const;
195     jlong callStaticLongMethod(jclass cls, jmethodID mid, ...) const;
196     jshort callStaticShortMethod(jclass cls, jmethodID mid, ...) const;
197     void callStaticVoidMethod(jclass cls, jmethodID mid, ...) const;
198
199     jboolean booleanValue(jobject obj) const;
200     jbyte byteValue(jobject obj) const;
201     jchar charValue(jobject obj) const;
202     jdouble doubleValue(jobject obj) const;
203     jfloat floatValue(jobject obj) const;
204     jint intValue(jobject obj) const;
205     jlong longValue(jobject obj) const;
206     jshort shortValue(jobject obj) const;
207
208     jmethodID getMethodID(jclass cls, const char *name,
209                                   const char *signature) const;
210     jfieldID getFieldID(jclass cls, const char *name,
211                                 const char *signature) const;
212     jmethodID getStaticMethodID(jclass cls, const char *name,
213                                 const char *signature) const;
214
215     jobject getStaticObjectField(jclass cls, const char *name,
216                                          const char *signature) const;
217     jboolean getStaticBooleanField(jclass cls, const char *name) const;
218     jbyte getStaticByteField(jclass cls, const char *name) const;
219     jchar getStaticCharField(jclass cls, const char *name) const;
220     jdouble getStaticDoubleField(jclass cls, const char *name) const;
221     jfloat getStaticFloatField(jclass cls, const char *name) const;
222     jint getStaticIntField(jclass cls, const char *name) const;
223     jlong getStaticLongField(jclass cls, const char *name) const;
224     jshort getStaticShortField(jclass cls, const char *name) const;
225
226     jobject getObjectField(jobject obj, jfieldID id) const;
227     jboolean getBooleanField(jobject obj, jfieldID id) const;
228     jbyte getByteField(jobject obj, jfieldID id) const;
229     jchar getCharField(jobject obj, jfieldID id) const;
230     jdouble getDoubleField(jobject obj, jfieldID id) const;
231     jfloat getFloatField(jobject obj, jfieldID id) const;
232     jint getIntField(jobject obj, jfieldID id) const;
233     jlong getLongField(jobject obj, jfieldID id) const;
234     jshort getShortField(jobject obj, jfieldID id) const;
235
236     void setObjectField(jobject obj, jfieldID id, jobject value) const;
237     void setBooleanField(jobject obj, jfieldID id, jboolean value) const;
238     void setByteField(jobject obj, jfieldID id, jbyte value) const;
239     void setCharField(jobject obj, jfieldID id, jchar value) const;
240     void setDoubleField(jobject obj, jfieldID id, jdouble value) const;
241     void setFloatField(jobject obj, jfieldID id, jfloat value) const;
242     void setIntField(jobject obj, jfieldID id, jint value) const;
243     void setLongField(jobject obj, jfieldID id, jlong value) const;
244     void setShortField(jobject obj, jfieldID id, jshort value) const;
245
246     int id(jobject obj) const {
247         return obj
248             ? get_vm_env()->CallStaticIntMethod(_sys,
249                                                 _mids[mid_sys_identityHashCode],
250                                                 obj)
251             : 0;
252     }
253
254     int hash(jobject obj) const {
255         return obj
256             ? get_vm_env()->CallIntMethod(obj, _mids[mid_obj_hashCode])
257             : 0;
258     }
259
260     void setClassPath(const char *classPath);
261     char *getClassPath();
262
263     jstring fromUTF(const char *bytes) const;
264     char *toUTF(jstring str) const;
265     char *toString(jobject obj) const;
266     char *getClassName(jobject obj) const;
267 #ifdef PYTHON
268     jclass getPythonExceptionClass() const;
269     jstring fromPyString(PyObject *object) const;
270     PyObject *fromJString(jstring js, int delete_local_ref) const;
271     void finalizeObject(JNIEnv *jenv, PyObject *obj);
272 #endif
273
274     inline int isSame(jobject o1, jobject o2) const
275     {
276         return o1 == o2 || get_vm_env()->IsSameObject(o1, o2);
277     }
278 };
279
280 #ifdef PYTHON
281
282 class PythonGIL {
283   private:
284     PyGILState_STATE state;
285   public:
286     PythonGIL()
287     {
288         state = PyGILState_Ensure();
289     }
290     PythonGIL(JNIEnv *vm_env)
291     {
292         state = PyGILState_Ensure();
293         env->set_vm_env(vm_env);
294     }
295     ~PythonGIL()
296     {
297         PyGILState_Release(state);
298     }
299 };
300
301 class PythonThreadState {
302   private:
303     PyThreadState *state;
304     int handler;
305   public:
306     PythonThreadState(int handler=0)
307     {
308         state = PyEval_SaveThread();
309         this->handler = handler;
310         env->handlers += handler;
311     }
312     ~PythonThreadState()
313     {
314         PyEval_RestoreThread(state);
315         env->handlers -= handler;
316     }
317 };
318
319 #endif
320
321 #endif /* _JCCEnv_H */