comparison libinterp/dldfcn/__java__.h @ 15745:19850dfc2f88

* __java__.h, __java__.cc: Style fixes.
author John W. Eaton <jwe@octave.org>
date Fri, 07 Dec 2012 16:04:58 -0500
parents 686580b1a934
children da26f72408a7
comparison
equal deleted inserted replaced
15744:686580b1a934 15745:19850dfc2f88
32 32
33 template <class T> 33 template <class T>
34 class java_local_ref 34 class java_local_ref
35 { 35 {
36 public: 36 public:
37
37 java_local_ref (JNIEnv *_env) 38 java_local_ref (JNIEnv *_env)
38 : jobj (0), detached (false), env (_env) 39 : jobj (0), detached (false), env (_env) { }
39 { }
40 40
41 java_local_ref (JNIEnv *_env, T obj) 41 java_local_ref (JNIEnv *_env, T obj)
42 : jobj (obj), detached (false), env (_env) 42 : jobj (obj), detached (false), env (_env) { }
43 { } 43
44 44 ~java_local_ref (void) { release (); }
45 ~java_local_ref (void)
46 {
47 release ();
48 }
49 45
50 T& operator= (T obj) 46 T& operator= (T obj)
51 { 47 {
52 release (); 48 release ();
53 jobj = obj; 49 jobj = obj;
54 detached = false; 50 detached = false;
55 return jobj; 51 return jobj;
56 } 52 }
53
57 operator bool () const { return (jobj != 0); } 54 operator bool () const { return (jobj != 0); }
58 operator T () { return jobj; } 55 operator T () { return jobj; }
59 56
60 void detach () { detached = true; } 57 void detach (void) { detached = true; }
61 58
62 private: 59 private:
60
63 void release (void) 61 void release (void)
64 { 62 {
65 if (env && jobj && ! detached) 63 if (env && jobj && ! detached)
66 env->DeleteLocalRef (jobj); 64 env->DeleteLocalRef (jobj);
67 jobj = 0; 65
68 } 66 jobj = 0;
67 }
69 68
70 java_local_ref (void) 69 java_local_ref (void)
71 : jobj (0), detached (false), env (0) 70 : jobj (0), detached (false), env (0)
72 { } 71 { }
73
74 72
75 protected: 73 protected:
76 T jobj; 74 T jobj;
77 bool detached; 75 bool detached;
78 JNIEnv *env; 76 JNIEnv *env;
85 typedef java_local_ref<jintArray> jintArray_ref; 83 typedef java_local_ref<jintArray> jintArray_ref;
86 typedef java_local_ref<jbyteArray> jbyteArray_ref; 84 typedef java_local_ref<jbyteArray> jbyteArray_ref;
87 typedef java_local_ref<jdoubleArray> jdoubleArray_ref; 85 typedef java_local_ref<jdoubleArray> jdoubleArray_ref;
88 typedef java_local_ref<jthrowable> jthrowable_ref; 86 typedef java_local_ref<jthrowable> jthrowable_ref;
89 87
90 extern JAVAPKG_API std::string jstring_to_string (JNIEnv* jni_env, jstring s); 88 extern JAVAPKG_API std::string
91 extern JAVAPKG_API std::string jstring_to_string (JNIEnv* jni_env, jobject obj); 89 jstring_to_string (JNIEnv* jni_env, jstring s);
92 extern JAVAPKG_API octave_value box (JNIEnv* jni_env, jobject jobj, jclass jcls = 0); 90
93 extern JAVAPKG_API octave_value box_more (JNIEnv* jni_env, jobject jobj, jclass jcls = 0); 91 extern JAVAPKG_API std::string
94 extern JAVAPKG_API int unbox (JNIEnv* jni_env, const octave_value& val, jobject_ref& jobj, jclass_ref& jcls); 92 jstring_to_string (JNIEnv* jni_env, jobject obj);
95 extern JAVAPKG_API int unbox (JNIEnv* jni_env, const octave_value_list& args, jobjectArray_ref& jobjs, jobjectArray_ref& jclss); 93
94 extern JAVAPKG_API octave_value
95 box (JNIEnv* jni_env, jobject jobj, jclass jcls = 0);
96
97 extern JAVAPKG_API octave_value
98 box_more (JNIEnv* jni_env, jobject jobj, jclass jcls = 0);
99
100 extern JAVAPKG_API int
101 unbox (JNIEnv* jni_env, const octave_value& val, jobject_ref& jobj,
102 jclass_ref& jcls);
103
104 extern JAVAPKG_API int
105 unbox (JNIEnv* jni_env, const octave_value_list& args,
106 jobjectArray_ref& jobjs, jobjectArray_ref& jclss);
96 107
97 extern JAVAPKG_API bool Vjava_convert_matrix; 108 extern JAVAPKG_API bool Vjava_convert_matrix;
109
98 extern JAVAPKG_API bool Vjava_unsigned_conversion; 110 extern JAVAPKG_API bool Vjava_unsigned_conversion;
111
99 extern JAVAPKG_API bool Vjava_debug; 112 extern JAVAPKG_API bool Vjava_debug;
100 113
101 class JAVAPKG_API octave_java : public octave_base_value 114 class JAVAPKG_API octave_java : public octave_base_value
102 { 115 {
103 public: 116 public:
117
104 octave_java (void) 118 octave_java (void)
105 : octave_base_value (), java_object (0), java_class (0) 119 : octave_base_value (), java_object (0), java_class (0)
106 { } 120 { }
107 121
108 octave_java (const octave_java& jobj) 122 octave_java (const octave_java& jobj)
109 : octave_base_value (jobj), java_object (0), java_class (0) 123 : octave_base_value (jobj), java_object (0), java_class (0)
110 { 124 {
111 init (jobj.java_object, jobj.java_class); 125 init (jobj.java_object, jobj.java_class);
112 } 126 }
113 127
114 octave_java (const jobject& obj, jclass cls = 0) 128 octave_java (const jobject& obj, jclass cls = 0)
115 : octave_base_value (), java_object (0) 129 : octave_base_value (), java_object (0)
116 { 130 {
117 init (obj, cls); 131 init (obj, cls);
118 } 132 }
119 133
120 ~octave_java (void) 134 ~octave_java (void) { release (); }
121 { 135
122 release (); 136 jobject to_java (void) const { return java_object; }
123 } 137 jclass to_class (void) const { return java_class; }
124 138
125 jobject to_java () const { return java_object; } 139 std::string java_class_name (void) const { return java_type; }
126 jclass to_class () const { return java_class; } 140
127 std::string java_class_name () const { return java_type; } 141 octave_base_value* clone (void) const { return new octave_java (*this); }
128 142 octave_base_value* empty_clone (void) const { return new octave_java (); }
129 octave_base_value* clone(void) const { return new octave_java(*this); } 143
130 octave_base_value* empty_clone(void) const { return new octave_java(); } 144 bool is_defined (void) const { return true; }
131
132 bool is_defined(void) const { return true; }
133 145
134 bool is_map (void) const { return true; } 146 bool is_map (void) const { return true; }
135 147
136 string_vector map_keys(void) const; 148 string_vector map_keys (void) const;
137 149
138 dim_vector dims(void) const; 150 dim_vector dims (void) const;
139 151
140 void print(std::ostream& os, bool pr_as_read_syntax = false) const 152 void print (std::ostream& os, bool pr_as_read_syntax = false) const
141 { 153 {
142 os << "<Java object: " << java_type << ">"; 154 os << "<Java object: " << java_type << ">";
143 newline(os); 155 newline(os);
144 } 156 }
145 157
146 void print_raw(std::ostream& os, bool pr_as_read_syntax = false) const 158 void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const
147 { 159 {
148 print(os, pr_as_read_syntax); 160 print(os, pr_as_read_syntax);
149 } 161 }
150 162
151 octave_value_list subsref (const std::string& type, const std::list<octave_value_list>& idx, int nargout); 163 octave_value_list
152 164 subsref (const std::string& type,
153 octave_value subsref (const std::string& type, 165 const std::list<octave_value_list>& idx, int nargout);
154 const std::list<octave_value_list>& idx) 166
155 { 167 octave_value
156 octave_value_list retval = subsref (type, idx, 1); 168 subsref (const std::string& type, const std::list<octave_value_list>& idx)
157 return (retval.length () > 0 ? retval(0) : octave_value ()); 169 {
158 } 170 octave_value_list retval = subsref (type, idx, 1);
159 171 return (retval.length () > 0 ? retval(0) : octave_value ());
160 octave_value subsasgn (const std::string& type, const std::list<octave_value_list>& idx, const octave_value& rhs); 172 }
173
174 octave_value subsasgn (const std::string& type,
175 const std::list<octave_value_list>& idx,
176 const octave_value& rhs);
161 177
162 octave_value convert_to_str_internal (bool pad, bool force, char type) const; 178 octave_value convert_to_str_internal (bool pad, bool force, char type) const;
163 179
164 bool is_string (void) const 180 bool is_string (void) const
165 { 181 {
166 JNIEnv *current_env = thread_jni_env (); 182 JNIEnv *current_env = thread_jni_env ();
167 183
168 if (current_env && java_object) 184 if (current_env && java_object)
169 { 185 {
170 jclass_ref cls (current_env, current_env->FindClass ("java/lang/String")); 186 jclass_ref cls (current_env, current_env->FindClass ("java/lang/String"));
171 return current_env->IsInstanceOf (java_object, cls); 187 return current_env->IsInstanceOf (java_object, cls);
172 } 188 }
173 return false; 189
174 } 190 return false;
191 }
175 192
176 static JNIEnv* thread_jni_env (void); 193 static JNIEnv* thread_jni_env (void);
177 194
178 octave_value do_java_invoke (JNIEnv* jni_env, const std::string& name, 195 octave_value do_java_invoke (JNIEnv* jni_env, const std::string& name,
179 const octave_value_list& args); 196 const octave_value_list& args);
180 197
181 octave_value do_java_invoke (const std::string& name, const octave_value_list& args) 198 octave_value
182 { return do_java_invoke(thread_jni_env (), name, args); } 199 do_java_invoke (const std::string& name, const octave_value_list& args)
183 200 {
184 static octave_value do_java_invoke (JNIEnv* jni_env, const std::string& class_name, 201 return do_java_invoke(thread_jni_env (), name, args);
185 const std::string& name, const octave_value_list& args); 202 }
186 203
187 static octave_value do_java_invoke (const std::string& class_name, 204 static octave_value
188 const std::string& name, const octave_value_list& args) 205 do_java_invoke (JNIEnv* jni_env, const std::string& class_name,
189 { return do_java_invoke(thread_jni_env (), class_name, name, args); } 206 const std::string& name, const octave_value_list& args);
190 207
191 static octave_value do_java_create (JNIEnv* jni_env, const std::string& name, 208 static octave_value
192 const octave_value_list& args); 209 do_java_invoke (const std::string& class_name, const std::string& name,
193 210 const octave_value_list& args)
194 static octave_value do_java_create (const std::string& name, const octave_value_list& args) 211 {
195 { return do_java_create (thread_jni_env (), name, args); } 212 return do_java_invoke(thread_jni_env (), class_name, name, args);
213 }
214
215 static octave_value
216 do_java_create (JNIEnv* jni_env, const std::string& name,
217 const octave_value_list& args);
218
219 static octave_value
220 do_java_create (const std::string& name, const octave_value_list& args)
221 {
222 return do_java_create (thread_jni_env (), name, args);
223 }
196 224
197 octave_value do_java_get (JNIEnv* jni_env, const std::string& name); 225 octave_value do_java_get (JNIEnv* jni_env, const std::string& name);
198 226
199 octave_value do_java_get (const std::string& name) 227 octave_value do_java_get (const std::string& name)
200 { return do_java_get (thread_jni_env (), name); } 228 {
201 229 return do_java_get (thread_jni_env (), name);
202 static octave_value do_java_get (JNIEnv* jni_env, const std::string& class_name, 230 }
203 const std::string& name); 231
204 232 static octave_value
205 static octave_value do_java_get (const std::string& class_name, const std::string& name) 233 do_java_get (JNIEnv* jni_env, const std::string& class_name,
206 { return do_java_get (thread_jni_env (), class_name, name); } 234 const std::string& name);
207 235
208 octave_value do_java_set (JNIEnv* jni_env, const std::string& name, const octave_value& val); 236 static octave_value
209 237 do_java_get (const std::string& class_name, const std::string& name)
238 {
239 return do_java_get (thread_jni_env (), class_name, name);
240 }
241
242 octave_value do_java_set (JNIEnv* jni_env, const std::string& name,
243 const octave_value& val);
244
210 octave_value do_java_set (const std::string& name, const octave_value& val) 245 octave_value do_java_set (const std::string& name, const octave_value& val)
211 { return do_java_set (thread_jni_env (), name, val); } 246 {
212 247 return do_java_set (thread_jni_env (), name, val);
213 static octave_value do_java_set (JNIEnv* jni_env, const std::string& class_name, 248 }
214 const std::string& name, const octave_value& val); 249
215 250 static octave_value
216 static octave_value do_java_set (const std::string& class_name, const std::string& name, 251 do_java_set (JNIEnv* jni_env, const std::string& class_name,
217 const octave_value& val) 252 const std::string& name, const octave_value& val);
218 { return do_java_set (thread_jni_env (), class_name, name, val); } 253
254 static octave_value
255 do_java_set (const std::string& class_name, const std::string& name,
256 const octave_value& val)
257 {
258 return do_java_set (thread_jni_env (), class_name, name, val);
259 }
219 260
220 private: 261 private:
262
221 void init (jobject jobj, jclass jcls) 263 void init (jobject jobj, jclass jcls)
222 { 264 {
223 JNIEnv *current_env = thread_jni_env (); 265 JNIEnv *current_env = thread_jni_env ();
224 266
225 if (current_env) 267 if (current_env)
226 { 268 {
227 if (jobj) 269 if (jobj)
228 java_object = current_env->NewGlobalRef (jobj); 270 java_object = current_env->NewGlobalRef (jobj);
229 if (jcls) 271
230 java_class = reinterpret_cast<jclass> (current_env->NewGlobalRef (jcls)); 272 if (jcls)
231 else if (java_object) 273 java_class = reinterpret_cast<jclass> (current_env->NewGlobalRef (jcls));
232 { 274 else if (java_object)
233 jclass_ref ocls (current_env, current_env->GetObjectClass (java_object)); 275 {
234 java_class = reinterpret_cast<jclass> (current_env->NewGlobalRef (jclass (ocls))); 276 jclass_ref ocls (current_env, current_env->GetObjectClass (java_object));
235 } 277 java_class = reinterpret_cast<jclass> (current_env->NewGlobalRef (jclass (ocls)));
236 278 }
237 if (java_class) 279
238 { 280 if (java_class)
239 jclass_ref clsCls (current_env, current_env->GetObjectClass (java_class)); 281 {
240 jmethodID mID = current_env->GetMethodID (clsCls, "getCanonicalName", "()Ljava/lang/String;"); 282 jclass_ref clsCls (current_env, current_env->GetObjectClass (java_class));
241 jobject_ref resObj (current_env, current_env->CallObjectMethod (java_class, mID)); 283 jmethodID mID = current_env->GetMethodID (clsCls, "getCanonicalName", "()Ljava/lang/String;");
242 java_type = jstring_to_string (current_env, resObj); 284 jobject_ref resObj (current_env, current_env->CallObjectMethod (java_class, mID));
243 } 285 java_type = jstring_to_string (current_env, resObj);
244 } 286 }
245 } 287 }
246 288 }
247 void release () 289
248 { 290 void release (void)
249 JNIEnv *current_env = thread_jni_env (); 291 {
250 292 JNIEnv *current_env = thread_jni_env ();
251 if (current_env) 293
252 { 294 if (current_env)
253 if (java_object) 295 {
254 current_env->DeleteGlobalRef (java_object); 296 if (java_object)
255 if (java_class) 297 current_env->DeleteGlobalRef (java_object);
256 current_env->DeleteGlobalRef (java_class); 298
257 java_object = 0; 299 if (java_class)
258 java_class = 0; 300 current_env->DeleteGlobalRef (java_class);
259 } 301
260 } 302 java_object = 0;
303 java_class = 0;
304 }
305 }
261 306
262 private: 307 private:
308
263 DECLARE_OCTAVE_ALLOCATOR 309 DECLARE_OCTAVE_ALLOCATOR
264 310
265 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA 311 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
266 312
267 jobject java_object; 313 jobject java_object;
314
268 jclass java_class; 315 jclass java_class;
316
269 std::string java_type; 317 std::string java_type;
270 }; 318 };
271 319
272 #endif /* __JAVA__H__ */ 320 #endif /* __JAVA__H__ */