]> Creatis software - CreaPhase.git/blob - octave_packages/java-1.2.8/__java__.h
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / java-1.2.8 / __java__.h
1 /* Copyright (C) 2007 Michael Goffioul
2 **
3 ** This program is free software; you can redistribute it and/or modify
4 ** it under the terms of the GNU General Public License as published by
5 ** the Free Software Foundation; either version 2 of the License, or
6 ** (at your option) any later version.
7 **
8 ** This program is distributed in the hope that it will be useful,
9 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ** GNU General Public License for more details.
12 **
13 ** You should have received a copy of the GNU General Public License
14 ** along with this program; If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #ifndef __JAVA__H__
18 #define __JAVA__H__
19
20 #include <octave/oct.h>
21 #include <octave/config.h>
22 #ifndef OCTAVE_EXPORT
23 #include <octave/oct-dlldefs.h>
24 #endif // OCTAVE_EXPORT
25 #include <jni.h>
26
27 #ifdef JAVAPKG_BUILD
28 # define JAVAPKG_API OCTAVE_EXPORT
29 #else
30 # define JAVAPKG_API OCTAVE_IMPORT
31 #endif
32
33 template <class T>
34 class java_local_ref
35 {
36 public:
37   java_local_ref (JNIEnv *_env)
38     : jobj (0), detached (false), env (_env)
39     { }
40
41   java_local_ref (JNIEnv *_env, T obj)
42     : jobj (obj), detached (false), env (_env)
43     { }
44
45   ~java_local_ref (void)
46     {
47       release ();
48     }
49
50   T& operator= (T obj)
51     {
52       release ();
53       jobj = obj;
54       detached = false;
55       return jobj;
56     }
57   operator bool () const { return (jobj != 0); }
58   operator T () { return jobj; }
59
60   void detach () { detached = true; }
61
62 private:
63   void release (void)
64     {
65       if (env && jobj && ! detached)
66         env->DeleteLocalRef (jobj);
67       jobj = 0;
68     }
69
70   java_local_ref (void)
71     : jobj (0), detached (false), env (0)
72     { }
73         
74
75 protected:
76   T jobj;
77   bool detached;
78   JNIEnv *env;
79 };
80
81 typedef java_local_ref<jobject> jobject_ref;
82 typedef java_local_ref<jclass> jclass_ref;
83 typedef java_local_ref<jstring> jstring_ref;
84 typedef java_local_ref<jobjectArray> jobjectArray_ref;
85 typedef java_local_ref<jintArray> jintArray_ref;
86 typedef java_local_ref<jbyteArray> jbyteArray_ref;
87 typedef java_local_ref<jdoubleArray> jdoubleArray_ref;
88 typedef java_local_ref<jthrowable> jthrowable_ref;
89
90 extern JAVAPKG_API std::string jstring_to_string (JNIEnv* jni_env, jstring s);
91 extern JAVAPKG_API std::string jstring_to_string (JNIEnv* jni_env, jobject obj);
92 extern JAVAPKG_API octave_value box (JNIEnv* jni_env, jobject jobj, jclass jcls = 0);
93 extern JAVAPKG_API octave_value box_more (JNIEnv* jni_env, jobject jobj, jclass jcls = 0);
94 extern JAVAPKG_API int unbox (JNIEnv* jni_env, const octave_value& val, jobject_ref& jobj, jclass_ref& jcls);
95 extern JAVAPKG_API int unbox (JNIEnv* jni_env, const octave_value_list& args, jobjectArray_ref& jobjs, jobjectArray_ref& jclss);
96
97 extern JAVAPKG_API bool Vjava_convert_matrix;
98 extern JAVAPKG_API bool Vjava_unsigned_conversion;
99 extern JAVAPKG_API bool Vjava_debug;
100
101 class JAVAPKG_API octave_java : public octave_base_value
102 {
103 public:
104   octave_java (void)
105     : java_object (0), java_class (0)
106     { }
107
108   octave_java (const octave_java& jobj)
109     : java_object (0), java_class (0)
110     {
111       init (jobj.java_object, jobj.java_class);
112     }
113
114   octave_java (jobject obj, jclass cls = 0)
115     : java_object (0)
116     {
117       init (obj, cls);
118     }
119
120   ~octave_java (void)
121     {
122       release ();
123     }
124
125   jobject to_java () const { return java_object; }
126   jclass to_class () const { return java_class; }
127   std::string java_class_name () const { return java_type; }
128         
129   octave_base_value* clone(void) const { return new octave_java(*this); }
130   octave_base_value* empty_clone(void) const { return new octave_java(); }
131
132   bool is_defined(void) const { return true; }
133
134   bool is_map (void) const { return true; }
135
136   string_vector map_keys(void) const;
137
138   dim_vector dims(void) const;
139
140   void print(std::ostream& os, bool pr_as_read_syntax = false) const
141     {
142       os << "<Java object: " << java_type << ">";
143       newline(os);
144     }
145
146   void print_raw(std::ostream& os, bool pr_as_read_syntax = false) const
147     {
148       print(os, pr_as_read_syntax);
149     }
150
151   octave_value_list subsref (const std::string& type, const std::list<octave_value_list>& idx, int nargout);
152         
153   octave_value subsref (const std::string& type,
154                         const std::list<octave_value_list>& idx)
155     {
156       octave_value_list retval = subsref (type, idx, 1);
157       return (retval.length () > 0 ? retval(0) : octave_value ());
158     }
159
160   octave_value subsasgn (const std::string& type, const std::list<octave_value_list>& idx, const octave_value& rhs);
161
162   octave_value convert_to_str_internal (bool pad, bool force, char type) const;
163
164   bool is_string (void) const
165     {
166       JNIEnv *current_env = thread_jni_env ();
167
168       if (current_env && java_object)
169         {
170           jclass_ref cls (current_env, current_env->FindClass ("java/lang/String"));
171           return current_env->IsInstanceOf (java_object, cls);
172         }
173       return false;
174     }
175
176   static JNIEnv* thread_jni_env (void);
177
178   octave_value do_java_invoke (JNIEnv* jni_env, const std::string& name,
179       const octave_value_list& args);
180   
181   octave_value do_java_invoke (const std::string& name, const octave_value_list& args)
182     { return do_java_invoke(thread_jni_env (), name, args); }
183
184   static octave_value do_java_invoke (JNIEnv* jni_env, const std::string& class_name,
185     const std::string& name, const octave_value_list& args);
186   
187   static octave_value do_java_invoke (const std::string& class_name, 
188       const std::string& name, const octave_value_list& args)
189     { return do_java_invoke(thread_jni_env (), class_name, name, args); }
190
191   static octave_value do_java_create (JNIEnv* jni_env, const std::string& name,
192       const octave_value_list& args);
193   
194   static octave_value do_java_create (const std::string& name, const octave_value_list& args)
195     { return do_java_create (thread_jni_env (), name, args); }
196
197   octave_value do_java_get (JNIEnv* jni_env, const std::string& name);
198   
199   octave_value do_java_get (const std::string& name)
200     { return do_java_get (thread_jni_env (), name); }
201
202   static octave_value do_java_get (JNIEnv* jni_env, const std::string& class_name,
203       const std::string& name);
204   
205   static octave_value do_java_get (const std::string& class_name, const std::string& name)
206     { return do_java_get (thread_jni_env (), class_name, name); }
207
208   octave_value do_java_set (JNIEnv* jni_env, const std::string& name, const octave_value& val);
209   
210   octave_value do_java_set (const std::string& name, const octave_value& val)
211     { return do_java_set (thread_jni_env (), name, val); }
212
213   static octave_value do_java_set (JNIEnv* jni_env, const std::string& class_name,
214       const std::string& name, const octave_value& val);
215   
216   static octave_value do_java_set (const std::string& class_name, const std::string& name,
217       const octave_value& val)
218     { return do_java_set (thread_jni_env (), class_name, name, val); }
219
220 private:
221   void init (jobject jobj, jclass jcls)
222     {
223       JNIEnv *current_env = thread_jni_env ();
224
225       if (current_env)
226         {
227           if (jobj)
228             java_object = current_env->NewGlobalRef (jobj);
229           if (jcls)
230             java_class = reinterpret_cast<jclass> (current_env->NewGlobalRef (jcls));
231           else if (java_object)
232             {
233               jclass_ref ocls (current_env, current_env->GetObjectClass (java_object));
234               java_class = reinterpret_cast<jclass> (current_env->NewGlobalRef (jclass (ocls)));
235             }
236
237           if (java_class)
238             {
239               jclass_ref clsCls (current_env, current_env->GetObjectClass (java_class));
240               jmethodID mID = current_env->GetMethodID (clsCls, "getCanonicalName", "()Ljava/lang/String;");
241               jobject_ref resObj (current_env, current_env->CallObjectMethod (java_class, mID));
242               java_type = jstring_to_string (current_env, resObj);
243             }
244         }
245     }
246
247   void release ()
248     {
249       JNIEnv *current_env = thread_jni_env ();
250
251       if (current_env)
252         {
253           if (java_object)
254             current_env->DeleteGlobalRef (java_object);
255           if (java_class)
256             current_env->DeleteGlobalRef (java_class);
257           java_object = 0;
258           java_class = 0;
259         }
260     }
261
262 private:
263   DECLARE_OCTAVE_ALLOCATOR
264         
265   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
266
267   jobject java_object;
268   jclass java_class;
269   std::string java_type;
270 };
271
272 #endif /* __JAVA__H__ */