5900
|
1 #include <config.h> |
5864
|
2 |
|
3 #include <cfloat> |
|
4 #include <csetjmp> |
5900
|
5 #include <cstdarg> |
5864
|
6 #include <cstdlib> |
5900
|
7 #include <cstring> |
|
8 #include <cctype> |
|
9 |
5864
|
10 #include <set> |
5900
|
11 |
|
12 #include "f77-fcn.h" |
|
13 #include "lo-ieee.h" |
|
14 |
|
15 // mxArray must be declared as a class before including mexproto.h. |
|
16 class mxArray; |
|
17 #include "Cell.h" |
|
18 #include "mexproto.h" |
|
19 #include "oct-map.h" |
|
20 #include "oct-obj.h" |
|
21 #include "ov.h" |
|
22 #include "ov-usr-fcn.h" |
5864
|
23 #include "pager.h" |
|
24 #include "parse.h" |
|
25 #include "toplev.h" |
5900
|
26 #include "unwind-prot.h" |
|
27 #include "utils.h" |
5864
|
28 #include "variables.h" |
5900
|
29 |
|
30 // #define DEBUG 1 |
|
31 |
|
32 static int |
|
33 max_str_len (int m, const char **str) |
|
34 { |
|
35 int max_len = 0; |
|
36 |
|
37 for (int i = 0; i < m; i++) |
|
38 { |
|
39 int tmp = strlen (str[i]); |
|
40 |
|
41 if (tmp > max_len) |
|
42 max_len = tmp; |
|
43 } |
|
44 |
|
45 return max_len; |
|
46 } |
|
47 |
|
48 static int |
|
49 valid_key (const char *key) |
|
50 { |
|
51 int retval = 0; |
|
52 |
|
53 int nel = strlen (key); |
|
54 |
|
55 if (nel > 0) |
|
56 { |
|
57 if (isalpha (key[0])) |
|
58 { |
|
59 for (int i = 1; i < nel; i++) |
|
60 { |
|
61 if (! (isalnum (key[i]) || key[i] == '_')) |
|
62 goto done; |
|
63 } |
|
64 |
|
65 retval = 1; |
|
66 } |
|
67 } |
|
68 |
|
69 done: |
|
70 |
|
71 return retval; |
|
72 } |
|
73 |
|
74 // ------------------------------------------------------------------ |
|
75 |
|
76 // A class to provide the default implemenation of some of the virtual |
|
77 // functions declared in the mxArray class. |
|
78 |
|
79 class mxArray_base : public mxArray |
|
80 { |
|
81 protected: |
|
82 |
|
83 mxArray_base (void) : mxArray (xmxArray ()) { } |
|
84 |
|
85 public: |
|
86 |
|
87 mxArray *clone (void) const = 0; |
|
88 |
|
89 ~mxArray_base (void) { } |
|
90 |
|
91 octave_value as_octave_value (void) const = 0; |
|
92 |
|
93 bool is_octave_value (void) const { return false; } |
|
94 |
|
95 int is_cell (void) const = 0; |
|
96 |
|
97 int is_char (void) const = 0; |
|
98 |
|
99 int is_class (const char *name_arg) const |
|
100 { |
|
101 int retval = 0; |
|
102 |
|
103 const char *cname = get_class_name (); |
|
104 |
|
105 if (cname && name_arg) |
|
106 retval = ! strcmp (cname, name_arg); |
|
107 |
|
108 return retval; |
|
109 } |
|
110 |
|
111 int is_complex (void) const = 0; |
|
112 |
|
113 int is_double (void) const = 0; |
|
114 |
|
115 int is_int16 (void) const = 0; |
|
116 |
|
117 int is_int32 (void) const = 0; |
|
118 |
|
119 int is_int64 (void) const = 0; |
|
120 |
|
121 int is_int8 (void) const = 0; |
|
122 |
|
123 int is_logical (void) const = 0; |
|
124 |
|
125 int is_numeric (void) const = 0; |
|
126 |
|
127 int is_single (void) const = 0; |
|
128 |
|
129 int is_sparse (void) const = 0; |
|
130 |
|
131 int is_struct (void) const = 0; |
|
132 |
|
133 int is_uint16 (void) const = 0; |
|
134 |
|
135 int is_uint32 (void) const = 0; |
|
136 |
|
137 int is_uint64 (void) const = 0; |
|
138 |
|
139 int is_uint8 (void) const = 0; |
|
140 |
|
141 int is_logical_scalar (void) const |
|
142 { |
|
143 return is_logical () && get_number_of_elements () == 1; |
|
144 } |
|
145 |
|
146 int is_logical_scalar_true (void) const = 0; |
|
147 |
|
148 int get_m (void) const = 0; |
|
149 |
|
150 int get_n (void) const = 0; |
|
151 |
|
152 int *get_dimensions (void) const = 0; |
|
153 |
|
154 int get_number_of_dimensions (void) const = 0; |
|
155 |
|
156 void set_m (int m) = 0; |
|
157 |
|
158 void set_n (int n) = 0; |
|
159 |
|
160 void set_dimensions (int *dims_arg, int ndims_arg) = 0; |
|
161 |
|
162 int get_number_of_elements (void) const = 0; |
|
163 |
|
164 int is_empty (void) const = 0; |
|
165 |
|
166 mxClassID get_class_id (void) const = 0; |
|
167 |
|
168 const char *get_class_name (void) const = 0; |
|
169 |
|
170 void set_class_name (const char *name_arg) = 0; |
|
171 |
|
172 mxArray *get_cell (int /*idx*/) const |
|
173 { |
|
174 invalid_type_error (); |
|
175 return 0; |
|
176 } |
|
177 |
|
178 void set_cell (int idx, mxArray *val) = 0; |
|
179 |
|
180 void *get_data (void) const = 0; |
|
181 |
|
182 void *get_imag_data (void) const = 0; |
|
183 |
|
184 void set_data (void *pr) = 0; |
|
185 |
|
186 void set_imag_data (void *pi) = 0; |
|
187 |
|
188 int *get_ir (void) const = 0; |
|
189 |
|
190 int *get_jc (void) const = 0; |
|
191 |
|
192 int get_nzmax (void) const = 0; |
|
193 |
|
194 void set_ir (int *ir) = 0; |
|
195 |
|
196 void set_jc (int *jc) = 0; |
|
197 |
|
198 void set_nzmax (int nzmax) = 0; |
|
199 |
|
200 int add_field (const char *key) = 0; |
|
201 |
|
202 void remove_field (int key_num) = 0; |
|
203 |
|
204 mxArray *get_field_by_number (int index, int key_num) const = 0; |
|
205 |
|
206 void set_field_by_number (int index, int key_num, mxArray *val) = 0; |
|
207 |
|
208 int get_number_of_fields (void) const = 0; |
|
209 |
|
210 const char *get_field_name_by_number (int key_num) const = 0; |
|
211 |
|
212 int get_field_number (const char *key) const = 0; |
|
213 |
|
214 int get_string (char *buf, int buflen) const = 0; |
|
215 |
|
216 char *array_to_string (void) const = 0; |
|
217 |
|
218 int calc_single_subscript (int nsubs, int *subs) const = 0; |
|
219 |
|
220 int get_element_size (void) const = 0; |
|
221 |
|
222 bool mutation_needed (void) const { return false; } |
|
223 |
|
224 mxArray *mutate (void) const { return 0; } |
|
225 |
|
226 protected: |
|
227 |
|
228 mxArray_base (const mxArray_base&) : mxArray (xmxArray ()) { } |
|
229 |
|
230 void invalid_type_error (void) const |
|
231 { |
|
232 error ("invalid type for operation"); |
|
233 } |
|
234 |
|
235 void error (const char *msg) const |
|
236 { |
|
237 // FIXME |
|
238 ::error ("%s", msg); |
|
239 } |
|
240 }; |
|
241 |
|
242 // The object that handles values pass to MEX files from Octave. Some |
|
243 // methods in this class may set mutate_flag to TRUE to tell the |
|
244 // mxArray class to convert to the Matlab-style representation and |
|
245 // then invoke the method on that object instead (for example, getting |
|
246 // a pointer to real or imaginary data from a complex object requires |
|
247 // a mutation but getting a pointer to real data from a real object |
|
248 // does not). Changing the representation causes a copy so we try to |
|
249 // avoid it unless it is really necessary. Once the conversion |
|
250 // happens, we delete this representation, so the conversion can only |
|
251 // happen once per call to a MEX file. |
|
252 |
|
253 class mxArray_octave_value : public mxArray_base |
|
254 { |
|
255 public: |
|
256 |
|
257 mxArray_octave_value (const octave_value& ov) |
|
258 : mxArray_base (), val (ov), mutate_flag (false), |
|
259 id (mxUNKNOWN_CLASS), class_name (0), ndims (-1), dims (0) { } |
|
260 |
|
261 mxArray *clone (void) const { return new mxArray_octave_value (*this); } |
|
262 |
|
263 ~mxArray_octave_value (void) |
|
264 { |
|
265 mxFree (class_name); |
|
266 mxFree (dims); |
|
267 } |
|
268 |
|
269 octave_value as_octave_value (void) const { return val; } |
|
270 |
|
271 bool is_octave_value (void) const { return true; } |
|
272 |
|
273 int is_cell (void) const { return val.is_cell (); } |
|
274 |
|
275 int is_char (void) const { return val.is_string (); } |
|
276 |
|
277 int is_complex (void) const { return val.is_complex_type (); } |
|
278 |
|
279 int is_double (void) const { return val.is_double_type (); } |
|
280 |
|
281 int is_int16 (void) const { return val.is_int16_type (); } |
|
282 |
|
283 int is_int32 (void) const { return val.is_int32_type (); } |
|
284 |
|
285 int is_int64 (void) const { return val.is_int64_type (); } |
|
286 |
|
287 int is_int8 (void) const { return val.is_int8_type (); } |
|
288 |
|
289 int is_logical (void) const { return val.is_bool_type (); } |
|
290 |
|
291 int is_numeric (void) const { return val.is_numeric_type (); } |
|
292 |
|
293 int is_single (void) const { return val.is_single_type (); } |
|
294 |
|
295 int is_sparse (void) const { return val.is_sparse_type (); } |
|
296 |
|
297 int is_struct (void) const { return val.is_map (); } |
|
298 |
|
299 int is_uint16 (void) const { return val.is_uint16_type (); } |
|
300 |
|
301 int is_uint32 (void) const { return val.is_int32_type (); } |
|
302 |
|
303 int is_uint64 (void) const { return val.is_int64_type (); } |
|
304 |
|
305 int is_uint8 (void) const { return val.is_int8_type (); } |
|
306 |
|
307 int is_range (void) const { return val.is_range (); } |
|
308 |
|
309 int is_real_type (void) const { return val.is_real_type (); } |
|
310 |
|
311 int is_logical_scalar_true (void) const |
|
312 { |
|
313 return (is_logical_scalar () && val.is_true ()); |
|
314 } |
|
315 |
|
316 int get_m (void) const { return val.rows (); } |
|
317 |
|
318 int get_n (void) const { return val.columns (); } |
|
319 |
|
320 int *get_dimensions (void) const |
|
321 { |
|
322 if (! dims) |
|
323 { |
|
324 // Force ndims to be cached. |
|
325 get_number_of_dimensions (); |
|
326 |
|
327 dims = static_cast<int *> (malloc (ndims * sizeof (int))); |
|
328 |
|
329 dim_vector dv = val.dims (); |
|
330 |
|
331 for (int i = 0; i < ndims; i++) |
|
332 dims[i] = dv(i); |
|
333 } |
|
334 |
|
335 return dims; |
|
336 } |
|
337 |
|
338 int get_number_of_dimensions (void) const |
|
339 { |
|
340 if (ndims < 0) |
|
341 ndims = val.ndims (); |
|
342 |
|
343 return ndims; |
|
344 } |
|
345 |
|
346 void set_m (int /*m*/) { panic_impossible (); } |
|
347 |
|
348 void set_n (int /*n*/) { panic_impossible (); } |
|
349 |
|
350 void set_dimensions (int */*dims_arg*/, int /*ndims_arg*/) |
|
351 { |
|
352 panic_impossible (); |
|
353 } |
|
354 |
|
355 int get_number_of_elements (void) const { return val.numel (); } |
|
356 |
|
357 int is_empty (void) const { return val.is_empty (); } |
|
358 |
|
359 mxClassID get_class_id (void) const |
|
360 { |
|
361 id = mxUNKNOWN_CLASS; |
|
362 |
|
363 std::string cn = val.class_name (); |
|
364 |
|
365 if (cn == "cell") |
|
366 id = mxCELL_CLASS; |
|
367 else if (cn == "struct") |
|
368 id = mxSTRUCT_CLASS; |
|
369 else if (cn == "logical") |
|
370 id = mxLOGICAL_CLASS; |
|
371 else if (cn == "char") |
|
372 id = mxCHAR_CLASS; |
|
373 else if (cn == "double") |
|
374 id = mxDOUBLE_CLASS; |
|
375 else if (cn == "single") |
|
376 id = mxSINGLE_CLASS; |
|
377 else if (cn == "int8") |
|
378 id = mxINT8_CLASS; |
|
379 else if (cn == "uint8") |
|
380 id = mxUINT8_CLASS; |
|
381 else if (cn == "int16") |
|
382 id = mxINT16_CLASS; |
|
383 else if (cn == "uint16") |
|
384 id = mxUINT16_CLASS; |
|
385 else if (cn == "int32") |
|
386 id = mxINT32_CLASS; |
|
387 else if (cn == "uint32") |
|
388 id = mxUINT32_CLASS; |
|
389 else if (cn == "int64") |
|
390 id = mxINT64_CLASS; |
|
391 else if (cn == "uint64") |
|
392 id = mxUINT64_CLASS; |
|
393 else if (cn == "function handle") |
|
394 id = mxFUNCTION_CLASS; |
|
395 |
|
396 return id; |
|
397 } |
|
398 |
|
399 const char *get_class_name (void) const |
|
400 { |
|
401 if (! class_name) |
|
402 { |
|
403 std::string s = val.class_name (); |
|
404 class_name = strsave (s.c_str ()); |
|
405 } |
|
406 |
|
407 return class_name; |
|
408 } |
|
409 |
|
410 // Not allowed. |
|
411 void set_class_name (const char */*name_arg*/) { panic_impossible (); } |
|
412 |
|
413 mxArray *get_cell (int /*idx*/) const |
|
414 { |
|
415 request_mutation (); |
|
416 return 0; |
|
417 } |
|
418 |
|
419 // Not allowed. |
|
420 void set_cell (int /*idx*/, mxArray */*val*/) { panic_impossible (); } |
|
421 |
|
422 void *get_data (void) const |
|
423 { |
|
424 void *retval = 0; |
|
425 |
|
426 if (is_char () |
|
427 || (is_numeric () && is_real_type () && ! is_range ())) |
|
428 retval = val.mex_get_data (); |
|
429 else |
|
430 request_mutation (); |
|
431 |
|
432 return retval; |
|
433 } |
|
434 |
|
435 void *get_imag_data (void) const |
|
436 { |
|
437 void *retval = 0; |
|
438 |
|
439 if (is_numeric () && is_real_type ()) |
|
440 retval = 0; |
|
441 else |
|
442 request_mutation (); |
|
443 |
|
444 return retval; |
|
445 } |
|
446 |
|
447 // Not allowed. |
|
448 void set_data (void */*pr*/) { panic_impossible (); } |
|
449 |
|
450 // Not allowed. |
|
451 void set_imag_data (void */*pi*/) { panic_impossible (); } |
|
452 |
|
453 int *get_ir (void) const |
|
454 { |
|
455 #if SIZEOF_OCTAVE_IDX_TYPE == SIZEOF_INT |
5902
|
456 return val.mex_get_ir (); |
5900
|
457 #else |
|
458 request_mutation (); |
|
459 return 0; |
|
460 #endif |
|
461 } |
|
462 |
|
463 int *get_jc (void) const |
|
464 { |
|
465 #if SIZEOF_OCTAVE_IDX_TYPE == SIZEOF_INT |
|
466 return val.mex_get_jc (); |
|
467 #else |
|
468 request_mutation (); |
|
469 return 0; |
|
470 #endif |
|
471 } |
|
472 |
|
473 int get_nzmax (void) const { return val.nzmax (); } |
|
474 |
|
475 // Not allowed. |
|
476 void set_ir (int */*ir*/) { panic_impossible (); } |
|
477 |
|
478 // Not allowed. |
|
479 void set_jc (int */*jc*/) { panic_impossible (); } |
|
480 |
|
481 // Not allowed. |
|
482 void set_nzmax (int /*nzmax*/) { panic_impossible (); } |
|
483 |
|
484 // Not allowed. |
|
485 int add_field (const char */*key*/) |
|
486 { |
|
487 panic_impossible (); |
|
488 return -1; |
|
489 } |
|
490 |
|
491 // Not allowed. |
|
492 void remove_field (int /*key_num*/) { panic_impossible (); } |
|
493 |
|
494 mxArray *get_field_by_number (int /*index*/, int /*key_num*/) const |
|
495 { |
|
496 request_mutation (); |
|
497 return 0; |
|
498 } |
|
499 |
|
500 // Not allowed. |
|
501 void set_field_by_number (int /*index*/, int /*key_num*/, mxArray */*val*/) |
|
502 { |
|
503 panic_impossible (); |
|
504 } |
|
505 |
|
506 int get_number_of_fields (void) const { return val.nfields (); } |
|
507 |
|
508 const char *get_field_name_by_number (int /*key_num*/) const |
|
509 { |
|
510 request_mutation (); |
|
511 return 0; |
|
512 } |
|
513 |
|
514 int get_field_number (const char */*key*/) const |
|
515 { |
|
516 request_mutation (); |
|
517 return 0; |
|
518 } |
|
519 |
|
520 int get_string (char *buf, int buflen) const |
|
521 { |
|
522 int retval = 1; |
|
523 |
|
524 int nel = get_number_of_elements (); |
|
525 |
|
526 if (val.is_string () && nel < buflen) |
|
527 { |
|
528 charNDArray tmp = val.char_array_value (); |
|
529 |
|
530 const char *p = tmp.data (); |
|
531 |
|
532 for (int i = 0; i < buflen; i++) |
|
533 buf[i] = p[i]; |
|
534 |
|
535 buf[nel] = 0; |
|
536 |
|
537 retval = 0; |
|
538 } |
|
539 |
|
540 return retval; |
|
541 } |
|
542 |
|
543 char *array_to_string (void) const |
|
544 { |
|
545 // FIXME -- this is suposed to handle multi-byte character |
|
546 // strings. |
|
547 |
|
548 char *buf = 0; |
|
549 |
|
550 if (val.is_string ()) |
|
551 { |
|
552 int nel = get_number_of_elements (); |
|
553 |
|
554 buf = static_cast<char *> (malloc (nel + 1)); |
|
555 |
|
556 if (buf) |
|
557 { |
|
558 charNDArray tmp = val.char_array_value (); |
|
559 |
|
560 const char *p = tmp.data (); |
|
561 |
|
562 for (int i = 0; i < nel; i++) |
|
563 buf[i] = p[i]; |
|
564 |
|
565 buf[nel] = '\0'; |
|
566 } |
|
567 } |
|
568 |
|
569 return buf; |
|
570 } |
|
571 |
|
572 int calc_single_subscript (int nsubs, int *subs) const |
|
573 { |
|
574 int retval = 0; |
|
575 |
|
576 // Force ndims, dims to be cached. |
|
577 get_dimensions (); |
|
578 |
|
579 int n = nsubs <= ndims ? nsubs : ndims; |
|
580 |
|
581 while (--n > 0) |
|
582 retval = retval * dims[n] + subs[n]; |
|
583 |
|
584 return retval; |
|
585 } |
|
586 |
|
587 int get_element_size (void) const |
|
588 { |
|
589 // Force id to be cached. |
|
590 get_class_id (); |
|
591 |
|
592 switch (id) |
|
593 { |
|
594 case mxCELL_CLASS: return sizeof (mxArray *); |
|
595 case mxSTRUCT_CLASS: return sizeof (mxArray *); |
|
596 case mxLOGICAL_CLASS: return sizeof (mxLogical); |
|
597 case mxCHAR_CLASS: return sizeof (mxChar); |
|
598 case mxDOUBLE_CLASS: return sizeof (double); |
|
599 case mxSINGLE_CLASS: return sizeof (float); |
|
600 case mxINT8_CLASS: return 1; |
|
601 case mxUINT8_CLASS: return 1; |
|
602 case mxINT16_CLASS: return 2; |
|
603 case mxUINT16_CLASS: return 2; |
|
604 case mxINT32_CLASS: return 4; |
|
605 case mxUINT32_CLASS: return 4; |
|
606 case mxINT64_CLASS: return 8; |
|
607 case mxUINT64_CLASS: return 8; |
|
608 case mxFUNCTION_CLASS: return 0; |
|
609 default: return 0; |
|
610 } |
|
611 } |
|
612 |
|
613 bool mutation_needed (void) const { return mutate_flag; } |
|
614 |
|
615 void request_mutation (void) const |
|
616 { |
|
617 if (mutate_flag) |
|
618 panic_impossible (); |
|
619 |
|
620 mutate_flag = true; |
|
621 } |
|
622 |
|
623 mxArray *mutate (void) const { return val.as_mxArray (); } |
|
624 |
|
625 protected: |
|
626 |
|
627 mxArray_octave_value (const mxArray_octave_value& arg) |
|
628 : mxArray_base (arg), val (arg.val), mutate_flag (arg.mutate_flag), |
|
629 id (arg.id), class_name (strsave (arg.class_name)), ndims (arg.ndims), |
|
630 dims (ndims > 0 ? static_cast<int *> (malloc (ndims * sizeof (int))) : 0) |
|
631 { |
|
632 if (dims) |
|
633 { |
|
634 for (int i = 0; i < ndims; i++) |
|
635 dims[i] = arg.dims[i]; |
|
636 } |
|
637 } |
|
638 |
|
639 private: |
|
640 |
|
641 octave_value val; |
|
642 |
|
643 mutable bool mutate_flag; |
|
644 |
|
645 // Caching these does not cost much or lead to much duplicated |
|
646 // code. For other things, we just request mutation to a |
|
647 // Matlab-style mxArray object. |
|
648 |
|
649 mutable mxClassID id; |
|
650 mutable char *class_name; |
|
651 mutable int ndims; |
|
652 mutable int *dims; |
|
653 }; |
|
654 |
|
655 // The base class for the Matlab-style representation, used to handle |
|
656 // things that are common to all Matlab-style objects. |
|
657 |
|
658 class mxArray_matlab : public mxArray_base |
|
659 { |
|
660 protected: |
|
661 |
|
662 mxArray_matlab (mxClassID id_arg = mxUNKNOWN_CLASS) |
|
663 : mxArray_base (), class_name (0), id (id_arg), ndims (0), dims (0) { } |
|
664 |
|
665 mxArray_matlab (mxClassID id_arg, int ndims_arg, const int *dims_arg) |
|
666 : mxArray_base (), class_name (0), id (id_arg), |
|
667 ndims (ndims_arg < 2 ? 2 : ndims_arg), |
|
668 dims (static_cast<int *> (malloc (ndims * sizeof (int)))) |
|
669 { |
|
670 if (ndims_arg < 2) |
|
671 { |
|
672 dims[0] = 1; |
|
673 dims[1] = 1; |
|
674 } |
|
675 |
|
676 for (int i = 0; i < ndims_arg; i++) |
|
677 dims[i] = dims_arg[i]; |
|
678 |
|
679 for (int i = ndims - 1; i > 1; i--) |
|
680 { |
|
681 if (dims[i] == 1) |
|
682 ndims--; |
|
683 else |
|
684 break; |
|
685 } |
|
686 } |
|
687 |
|
688 mxArray_matlab (mxClassID id_arg, const dim_vector& dv) |
|
689 : mxArray_base (), class_name (0), id (id_arg), |
|
690 ndims (dv.length ()), |
|
691 dims (static_cast<int *> (malloc (ndims * sizeof (int)))) |
|
692 { |
|
693 for (int i = 0; i < ndims; i++) |
|
694 dims[i] = dv(i); |
|
695 |
|
696 for (int i = ndims - 1; i > 1; i--) |
|
697 { |
|
698 if (dims[i] == 1) |
|
699 ndims--; |
|
700 else |
|
701 break; |
|
702 } |
|
703 } |
|
704 |
|
705 mxArray_matlab (mxClassID id_arg, int m, int n) |
|
706 : mxArray_base (), class_name (0), id (id_arg), ndims (2), |
|
707 dims (static_cast<int *> (malloc (ndims * sizeof (int)))) |
|
708 { |
|
709 dims[0] = m; |
|
710 dims[1] = n; |
|
711 } |
|
712 |
|
713 public: |
|
714 |
|
715 ~mxArray_matlab (void) |
|
716 { |
|
717 mxFree (class_name); |
|
718 mxFree (dims); |
|
719 } |
|
720 |
|
721 int is_cell (void) const { return id == mxCELL_CLASS; } |
|
722 |
|
723 int is_char (void) const { return id == mxCHAR_CLASS; } |
|
724 |
|
725 int is_complex (void) const { return 0; } |
|
726 |
|
727 int is_double (void) const { return id == mxDOUBLE_CLASS; } |
|
728 |
|
729 int is_int16 (void) const { return id == mxINT16_CLASS; } |
|
730 |
|
731 int is_int32 (void) const { return id == mxINT32_CLASS; } |
|
732 |
|
733 int is_int64 (void) const { return id == mxINT64_CLASS; } |
|
734 |
|
735 int is_int8 (void) const { return id == mxINT8_CLASS; } |
|
736 |
|
737 int is_logical (void) const { return id == mxLOGICAL_CLASS; } |
|
738 |
|
739 int is_numeric (void) const |
|
740 { |
|
741 return (id == mxDOUBLE_CLASS || id == mxSINGLE_CLASS |
|
742 || id == mxINT8_CLASS || id == mxUINT8_CLASS |
|
743 || id == mxINT16_CLASS || id == mxUINT16_CLASS |
|
744 || id == mxINT32_CLASS || id == mxUINT32_CLASS |
|
745 || id == mxINT64_CLASS || id == mxUINT64_CLASS); |
|
746 } |
|
747 |
|
748 int is_single (void) const { return id == mxSINGLE_CLASS; } |
|
749 |
|
750 int is_sparse (void) const { return 0; } |
|
751 |
|
752 int is_struct (void) const { return id == mxSTRUCT_CLASS; } |
|
753 |
|
754 int is_uint16 (void) const { return id == mxUINT16_CLASS; } |
|
755 |
|
756 int is_uint32 (void) const { return id == mxUINT32_CLASS; } |
|
757 |
|
758 int is_uint64 (void) const { return id == mxUINT64_CLASS; } |
|
759 |
|
760 int is_uint8 (void) const { return id == mxUINT8_CLASS; } |
|
761 |
|
762 int is_logical_scalar_true (void) const |
|
763 { |
|
764 return (is_logical_scalar () |
|
765 && static_cast<mxLogical *> (get_data ())[0] != 0); |
|
766 } |
|
767 |
|
768 int get_m (void) const { return dims[0]; } |
|
769 |
|
770 int get_n (void) const { return dims[1]; } |
|
771 |
|
772 int *get_dimensions (void) const { return dims; } |
|
773 |
|
774 int get_number_of_dimensions (void) const { return ndims; } |
|
775 |
|
776 void set_m (int m) { dims[0] = m; } |
|
777 |
|
778 void set_n (int n) { dims[1] = n; } |
|
779 |
|
780 void set_dimensions (int *dims_arg, int ndims_arg) |
|
781 { |
|
782 dims = dims_arg; |
|
783 ndims = ndims_arg; |
|
784 } |
|
785 |
|
786 int get_number_of_elements (void) const |
|
787 { |
|
788 int retval = dims[0]; |
|
789 |
|
790 for (int i = 1; i < ndims; i++) |
|
791 retval *= dims[i]; |
|
792 |
|
793 return retval; |
|
794 } |
|
795 |
|
796 int is_empty (void) const { return get_number_of_elements () == 0; } |
|
797 |
|
798 mxClassID get_class_id (void) const { return id; } |
|
799 |
|
800 const char *get_class_name (void) const |
|
801 { |
|
802 switch (id) |
|
803 { |
|
804 case mxCELL_CLASS: return "cell"; |
|
805 case mxSTRUCT_CLASS: return "struct"; |
|
806 case mxLOGICAL_CLASS: return "logical"; |
|
807 case mxCHAR_CLASS: return "char"; |
|
808 case mxDOUBLE_CLASS: return "double"; |
|
809 case mxSINGLE_CLASS: return "single"; |
|
810 case mxINT8_CLASS: return "int8"; |
|
811 case mxUINT8_CLASS: return "uint8"; |
|
812 case mxINT16_CLASS: return "int16"; |
|
813 case mxUINT16_CLASS: return "uint16"; |
|
814 case mxINT32_CLASS: return "int32"; |
|
815 case mxUINT32_CLASS: return "uint32"; |
|
816 case mxINT64_CLASS: return "int64"; |
|
817 case mxUINT64_CLASS: return "uint64"; |
|
818 case mxFUNCTION_CLASS: return "function handle"; |
|
819 default: return "unknown"; |
|
820 } |
|
821 } |
|
822 |
|
823 void set_class_name (const char *name_arg) |
|
824 { |
|
825 mxFree (class_name); |
|
826 class_name = static_cast<char *> (malloc (strlen (name_arg) + 1)); |
|
827 strcpy (class_name, name_arg); |
|
828 } |
|
829 |
|
830 mxArray *get_cell (int /*idx*/) const |
|
831 { |
|
832 invalid_type_error (); |
|
833 return 0; |
|
834 } |
|
835 |
|
836 void set_cell (int /*idx*/, mxArray */*val*/) |
|
837 { |
|
838 invalid_type_error (); |
|
839 } |
|
840 |
|
841 void *get_data (void) const |
|
842 { |
|
843 invalid_type_error (); |
|
844 return 0; |
|
845 } |
|
846 |
|
847 void *get_imag_data (void) const |
|
848 { |
|
849 invalid_type_error (); |
|
850 return 0; |
|
851 } |
|
852 |
|
853 void set_data (void */*pr*/) |
|
854 { |
|
855 invalid_type_error (); |
|
856 } |
|
857 |
|
858 void set_imag_data (void */*pi*/) |
|
859 { |
|
860 invalid_type_error (); |
|
861 } |
|
862 |
|
863 int *get_ir (void) const |
|
864 { |
|
865 invalid_type_error (); |
|
866 return 0; |
|
867 } |
|
868 |
|
869 int *get_jc (void) const |
|
870 { |
|
871 invalid_type_error (); |
|
872 return 0; |
|
873 } |
|
874 |
|
875 int get_nzmax (void) const |
|
876 { |
|
877 invalid_type_error (); |
|
878 return 0; |
|
879 } |
|
880 |
|
881 void set_ir (int */*ir*/) |
|
882 { |
|
883 invalid_type_error (); |
|
884 } |
|
885 |
|
886 void set_jc (int */*jc*/) |
|
887 { |
|
888 invalid_type_error (); |
|
889 } |
|
890 |
|
891 void set_nzmax (int /*nzmax*/) |
|
892 { |
|
893 invalid_type_error (); |
|
894 } |
|
895 |
|
896 int add_field (const char */*key*/) |
|
897 { |
|
898 invalid_type_error (); |
|
899 return -1; |
|
900 } |
|
901 |
|
902 void remove_field (int /*key_num*/) |
|
903 { |
|
904 invalid_type_error (); |
|
905 } |
|
906 |
|
907 mxArray *get_field_by_number (int /*index*/, int /*key_num*/) const |
|
908 { |
|
909 invalid_type_error (); |
|
910 return 0; |
|
911 } |
|
912 |
|
913 void set_field_by_number (int /*index*/, int /*key_num*/, mxArray */*val*/) |
|
914 { |
|
915 invalid_type_error (); |
|
916 } |
|
917 |
|
918 int get_number_of_fields (void) const |
|
919 { |
|
920 invalid_type_error (); |
|
921 return 0; |
|
922 } |
|
923 |
|
924 const char *get_field_name_by_number (int /*key_num*/) const |
|
925 { |
|
926 invalid_type_error (); |
|
927 return 0; |
|
928 } |
|
929 |
|
930 int get_field_number (const char */*key*/) const |
|
931 { |
|
932 return -1; |
|
933 } |
|
934 |
|
935 int get_string (char */*buf*/, int /*buflen*/) const |
|
936 { |
|
937 invalid_type_error (); |
|
938 return 0; |
|
939 } |
|
940 |
|
941 char *array_to_string (void) const |
|
942 { |
|
943 invalid_type_error (); |
|
944 return 0; |
|
945 } |
|
946 |
|
947 int calc_single_subscript (int nsubs, int *subs) const |
|
948 { |
|
949 int retval = 0; |
|
950 |
|
951 int n = nsubs <= ndims ? nsubs : ndims; |
|
952 |
|
953 while (--n > 0) |
|
954 retval = retval * dims[n] + subs[n]; |
|
955 |
|
956 return retval; |
|
957 } |
|
958 |
|
959 int get_element_size (void) const |
|
960 { |
|
961 switch (id) |
|
962 { |
|
963 case mxCELL_CLASS: return sizeof (mxArray *); |
|
964 case mxSTRUCT_CLASS: return sizeof (mxArray *); |
|
965 case mxLOGICAL_CLASS: return sizeof (mxLogical); |
|
966 case mxCHAR_CLASS: return sizeof (mxChar); |
|
967 case mxDOUBLE_CLASS: return sizeof (double); |
|
968 case mxSINGLE_CLASS: return sizeof (float); |
|
969 case mxINT8_CLASS: return 1; |
|
970 case mxUINT8_CLASS: return 1; |
|
971 case mxINT16_CLASS: return 2; |
|
972 case mxUINT16_CLASS: return 2; |
|
973 case mxINT32_CLASS: return 4; |
|
974 case mxUINT32_CLASS: return 4; |
|
975 case mxINT64_CLASS: return 8; |
|
976 case mxUINT64_CLASS: return 8; |
|
977 case mxFUNCTION_CLASS: return 0; |
|
978 default: return 0; |
|
979 } |
|
980 } |
|
981 |
|
982 protected: |
|
983 |
|
984 mxArray_matlab (const mxArray_matlab& val) |
|
985 : mxArray_base (val), class_name (strsave (val.class_name)), |
|
986 id (val.id), ndims (val.ndims), |
|
987 dims (static_cast<int *> (malloc (ndims * sizeof (int)))) |
|
988 { |
|
989 for (int i = 0; i < ndims; i++) |
|
990 dims[i] = val.dims[i]; |
|
991 } |
|
992 |
|
993 dim_vector |
|
994 dims_to_dim_vector (void) const |
|
995 { |
|
996 int nd = get_number_of_dimensions (); |
|
997 |
|
998 int *d = get_dimensions (); |
|
999 |
|
1000 dim_vector dv; |
|
1001 dv.resize (nd); |
|
1002 |
|
1003 for (int i = 0; i < nd; i++) |
|
1004 dv(i) = d[i]; |
|
1005 |
|
1006 return dv; |
|
1007 } |
|
1008 |
|
1009 private: |
|
1010 |
|
1011 char *class_name; |
|
1012 |
|
1013 mxClassID id; |
|
1014 |
|
1015 int ndims; |
|
1016 int *dims; |
|
1017 |
|
1018 void invalid_type_error (void) const |
|
1019 { |
|
1020 error ("invalid type for operation"); |
|
1021 } |
|
1022 }; |
|
1023 |
|
1024 // Matlab-style numeric, character, and logical data. |
|
1025 |
|
1026 class mxArray_number : public mxArray_matlab |
|
1027 { |
|
1028 public: |
|
1029 |
|
1030 mxArray_number (mxClassID id_arg, int ndims_arg, const int *dims_arg, |
|
1031 mxComplexity flag = mxREAL) |
|
1032 : mxArray_matlab (id_arg, ndims_arg, dims_arg), |
|
1033 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1034 pi (flag == mxCOMPLEX ? calloc (get_number_of_elements (), get_element_size ()) : 0) { } |
|
1035 |
|
1036 mxArray_number (mxClassID id_arg, const dim_vector& dv, |
|
1037 mxComplexity flag = mxREAL) |
|
1038 : mxArray_matlab (id_arg, dv), |
|
1039 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1040 pi (flag == mxCOMPLEX ? calloc (get_number_of_elements (), get_element_size ()) : 0) { } |
|
1041 |
|
1042 mxArray_number (mxClassID id_arg, int m, int n, mxComplexity flag = mxREAL) |
|
1043 : mxArray_matlab (id_arg, m, n), |
|
1044 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1045 pi (flag == mxCOMPLEX ? calloc (get_number_of_elements (), get_element_size ()) : 0) { } |
|
1046 |
|
1047 mxArray_number (mxClassID id_arg, double val) |
|
1048 : mxArray_matlab (id_arg, 1, 1), |
|
1049 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1050 pi (0) |
|
1051 { |
|
1052 double *dpr = static_cast<double *> (pr); |
|
1053 dpr[0] = val; |
|
1054 } |
|
1055 |
|
1056 mxArray_number (mxClassID id_arg, mxLogical val) |
|
1057 : mxArray_matlab (id_arg, 1, 1), |
|
1058 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1059 pi (0) |
|
1060 { |
|
1061 mxLogical *lpr = static_cast<mxLogical *> (pr); |
|
1062 lpr[0] = val; |
|
1063 } |
|
1064 |
|
1065 mxArray_number (const char *str) |
|
1066 : mxArray_matlab (mxCHAR_CLASS, 1, strlen (str)), |
|
1067 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1068 pi (0) |
|
1069 { |
|
1070 mxChar *cpr = static_cast<mxChar *> (pr); |
|
1071 int nel = get_number_of_elements (); |
|
1072 for (int i = 0; i < nel; i++) |
|
1073 cpr[i] = str[i]; |
|
1074 } |
|
1075 |
|
1076 mxArray_number (int m, const char **str) |
|
1077 : mxArray_matlab (mxCHAR_CLASS, m, max_str_len (m, str)), |
|
1078 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1079 pi (0) |
|
1080 { |
|
1081 mxChar *cpr = static_cast<mxChar *> (pr); |
|
1082 |
|
1083 int *dv = get_dimensions (); |
|
1084 |
|
1085 int nc = dv[1]; |
|
1086 |
|
1087 for (int j = 0; j < m; j++) |
|
1088 { |
|
1089 const char *ptr = str[j]; |
|
1090 |
|
1091 int tmp_len = strlen (ptr); |
|
1092 |
|
1093 for (int i = 0; i < tmp_len; i++) |
|
1094 cpr[i] = static_cast<mxChar> (ptr[i]); |
|
1095 |
|
1096 for (int i = tmp_len; i < nc; i++) |
|
1097 cpr[i] = static_cast<mxChar> (' '); |
|
1098 } |
|
1099 } |
|
1100 |
|
1101 mxArray_number *clone (void) const { return new mxArray_number (*this); } |
|
1102 |
|
1103 ~mxArray_number (void) |
|
1104 { |
|
1105 mxFree (pr); |
|
1106 mxFree (pi); |
|
1107 } |
|
1108 |
|
1109 template <typename ELT_T, typename ARRAY_T, typename ARRAY_ELT_T> |
|
1110 octave_value |
|
1111 int_to_ov (const dim_vector& dv) const |
|
1112 { |
|
1113 octave_value retval; |
|
1114 |
|
1115 int nel = get_number_of_elements (); |
|
1116 |
|
1117 ELT_T *ppr = static_cast<ELT_T *> (pr); |
|
1118 |
|
1119 if (pi) |
|
1120 error ("complex integer types are not supported"); |
|
1121 else |
|
1122 { |
|
1123 ARRAY_T val (dv); |
|
1124 |
|
1125 ARRAY_ELT_T *ptr = val.fortran_vec (); |
|
1126 |
|
1127 for (int i = 0; i < nel; i++) |
|
1128 ptr[i] = ppr[i]; |
|
1129 |
|
1130 retval = val; |
|
1131 } |
|
1132 |
|
1133 return retval; |
|
1134 } |
|
1135 |
|
1136 octave_value as_octave_value (void) const |
|
1137 { |
|
1138 octave_value retval; |
|
1139 |
|
1140 dim_vector dv = dims_to_dim_vector (); |
|
1141 |
|
1142 switch (get_class_id ()) |
|
1143 { |
|
1144 case mxLOGICAL_CLASS: |
|
1145 retval = int_to_ov<bool, boolNDArray, bool> (dv); |
|
1146 break; |
|
1147 |
|
1148 case mxCHAR_CLASS: |
|
1149 { |
|
1150 int nel = get_number_of_elements (); |
|
1151 |
|
1152 mxChar *ppr = static_cast<mxChar *> (pr); |
|
1153 |
|
1154 charNDArray val (dv); |
|
1155 |
|
1156 char *ptr = val.fortran_vec (); |
|
1157 |
|
1158 for (int i = 0; i < nel; i++) |
|
1159 ptr[i] = static_cast<char> (ppr[i]); |
|
1160 |
|
1161 retval = octave_value (val, true, '\''); |
|
1162 } |
|
1163 break; |
|
1164 |
|
1165 case mxSINGLE_CLASS: |
|
1166 error ("single precision data type not supported"); |
|
1167 break; |
|
1168 |
|
1169 case mxDOUBLE_CLASS: |
|
1170 { |
|
1171 int nel = get_number_of_elements (); |
|
1172 |
|
1173 double *ppr = static_cast<double *> (pr); |
|
1174 |
|
1175 if (pi) |
|
1176 { |
|
1177 ComplexNDArray val (dv); |
|
1178 |
|
1179 Complex *ptr = val.fortran_vec (); |
|
1180 |
|
1181 double *ppi = static_cast<double *> (pi); |
|
1182 |
|
1183 for (int i = 0; i < nel; i++) |
|
1184 ptr[i] = Complex (ppr[i], ppi[i]); |
|
1185 |
|
1186 retval = val; |
|
1187 } |
|
1188 else |
|
1189 { |
|
1190 NDArray val (dv); |
|
1191 |
|
1192 double *ptr = val.fortran_vec (); |
|
1193 |
|
1194 for (int i = 0; i < nel; i++) |
|
1195 ptr[i] = ppr[i]; |
|
1196 |
|
1197 retval = val; |
|
1198 } |
|
1199 } |
|
1200 break; |
|
1201 |
|
1202 case mxINT8_CLASS: |
|
1203 retval = int_to_ov<int8_t, int8NDArray, octave_int8> (dv); |
|
1204 break; |
|
1205 |
|
1206 case mxUINT8_CLASS: |
|
1207 retval = int_to_ov<uint8_t, uint8NDArray, octave_uint8> (dv); |
|
1208 break; |
|
1209 |
|
1210 case mxINT16_CLASS: |
|
1211 retval = int_to_ov<int16_t, int16NDArray, octave_int16> (dv); |
|
1212 break; |
|
1213 |
|
1214 case mxUINT16_CLASS: |
|
1215 retval = int_to_ov<uint16_t, uint16NDArray, octave_uint16> (dv); |
|
1216 break; |
|
1217 |
|
1218 case mxINT32_CLASS: |
|
1219 retval = int_to_ov<int32_t, int32NDArray, octave_int32> (dv); |
|
1220 break; |
|
1221 |
|
1222 case mxUINT32_CLASS: |
|
1223 retval = int_to_ov<uint32_t, uint32NDArray, octave_uint32> (dv); |
|
1224 break; |
|
1225 |
|
1226 case mxINT64_CLASS: |
|
1227 retval = int_to_ov<int64_t, int64NDArray, octave_int64> (dv); |
|
1228 break; |
|
1229 |
|
1230 case mxUINT64_CLASS: |
|
1231 retval = int_to_ov<uint64_t, uint64NDArray, octave_uint64> (dv); |
|
1232 break; |
|
1233 |
|
1234 default: |
|
1235 panic_impossible (); |
|
1236 } |
|
1237 |
|
1238 return retval; |
|
1239 } |
|
1240 |
|
1241 int is_complex (void) const { return pi != 0; } |
|
1242 |
|
1243 void *get_data (void) const { return pr; } |
|
1244 |
|
1245 void *get_imag_data (void) const { return pi; } |
|
1246 |
|
1247 void set_data (void *pr_arg) { pr = pr_arg; } |
|
1248 |
|
1249 void set_imag_data (void *pi_arg) { pi = pi_arg; } |
|
1250 |
|
1251 int get_string (char *buf, int buflen) const |
|
1252 { |
|
1253 int retval = 1; |
|
1254 |
|
1255 int n = get_number_of_elements (); |
|
1256 |
|
1257 if (n < buflen) |
|
1258 { |
|
1259 mxChar *ptr = static_cast<mxChar *> (pr); |
|
1260 |
|
1261 for (int i = 0; i < n; i++) |
|
1262 buf[i] = static_cast<char> (ptr[i]); |
|
1263 |
|
1264 buf[n] = 0; |
|
1265 } |
|
1266 |
|
1267 return retval; |
|
1268 } |
|
1269 |
|
1270 char *array_to_string (void) const |
|
1271 { |
|
1272 // FIXME -- this is suposed to handle multi-byte character |
|
1273 // strings. |
|
1274 |
|
1275 int nel = get_number_of_elements (); |
|
1276 |
|
1277 char *buf = static_cast<char *> (malloc (nel + 1)); |
|
1278 |
|
1279 if (buf) |
|
1280 { |
|
1281 mxChar *ptr = static_cast<mxChar *> (pr); |
|
1282 |
|
1283 for (int i = 0; i < nel; i++) |
|
1284 buf[i] = static_cast<char> (ptr[i]); |
|
1285 |
|
1286 buf[nel] = '\0'; |
|
1287 } |
|
1288 |
|
1289 return buf; |
|
1290 } |
|
1291 |
|
1292 protected: |
|
1293 |
|
1294 mxArray_number (const mxArray_number& val) |
|
1295 : mxArray_matlab (val), |
|
1296 pr (malloc (get_number_of_elements () * get_element_size ())), |
|
1297 pi (val.pi ? malloc (get_number_of_elements () * get_element_size ()) : 0) |
|
1298 { |
|
1299 int ntot = get_number_of_elements () * get_element_size (); |
|
1300 |
|
1301 memcpy (pr, val.pr, ntot); |
|
1302 |
|
1303 if (pi) |
|
1304 memcpy (pi, val.pi, ntot); |
|
1305 } |
|
1306 |
|
1307 private: |
|
1308 |
|
1309 void *pr; |
|
1310 void *pi; |
|
1311 }; |
|
1312 |
|
1313 // Matlab-style sparse arrays. |
|
1314 |
|
1315 class mxArray_sparse : public mxArray_number |
|
1316 { |
|
1317 public: |
|
1318 |
|
1319 mxArray_sparse (mxClassID id_arg, int m, int n, int nzmax_arg, |
|
1320 mxComplexity flag = mxREAL) |
|
1321 : mxArray_number (id_arg, m, n, flag), nzmax (nzmax_arg) |
|
1322 { |
|
1323 ir = static_cast<int *> (calloc (nzmax, sizeof (int))); |
|
1324 jc = static_cast<int *> (calloc (nzmax, sizeof (int))); |
|
1325 } |
|
1326 |
|
1327 mxArray_sparse *clone (void) const { return new mxArray_sparse (*this); } |
|
1328 |
|
1329 ~mxArray_sparse (void) |
|
1330 { |
|
1331 mxFree (ir); |
|
1332 mxFree (jc); |
|
1333 } |
|
1334 |
|
1335 octave_value as_octave_value (void) const |
|
1336 { |
|
1337 // FIXME |
|
1338 abort (); |
|
1339 return octave_value (); |
|
1340 } |
|
1341 |
|
1342 int is_sparse (void) const { return 1; } |
|
1343 |
|
1344 int *get_ir (void) const { return ir; } |
|
1345 |
|
1346 int *get_jc (void) const { return jc; } |
|
1347 |
|
1348 int get_nzmax (void) const { return nzmax; } |
|
1349 |
|
1350 void set_ir (int *ir_arg) { ir = ir_arg; } |
|
1351 |
|
1352 void set_jc (int *jc_arg) { jc = jc_arg; } |
|
1353 |
|
1354 void set_nzmax (int nzmax_arg) { nzmax = nzmax_arg; } |
|
1355 |
|
1356 private: |
|
1357 |
|
1358 int nzmax; |
|
1359 |
|
1360 int *ir; |
|
1361 int *jc; |
|
1362 |
|
1363 mxArray_sparse (const mxArray_sparse& val) |
|
1364 : mxArray_number (val), nzmax (val.nzmax), |
|
1365 ir (static_cast<int *> (malloc (nzmax * sizeof (int)))), |
|
1366 jc (static_cast<int *> (malloc (nzmax * sizeof (int)))) |
|
1367 { |
|
1368 for (int i = 0; i < nzmax; i++) |
|
1369 { |
|
1370 ir[i] = val.ir[i]; |
|
1371 jc[i] = val.jc[i]; |
|
1372 } |
|
1373 } |
|
1374 }; |
|
1375 |
|
1376 // Matlab-style struct arrays. |
|
1377 |
|
1378 class mxArray_struct : public mxArray_matlab |
|
1379 { |
|
1380 public: |
|
1381 |
|
1382 mxArray_struct (int ndims_arg, const int *dims_arg, int num_keys_arg, |
|
1383 const char **keys) |
|
1384 : mxArray_matlab (mxSTRUCT_CLASS, ndims_arg, dims_arg), nfields (num_keys_arg), |
|
1385 fields (static_cast<char **> (calloc (nfields, sizeof (char *)))), |
|
1386 data (static_cast<mxArray **> (calloc (nfields * get_number_of_elements (), sizeof (mxArray *)))) |
|
1387 { |
|
1388 init (keys); |
|
1389 } |
|
1390 |
|
1391 mxArray_struct (const dim_vector& dv, int num_keys_arg, const char **keys) |
|
1392 : mxArray_matlab (mxSTRUCT_CLASS, dv), nfields (num_keys_arg), |
|
1393 fields (static_cast<char **> (calloc (nfields, sizeof (char *)))), |
|
1394 data (static_cast<mxArray **> (calloc (nfields * get_number_of_elements (), sizeof (mxArray *)))) |
|
1395 { |
|
1396 init (keys); |
|
1397 } |
|
1398 |
|
1399 mxArray_struct (int m, int n, int num_keys_arg, const char **keys) |
|
1400 : mxArray_matlab (mxSTRUCT_CLASS, m, n), nfields (num_keys_arg), |
|
1401 fields (static_cast<char **> (calloc (nfields, sizeof (char *)))), |
|
1402 data (static_cast<mxArray **> (calloc (nfields * get_number_of_elements (), sizeof (mxArray *)))) |
|
1403 { |
|
1404 init (keys); |
|
1405 } |
|
1406 |
|
1407 void init (const char **keys) |
|
1408 { |
|
1409 for (int i = 0; i < nfields; i++) |
|
1410 fields[i] = strsave (keys[i]); |
|
1411 } |
|
1412 |
|
1413 mxArray_struct *clone (void) const { return new mxArray_struct (*this); } |
|
1414 |
|
1415 ~mxArray_struct (void) |
|
1416 { |
|
1417 for (int i = 0; i < nfields; i++) |
|
1418 mxFree (fields[i]); |
|
1419 |
|
1420 mxFree (fields); |
|
1421 |
|
1422 int ntot = nfields * get_number_of_elements (); |
|
1423 |
|
1424 for (int i = 0; i < ntot; i++) |
|
1425 mxDestroyArray (data[i]); |
|
1426 |
|
1427 mxFree (data); |
|
1428 } |
|
1429 |
|
1430 octave_value as_octave_value (void) const |
|
1431 { |
|
1432 dim_vector dv = dims_to_dim_vector (); |
|
1433 |
|
1434 string_vector keys (fields, nfields); |
|
1435 |
|
1436 Octave_map m; |
|
1437 |
|
1438 int ntot = nfields * get_number_of_elements (); |
|
1439 |
|
1440 for (int i = 0; i < nfields; i++) |
|
1441 { |
|
1442 Cell c (dv); |
|
1443 |
|
1444 octave_value *p = c.fortran_vec (); |
|
1445 |
|
1446 int k = 0; |
|
1447 for (int j = i; j < ntot; j += nfields) |
5902
|
1448 { |
|
1449 mxArray *t = data[j]; |
|
1450 p[k++] = t ? t->as_octave_value () : octave_value (Matrix ()); |
|
1451 } |
5900
|
1452 |
|
1453 m.assign (keys[i], c); |
|
1454 } |
|
1455 |
|
1456 return m; |
|
1457 } |
|
1458 |
|
1459 int add_field (const char *key) |
|
1460 { |
|
1461 int retval = -1; |
|
1462 |
|
1463 if (valid_key (key)) |
|
1464 { |
|
1465 nfields++; |
|
1466 |
|
1467 fields = static_cast<char **> (mxRealloc (fields, nfields * sizeof (char *))); |
|
1468 |
|
1469 if (fields) |
|
1470 { |
|
1471 fields[nfields-1] = strsave (key); |
|
1472 |
|
1473 int nel = get_number_of_elements (); |
|
1474 |
|
1475 int ntot = nfields * nel; |
|
1476 |
|
1477 mxArray **new_data = static_cast<mxArray **> (malloc (ntot * sizeof (mxArray *))); |
|
1478 |
|
1479 if (new_data) |
|
1480 { |
|
1481 int j = 0; |
|
1482 int k = 0; |
|
1483 int n = 0; |
|
1484 |
|
1485 for (int i = 0; i < ntot; i++) |
|
1486 { |
|
1487 if (++n == nfields) |
|
1488 { |
|
1489 new_data[j++] = 0; |
|
1490 n = 0; |
|
1491 } |
|
1492 else |
|
1493 new_data[j++] = data[k++]; |
|
1494 } |
|
1495 |
|
1496 mxFree (data); |
|
1497 |
|
1498 data = new_data; |
|
1499 |
|
1500 retval = nfields - 1; |
|
1501 } |
|
1502 } |
|
1503 } |
|
1504 |
|
1505 return retval; |
|
1506 } |
|
1507 |
|
1508 void remove_field (int key_num) |
|
1509 { |
|
1510 if (key_num >= 0 && key_num < nfields) |
|
1511 { |
|
1512 int nel = get_number_of_elements (); |
|
1513 |
|
1514 int ntot = nfields * nel; |
|
1515 |
|
1516 int new_nfields = nfields - 1; |
|
1517 |
|
1518 char **new_fields = static_cast<char **> (malloc (new_nfields * sizeof (char *))); |
|
1519 |
|
1520 mxArray **new_data = static_cast<mxArray **> (malloc (new_nfields * nel * sizeof (mxArray *))); |
|
1521 |
|
1522 for (int i = 0; i < key_num; i++) |
|
1523 new_fields[i] = fields[i]; |
|
1524 |
|
1525 for (int i = key_num + 1; i < nfields; i++) |
|
1526 new_fields[i-1] = fields[i]; |
|
1527 |
|
1528 if (new_nfields > 0) |
|
1529 { |
|
1530 int j = 0; |
|
1531 int k = 0; |
|
1532 int n = 0; |
|
1533 |
|
1534 for (int i = 0; i < ntot; i++) |
|
1535 { |
|
1536 if (n == key_num) |
|
1537 k++; |
|
1538 else |
|
1539 new_data[j++] = data[k++]; |
|
1540 |
|
1541 if (++n == nfields) |
|
1542 n = 0; |
|
1543 } |
|
1544 } |
|
1545 |
|
1546 nfields = new_nfields; |
|
1547 |
|
1548 mxFree (fields); |
|
1549 mxFree (data); |
|
1550 |
|
1551 fields = new_fields; |
|
1552 data = new_data; |
|
1553 } |
|
1554 } |
|
1555 |
|
1556 mxArray *get_field_by_number (int index, int key_num) const |
|
1557 { |
|
1558 int idx = nfields * index + key_num; |
|
1559 |
|
1560 return data[idx]; |
|
1561 } |
|
1562 |
|
1563 void set_field_by_number (int index, int key_num, mxArray *val) |
|
1564 { |
|
1565 int idx = nfields * index + key_num; |
|
1566 |
|
1567 data[idx] = val; |
|
1568 } |
|
1569 |
|
1570 int get_number_of_fields (void) const { return nfields; } |
|
1571 |
|
1572 const char *get_field_name_by_number (int key_num) const |
|
1573 { |
|
1574 return key_num >= 0 && key_num < nfields ? fields[key_num] : 0; |
|
1575 } |
|
1576 |
|
1577 int get_field_number (const char *key) const |
|
1578 { |
|
1579 int retval = -1; |
|
1580 |
|
1581 for (int i = 0; i < nfields; i++) |
|
1582 { |
|
1583 if (! strcmp (key, fields[i])) |
|
1584 { |
|
1585 retval = i; |
|
1586 break; |
|
1587 } |
|
1588 } |
|
1589 |
|
1590 return retval; |
|
1591 } |
|
1592 |
|
1593 void *get_data (void) const { return data; } |
|
1594 |
|
1595 void set_data (void *data_arg) { data = static_cast<mxArray **> (data_arg); } |
|
1596 |
|
1597 private: |
|
1598 |
|
1599 int nfields; |
|
1600 |
|
1601 char **fields; |
|
1602 |
|
1603 mxArray **data; |
|
1604 |
|
1605 mxArray_struct (const mxArray_struct& val) |
|
1606 : mxArray_matlab (val), nfields (val.nfields), |
|
1607 fields (static_cast<char **> (malloc (nfields * sizeof (char *)))), |
|
1608 data (static_cast<mxArray **> (malloc (nfields * get_number_of_elements () * sizeof (mxArray *)))) |
|
1609 { |
|
1610 for (int i = 0; i < nfields; i++) |
|
1611 fields[i] = strsave (val.fields[i]); |
|
1612 |
|
1613 int nel = get_number_of_elements (); |
|
1614 |
|
1615 for (int i = 0; i < nel * nfields; i++) |
|
1616 data[i] = val.data[i]->clone (); |
|
1617 } |
|
1618 }; |
|
1619 |
|
1620 // Matlab-style cell arrays. |
|
1621 |
|
1622 class mxArray_cell : public mxArray_matlab |
|
1623 { |
|
1624 public: |
|
1625 |
|
1626 mxArray_cell (int ndims_arg, const int *dims_arg) |
|
1627 : mxArray_matlab (mxCELL_CLASS, ndims_arg, dims_arg), |
|
1628 data (static_cast<mxArray **> (calloc (get_number_of_elements (), sizeof (mxArray *)))) { } |
|
1629 |
|
1630 mxArray_cell (const dim_vector& dv) |
|
1631 : mxArray_matlab (mxCELL_CLASS, dv), |
|
1632 data (static_cast<mxArray **> (calloc (get_number_of_elements (), sizeof (mxArray *)))) { } |
|
1633 |
|
1634 mxArray_cell (int m, int n) |
|
1635 : mxArray_matlab (mxCELL_CLASS, m, n), |
|
1636 data (static_cast<mxArray **> (calloc (get_number_of_elements (), sizeof (mxArray *)))) { } |
|
1637 |
|
1638 mxArray_cell *clone (void) const { return new mxArray_cell (*this); } |
|
1639 |
|
1640 ~mxArray_cell (void) |
|
1641 { |
|
1642 int nel = get_number_of_elements (); |
|
1643 |
|
1644 for (int i = 0; i < nel; i++) |
|
1645 mxDestroyArray (data[i]); |
|
1646 |
|
1647 mxFree (data); |
|
1648 } |
|
1649 |
|
1650 octave_value as_octave_value (void) const |
|
1651 { |
|
1652 dim_vector dv = dims_to_dim_vector (); |
|
1653 |
|
1654 Cell c (dv); |
|
1655 |
|
1656 int nel = get_number_of_elements (); |
|
1657 |
|
1658 octave_value *p = c.fortran_vec (); |
|
1659 |
|
1660 for (int i = 0; i < nel; i++) |
5902
|
1661 { |
|
1662 mxArray *t = data[i]; |
|
1663 p[i] = t ? t->as_octave_value () : octave_value (Matrix ()); |
|
1664 } |
5900
|
1665 |
|
1666 return c; |
|
1667 } |
|
1668 |
|
1669 mxArray *get_cell (int idx) const { return data[idx]; } |
|
1670 |
|
1671 void set_cell (int idx, mxArray *val) { data[idx] = val; } |
|
1672 |
|
1673 void *get_data (void) const { return data; } |
|
1674 |
|
1675 void set_data (void *data_arg) { data = static_cast<mxArray **> (data_arg); } |
|
1676 |
|
1677 private: |
|
1678 |
|
1679 mxArray **data; |
|
1680 |
|
1681 mxArray_cell (const mxArray_cell& val) |
|
1682 : mxArray_matlab (val), |
|
1683 data (static_cast<mxArray **> (malloc (get_number_of_elements () * sizeof (mxArray *)))) |
|
1684 { |
|
1685 int nel = get_number_of_elements (); |
|
1686 |
|
1687 for (int i = 0; i < nel; i++) |
|
1688 data[i] = val.data[i]->clone (); |
|
1689 } |
|
1690 }; |
|
1691 |
|
1692 // ------------------------------------------------------------------ |
|
1693 |
|
1694 mxArray::mxArray (const octave_value& ov) |
|
1695 : rep (new mxArray_octave_value (ov)), name (0), persistent (false) { } |
|
1696 |
|
1697 mxArray::mxArray (mxClassID id, int ndims, const int *dims, mxComplexity flag) |
|
1698 : rep (new mxArray_number (id, ndims, dims, flag)), name (0), persistent (false) { } |
|
1699 |
|
1700 mxArray::mxArray (mxClassID id, const dim_vector& dv, mxComplexity flag) |
|
1701 : rep (new mxArray_number (id, dv, flag)), name (0), persistent (false) { } |
|
1702 |
|
1703 mxArray::mxArray (mxClassID id, int m, int n, mxComplexity flag) |
|
1704 : rep (new mxArray_number (id, m, n, flag)), name (0), persistent (false) { } |
|
1705 |
|
1706 mxArray::mxArray (mxClassID id, double val) |
|
1707 : rep (new mxArray_number (id, val)), name (0), persistent (false) { } |
|
1708 |
|
1709 mxArray::mxArray (mxClassID id, mxLogical val) |
|
1710 : rep (new mxArray_number (id, val)), name (0), persistent (false) { } |
|
1711 |
|
1712 mxArray::mxArray (const char *str) |
|
1713 : rep (new mxArray_number (str)), name (0), persistent (false) { } |
|
1714 |
|
1715 mxArray::mxArray (int m, const char **str) |
|
1716 : rep (new mxArray_number (m, str)), name (0), persistent (false) { } |
|
1717 |
|
1718 mxArray::mxArray (mxClassID id, int m, int n, int nzmax, mxComplexity flag) |
|
1719 : rep (new mxArray_sparse (id, m, n, nzmax, flag)), name (0), persistent (false) { } |
|
1720 |
|
1721 mxArray::mxArray (int ndims, const int *dims, int num_keys, const char **keys) |
|
1722 : rep (new mxArray_struct (ndims, dims, num_keys, keys)), name (0), persistent (false) { } |
|
1723 |
|
1724 mxArray::mxArray (const dim_vector& dv, int num_keys, const char **keys) |
|
1725 : rep (new mxArray_struct (dv, num_keys, keys)), name (0), persistent (false) { } |
|
1726 |
|
1727 mxArray::mxArray (int m, int n, int num_keys, const char **keys) |
|
1728 : rep (new mxArray_struct (m, n, num_keys, keys)), name (0), persistent (false) { } |
|
1729 |
|
1730 mxArray::mxArray (int ndims, const int *dims) |
|
1731 : rep (new mxArray_cell (ndims, dims)), name (0), persistent (false) { } |
|
1732 |
|
1733 mxArray::mxArray (const dim_vector& dv) |
|
1734 : rep (new mxArray_cell (dv)), name (0), persistent (false) { } |
|
1735 |
|
1736 mxArray::mxArray (int m, int n) |
|
1737 : rep (new mxArray_cell (m, n)), name (0), persistent (false) { } |
|
1738 |
|
1739 mxArray::~mxArray (void) |
|
1740 { |
|
1741 mxFree (name); |
|
1742 |
|
1743 delete rep; |
|
1744 } |
|
1745 |
|
1746 octave_value |
|
1747 mxArray::as_octave_value (void) const |
|
1748 { |
|
1749 return rep->as_octave_value (); |
|
1750 } |
|
1751 |
|
1752 void |
|
1753 mxArray::set_name (const char *name_arg) |
|
1754 { |
|
1755 mxFree (name); |
|
1756 name = strsave (name_arg); |
|
1757 } |
|
1758 |
|
1759 void |
|
1760 mxArray::maybe_mutate (void) const |
|
1761 { |
|
1762 if (rep->is_octave_value ()) |
|
1763 { |
|
1764 // The mutate function returns a pointer to a complete new |
|
1765 // mxArray object (or 0, if no mutation happened). We just want |
|
1766 // to replace the existing rep with the rep from the new object. |
|
1767 |
|
1768 mxArray *new_val = rep->mutate (); |
|
1769 |
|
1770 if (new_val) |
|
1771 { |
|
1772 delete rep; |
|
1773 rep = new_val->rep; |
|
1774 new_val->rep = 0; |
|
1775 delete new_val; |
|
1776 } |
|
1777 } |
|
1778 } |
|
1779 |
|
1780 // ------------------------------------------------------------------ |
|
1781 |
|
1782 // A clas to manage calls to MEX functions. Mostly deals with memory |
|
1783 // management. |
5864
|
1784 |
|
1785 class mex |
|
1786 { |
|
1787 public: |
|
1788 |
5900
|
1789 mex (void) : memlist (), arraylist (), fname (0) { } |
5864
|
1790 |
|
1791 ~mex (void) |
|
1792 { |
|
1793 if (! memlist.empty ()) |
5900
|
1794 error ("mex: cleanup failed"); |
|
1795 |
|
1796 mxFree (fname); |
5864
|
1797 } |
|
1798 |
5900
|
1799 const char *function_name (void) const |
|
1800 { |
|
1801 if (! fname) |
|
1802 { |
|
1803 octave_function *fcn = octave_call_stack::current (); |
|
1804 |
|
1805 if (fcn) |
|
1806 { |
|
1807 std::string nm = fcn->name (); |
|
1808 fname = strsave (nm.c_str ()); |
|
1809 } |
|
1810 else |
|
1811 fname = strsave ("unknown"); |
|
1812 } |
|
1813 |
|
1814 return fname; |
|
1815 } |
|
1816 |
|
1817 // Free all unmarked pointers obtained from malloc and calloc. |
|
1818 static void cleanup (void *ptr) |
|
1819 { |
|
1820 mex *context = static_cast<mex *> (ptr); |
|
1821 |
|
1822 for (std::set<void *>::iterator p = context->memlist.begin (); |
|
1823 p != context->memlist.end (); p++) |
|
1824 context->free (*p); |
|
1825 |
|
1826 for (std::set<mxArray *>::iterator p = context->arraylist.begin (); |
|
1827 p != context->arraylist.end (); p++) |
|
1828 context->free_value (*p); |
|
1829 } |
5864
|
1830 |
|
1831 // allocate a pointer, and mark it to be freed on exit |
5900
|
1832 void *malloc_unmarked (size_t n) |
|
1833 { |
|
1834 void *ptr = ::malloc (n); |
|
1835 |
|
1836 #ifdef DEBUG |
|
1837 std::cerr << "malloc " << n << " bytes: " << ptr << std::endl; |
|
1838 #endif |
|
1839 |
|
1840 if (! ptr) |
|
1841 { |
|
1842 // FIXME -- could use "octave_new_handler();" instead |
|
1843 |
|
1844 error ("%s: failed to allocate %d bytes of memory", |
|
1845 mexFunctionName (), n); |
|
1846 |
|
1847 abort (); |
|
1848 } |
|
1849 |
|
1850 global_mark (ptr); |
|
1851 |
|
1852 return ptr; |
|
1853 } |
|
1854 |
|
1855 void *malloc (size_t n) |
|
1856 { |
|
1857 void *ptr = malloc_unmarked (n); |
|
1858 |
|
1859 mark (ptr); |
|
1860 |
|
1861 return ptr; |
|
1862 } |
|
1863 |
|
1864 // Allocate a pointer to be freed on exit, and initialize to 0. |
|
1865 void *calloc_unmarked (size_t n, size_t t) |
|
1866 { |
|
1867 void *ptr = malloc_unmarked (n*t); |
|
1868 |
|
1869 memset (ptr, 0, n*t); |
|
1870 |
|
1871 return ptr; |
|
1872 } |
|
1873 |
|
1874 void *calloc (size_t n, size_t t) |
|
1875 { |
|
1876 void *ptr = calloc_unmarked (n, t); |
|
1877 |
|
1878 mark (ptr); |
|
1879 |
|
1880 return ptr; |
|
1881 } |
|
1882 |
|
1883 // Reallocate a pointer obtained from malloc or calloc. We don't |
|
1884 // need an "unmarked" version of this. |
|
1885 void *realloc (void *ptr, size_t n) |
|
1886 { |
|
1887 void *v = ::realloc (ptr, n); |
|
1888 |
|
1889 #ifdef DEBUG |
|
1890 std::cerr << "realloc: " << n << " bytes: " << ptr << std::endl; |
|
1891 #endif |
|
1892 |
|
1893 std::set<void *>::iterator p = memlist.find (ptr); |
|
1894 |
|
1895 if (v && p != memlist.end ()) |
|
1896 { |
|
1897 memlist.erase (p); |
|
1898 memlist.insert (v); |
|
1899 } |
|
1900 |
|
1901 p = global_memlist.find (ptr); |
|
1902 |
|
1903 if (v && p != global_memlist.end ()) |
|
1904 { |
|
1905 global_memlist.erase (p); |
|
1906 global_memlist.insert (v); |
|
1907 } |
|
1908 |
|
1909 return v; |
|
1910 } |
|
1911 |
|
1912 // Free a pointer obtained from malloc or calloc. |
|
1913 void free (void *ptr) |
|
1914 { |
|
1915 if (ptr) |
|
1916 { |
|
1917 unmark (ptr); |
|
1918 |
|
1919 std::set<void *>::iterator p = global_memlist.find (ptr); |
|
1920 |
|
1921 if (p != global_memlist.end ()) |
|
1922 { |
|
1923 global_memlist.erase (p); |
|
1924 |
|
1925 #ifdef DEBUG |
|
1926 std::cerr << "free: " << ptr << std::endl; |
|
1927 #endif |
|
1928 ::free (ptr); |
|
1929 } |
|
1930 else |
|
1931 warning ("mxFree: skipping memory not allocated by mxMalloc, mxCalloc, or mxRealloc"); |
|
1932 } |
|
1933 } |
|
1934 |
|
1935 // Mark a pointer so that it will not be freed on exit. |
|
1936 void persistent (void *ptr) { unmark (ptr); } |
|
1937 |
|
1938 // Make a new array value and initialize from an octave value; it will be |
|
1939 // freed on exit unless marked as persistent. |
|
1940 mxArray *make_value (const octave_value& ov) |
|
1941 { |
|
1942 mxArray *ptr = new mxArray (ov); |
|
1943 arraylist.insert (ptr); |
|
1944 return ptr; |
|
1945 } |
|
1946 |
|
1947 // Free an array and its contents. |
|
1948 void free_value (mxArray *ptr) |
|
1949 { |
|
1950 arraylist.erase (ptr); |
|
1951 delete ptr; |
|
1952 } |
|
1953 |
|
1954 // Mark an array and its contents so it will not be freed on exit. |
|
1955 void persistent (mxArray *ptr) |
|
1956 { |
|
1957 ptr->mark_persistent (); |
|
1958 } |
|
1959 |
|
1960 // 1 if error should be returned to MEX file, 0 if abort. |
5864
|
1961 int trap_feval_error; |
|
1962 |
5900
|
1963 // longjmp return point if mexErrMsgTxt or error. |
5864
|
1964 jmp_buf jump; |
|
1965 |
5900
|
1966 // Trigger a long jump back to the mex calling function. |
5864
|
1967 void abort (void) { longjmp (jump, 1); } |
|
1968 |
|
1969 private: |
|
1970 |
5900
|
1971 // List of memory resources that need to be freed upon exit. |
|
1972 std::set<void *> memlist; |
|
1973 |
|
1974 std::set<mxArray *> arraylist; |
|
1975 |
|
1976 // The name of the currently executing function. |
|
1977 mutable char *fname; |
|
1978 |
|
1979 // Mark a pointer to be freed on exit. |
|
1980 void mark (void *p) |
|
1981 { |
5864
|
1982 #ifdef DEBUG |
5900
|
1983 if (memlist.find (p) != memlist.end ()) |
|
1984 warning ("%s: double registration ignored", mexFunctionName ()); |
5864
|
1985 #endif |
|
1986 |
5900
|
1987 memlist.insert (p); |
|
1988 } |
|
1989 |
|
1990 // Unmark a pointer to be freed on exit, either because it was |
|
1991 // made persistent, or because it was already freed. |
|
1992 void unmark (void *p) |
|
1993 { |
5864
|
1994 #ifdef DEBUG |
5900
|
1995 if (memlist.find (p) != memlist.end ()) |
|
1996 warning ("%s: value not marked", mexFunctionName ()); |
5864
|
1997 #endif |
|
1998 |
5900
|
1999 memlist.erase (p); |
|
2000 } |
|
2001 |
|
2002 // List of memory resources we allocated. |
|
2003 static std::set<void *> global_memlist; |
|
2004 |
|
2005 // Mark a pointer as one we allocated. |
|
2006 void global_mark (void *p) |
|
2007 { |
|
2008 #ifdef DEBUG |
|
2009 if (global_memlist.find (p) != global_memlist.end ()) |
|
2010 warning ("%s: double registration ignored", mexFunctionName ()); |
5864
|
2011 #endif |
5900
|
2012 |
|
2013 global_memlist.insert (p); |
5864
|
2014 } |
|
2015 |
5900
|
2016 // Unmark a pointer as one we allocated. |
|
2017 void global_unmark (void *p) |
5864
|
2018 { |
5900
|
2019 #ifdef DEBUG |
|
2020 if (global_memlist.find (p) != global_memlist.end ()) |
|
2021 warning ("%s: value not marked", mexFunctionName ()); |
|
2022 #endif |
|
2023 |
|
2024 global_memlist.erase (p); |
5864
|
2025 } |
|
2026 }; |
|
2027 |
5900
|
2028 // List of memory resources we allocated. |
|
2029 std::set<void *> mex::global_memlist; |
|
2030 |
|
2031 // Current context. |
|
2032 mex *mex_context = 0; |
|
2033 |
|
2034 void * |
|
2035 mxArray::malloc (size_t n) |
|
2036 { |
|
2037 return mex_context ? mex_context->malloc_unmarked (n) : malloc (n); |
|
2038 } |
|
2039 |
|
2040 void * |
|
2041 mxArray::calloc (size_t n, size_t t) |
|
2042 { |
|
2043 return mex_context ? mex_context->calloc_unmarked (n, t) : calloc (n, t); |
|
2044 } |
|
2045 |
|
2046 // ------------------------------------------------------------------ |
|
2047 |
|
2048 // C interface to mxArray objects: |
|
2049 |
|
2050 // Floating point predicates. |
|
2051 |
|
2052 int |
|
2053 mxIsFinite (const double v) |
|
2054 { |
|
2055 return lo_ieee_finite (v) != 0; |
|
2056 } |
|
2057 |
|
2058 int |
|
2059 mxIsInf (const double v) |
|
2060 { |
|
2061 return lo_ieee_isinf (v) != 0; |
|
2062 } |
|
2063 |
|
2064 int |
|
2065 mxIsNaN (const double v) |
|
2066 { |
|
2067 return lo_ieee_isnan (v) != 0; |
|
2068 } |
|
2069 |
|
2070 double |
|
2071 mxGetEps (void) |
|
2072 { |
|
2073 return DBL_EPSILON; |
|
2074 } |
|
2075 |
|
2076 double |
|
2077 mxGetInf (void) |
|
2078 { |
|
2079 return lo_ieee_inf_value (); |
|
2080 } |
|
2081 |
|
2082 double |
|
2083 mxGetNaN (void) |
|
2084 { |
|
2085 return lo_ieee_nan_value (); |
|
2086 } |
|
2087 |
|
2088 // Memory management. |
|
2089 void * |
|
2090 mxCalloc (size_t n, size_t size) |
|
2091 { |
|
2092 return mex_context ? mex_context->calloc (n, size) : calloc (n, size); |
|
2093 } |
|
2094 |
|
2095 void * |
|
2096 mxMalloc (size_t n) |
|
2097 { |
|
2098 return mex_context ? mex_context->malloc (n) : malloc (n); |
|
2099 } |
|
2100 |
|
2101 void * |
|
2102 mxRealloc (void *ptr, size_t size) |
|
2103 { |
|
2104 return mex_context ? mex_context->realloc (ptr, size) : realloc (ptr, size); |
|
2105 } |
|
2106 |
|
2107 void |
|
2108 mxFree (void *ptr) |
5864
|
2109 { |
5900
|
2110 if (mex_context) |
|
2111 mex_context->free (ptr); |
5864
|
2112 else |
5900
|
2113 free (ptr); |
|
2114 } |
|
2115 |
|
2116 // Constructors. |
|
2117 mxArray * |
|
2118 mxCreateCellArray (int ndims, const int *dims) |
|
2119 { |
|
2120 return new mxArray (ndims, dims); |
|
2121 } |
|
2122 |
|
2123 mxArray * |
|
2124 mxCreateCellMatrix (int m, int n) |
|
2125 { |
|
2126 return new mxArray (m, n); |
|
2127 } |
|
2128 |
|
2129 mxArray * |
|
2130 mxCreateCharArray (int ndims, const int *dims) |
|
2131 { |
|
2132 return new mxArray (mxCHAR_CLASS, ndims, dims); |
5864
|
2133 } |
|
2134 |
5900
|
2135 mxArray * |
|
2136 mxCreateCharMatrixFromStrings (int m, const char **str) |
|
2137 { |
|
2138 return new mxArray (m, str); |
|
2139 } |
|
2140 |
|
2141 mxArray * |
|
2142 mxCreateDoubleMatrix (int m, int n, mxComplexity flag) |
|
2143 { |
|
2144 return new mxArray (mxDOUBLE_CLASS, m, n, flag); |
|
2145 } |
|
2146 |
|
2147 mxArray * |
|
2148 mxCreateDoubleScalar (double val) |
|
2149 { |
|
2150 return new mxArray (mxDOUBLE_CLASS, val); |
|
2151 } |
|
2152 |
|
2153 mxArray * |
|
2154 mxCreateLogicalArray (int ndims, const int *dims) |
5864
|
2155 { |
5900
|
2156 return new mxArray (mxLOGICAL_CLASS, ndims, dims); |
|
2157 } |
|
2158 |
|
2159 mxArray * |
|
2160 mxCreateLogicalMatrix (int m, int n) |
|
2161 { |
|
2162 return new mxArray (mxLOGICAL_CLASS, m, n); |
|
2163 } |
|
2164 |
|
2165 mxArray * |
|
2166 mxCreateLogicalScalar (int val) |
|
2167 { |
|
2168 return new mxArray (mxLOGICAL_CLASS, val); |
|
2169 } |
|
2170 |
|
2171 mxArray * |
|
2172 mxCreateNumericArray (int ndims, const int *dims, mxClassID class_id, |
|
2173 mxComplexity flag) |
|
2174 { |
|
2175 return new mxArray (class_id, ndims, dims, flag); |
5864
|
2176 } |
|
2177 |
5900
|
2178 mxArray * |
|
2179 mxCreateNumericMatrix (int m, int n, mxClassID class_id, mxComplexity flag) |
|
2180 { |
|
2181 return new mxArray (class_id, m, n, flag); |
|
2182 } |
|
2183 |
|
2184 mxArray * |
|
2185 mxCreateSparse (int m, int n, int nzmax, mxComplexity flag) |
|
2186 { |
|
2187 return new mxArray (mxDOUBLE_CLASS, m, n, nzmax, flag); |
|
2188 } |
|
2189 |
|
2190 mxArray * |
|
2191 mxCreateSparseLogicalMatrix (int m, int n, int nzmax) |
|
2192 { |
|
2193 return new mxArray (mxLOGICAL_CLASS, m, n, nzmax); |
|
2194 } |
|
2195 |
|
2196 mxArray * |
|
2197 mxCreateString (const char *str) |
|
2198 { |
|
2199 return new mxArray (str); |
|
2200 } |
|
2201 |
|
2202 mxArray * |
|
2203 mxCreateStructArray (int ndims, int *dims, int num_keys, const char **keys) |
|
2204 { |
|
2205 return new mxArray (ndims, dims, num_keys, keys); |
|
2206 } |
5864
|
2207 |
|
2208 mxArray * |
5900
|
2209 mxCreateStructMatrix (int m, int n, int num_keys, const char **keys) |
|
2210 { |
|
2211 return new mxArray (m, n, num_keys, keys); |
|
2212 } |
|
2213 |
|
2214 // Copy constructor. |
|
2215 mxArray * |
|
2216 mxDuplicateArray (const mxArray *ptr) |
|
2217 { |
|
2218 return ptr->clone (); |
|
2219 } |
|
2220 |
|
2221 // Destructor. |
|
2222 void |
|
2223 mxDestroyArray (mxArray *ptr) |
|
2224 { |
|
2225 if (! ptr->is_persistent ()) |
|
2226 { |
|
2227 if (mex_context) |
|
2228 mex_context->free_value (ptr); |
|
2229 else |
|
2230 delete ptr; |
|
2231 } |
|
2232 } |
|
2233 |
|
2234 // Type Predicates. |
|
2235 int |
|
2236 mxIsCell (const mxArray *ptr) |
|
2237 { |
|
2238 return ptr->is_cell (); |
|
2239 } |
|
2240 |
|
2241 int |
|
2242 mxIsChar (const mxArray *ptr) |
|
2243 { |
|
2244 return ptr->is_char (); |
|
2245 } |
|
2246 |
|
2247 int |
|
2248 mxIsClass (const mxArray *ptr, const char *name) |
|
2249 { |
|
2250 return ptr->is_class (name); |
|
2251 } |
|
2252 |
|
2253 int |
|
2254 mxIsComplex (const mxArray *ptr) |
|
2255 { |
|
2256 return ptr->is_complex (); |
|
2257 } |
|
2258 |
|
2259 int |
|
2260 mxIsDouble (const mxArray *ptr) |
|
2261 { |
|
2262 return ptr->is_double (); |
|
2263 } |
|
2264 |
|
2265 int |
|
2266 mxIsInt16 (const mxArray *ptr) |
|
2267 { |
|
2268 return ptr->is_int16 (); |
|
2269 } |
|
2270 |
|
2271 int |
|
2272 mxIsInt32 (const mxArray *ptr) |
|
2273 { |
|
2274 return ptr->is_int32 (); |
|
2275 } |
|
2276 |
|
2277 int |
|
2278 mxIsInt64 (const mxArray *ptr) |
|
2279 { |
|
2280 return ptr->is_int64 (); |
|
2281 } |
|
2282 |
|
2283 int |
|
2284 mxIsInt8 (const mxArray *ptr) |
|
2285 { |
|
2286 return ptr->is_int8 (); |
|
2287 } |
|
2288 |
|
2289 int |
|
2290 mxIsLogical (const mxArray *ptr) |
|
2291 { |
|
2292 return ptr->is_logical (); |
|
2293 } |
|
2294 |
|
2295 int |
|
2296 mxIsNumeric (const mxArray *ptr) |
|
2297 { |
|
2298 return ptr->is_numeric (); |
|
2299 } |
|
2300 |
|
2301 int |
|
2302 mxIsSingle (const mxArray *ptr) |
|
2303 { |
|
2304 return ptr->is_single (); |
|
2305 } |
|
2306 |
|
2307 int |
|
2308 mxIsSparse (const mxArray *ptr) |
|
2309 { |
|
2310 return ptr->is_sparse (); |
|
2311 } |
|
2312 |
|
2313 int |
|
2314 mxIsStruct (const mxArray *ptr) |
|
2315 { |
|
2316 return ptr->is_struct (); |
|
2317 } |
|
2318 |
|
2319 int |
|
2320 mxIsUint16 (const mxArray *ptr) |
|
2321 { |
|
2322 return ptr->is_uint16 (); |
|
2323 } |
|
2324 |
|
2325 int |
|
2326 mxIsUint32 (const mxArray *ptr) |
|
2327 { |
|
2328 return ptr->is_uint32 (); |
|
2329 } |
|
2330 |
|
2331 int |
|
2332 mxIsUint64 (const mxArray *ptr) |
|
2333 { |
|
2334 return ptr->is_uint64 (); |
|
2335 } |
|
2336 |
|
2337 int |
|
2338 mxIsUint8 (const mxArray *ptr) |
|
2339 { |
|
2340 return ptr->is_uint8 (); |
|
2341 } |
|
2342 |
|
2343 // Odd type+size predicate. |
|
2344 int |
|
2345 mxIsLogicalScalar (const mxArray *ptr) |
|
2346 { |
|
2347 return ptr->is_logical_scalar (); |
|
2348 } |
|
2349 |
|
2350 // Odd type+size+value predicate. |
|
2351 int |
|
2352 mxIsLogicalScalarTrue (const mxArray *ptr) |
|
2353 { |
|
2354 return ptr->is_logical_scalar_true (); |
|
2355 } |
|
2356 |
|
2357 // Size predicate. |
|
2358 int |
|
2359 mxIsEmpty (const mxArray *ptr) |
|
2360 { |
|
2361 return ptr->is_empty (); |
|
2362 } |
|
2363 |
|
2364 // Just plain odd thing to ask of a value. |
|
2365 int |
|
2366 mxIsFromGlobalWS (const mxArray */*ptr*/) |
|
2367 { |
|
2368 // FIXME |
|
2369 abort (); |
|
2370 return 0; |
|
2371 } |
|
2372 |
|
2373 // Dimension extractors. |
|
2374 int |
|
2375 mxGetM (const mxArray *ptr) |
|
2376 { |
|
2377 return ptr->get_m (); |
|
2378 } |
|
2379 |
|
2380 int |
|
2381 mxGetN (const mxArray *ptr) |
|
2382 { |
|
2383 return ptr->get_n (); |
|
2384 } |
|
2385 |
|
2386 int * |
|
2387 mxGetDimensions (const mxArray *ptr) |
5864
|
2388 { |
5900
|
2389 return ptr->get_dimensions (); |
|
2390 } |
|
2391 |
|
2392 int |
|
2393 mxGetNumberOfDimensions (const mxArray *ptr) |
|
2394 { |
|
2395 return ptr->get_number_of_dimensions (); |
|
2396 } |
|
2397 |
|
2398 int |
|
2399 mxGetNumberOfElements (const mxArray *ptr) |
|
2400 { |
|
2401 return ptr->get_number_of_elements (); |
|
2402 } |
|
2403 |
|
2404 // Dimension setters. |
|
2405 void |
|
2406 mxSetM (mxArray *ptr, int m) |
|
2407 { |
|
2408 ptr->set_m (m); |
|
2409 } |
|
2410 |
|
2411 void |
|
2412 mxSetN (mxArray *ptr, int n) |
|
2413 { |
|
2414 ptr->set_n (n); |
|
2415 } |
|
2416 |
|
2417 void |
|
2418 mxSetDimensions (mxArray *ptr, int *dims, int ndims) |
|
2419 { |
|
2420 ptr->set_dimensions (dims, ndims); |
|
2421 } |
|
2422 |
|
2423 // Data extractors. |
|
2424 double * |
|
2425 mxGetPr (const mxArray *ptr) |
|
2426 { |
|
2427 return static_cast<double *> (ptr->get_data ()); |
|
2428 } |
|
2429 |
|
2430 double * |
|
2431 mxGetPi (const mxArray *ptr) |
|
2432 { |
|
2433 return static_cast<double *> (ptr->get_imag_data ()); |
|
2434 } |
|
2435 |
|
2436 double |
|
2437 mxGetScalar (const mxArray *ptr) |
|
2438 { |
|
2439 double *d = mxGetPr (ptr); |
|
2440 return d[0]; |
|
2441 } |
|
2442 |
|
2443 mxChar * |
|
2444 mxGetChars (const mxArray *ptr) |
|
2445 { |
|
2446 return static_cast<mxChar *> (ptr->get_data ()); |
|
2447 } |
|
2448 |
|
2449 mxLogical * |
|
2450 mxGetLogicals (const mxArray *ptr) |
|
2451 { |
|
2452 return static_cast<mxLogical *> (ptr->get_data ()); |
|
2453 } |
|
2454 |
|
2455 void * |
|
2456 mxGetData (const mxArray *ptr) |
|
2457 { |
|
2458 return ptr->get_data (); |
|
2459 } |
|
2460 |
|
2461 void * |
|
2462 mxGetImagData (const mxArray *ptr) |
|
2463 { |
|
2464 return ptr->get_imag_data (); |
|
2465 } |
|
2466 |
|
2467 // Data setters. |
|
2468 void |
|
2469 mxSetPr (mxArray *ptr, double *pr) |
|
2470 { |
|
2471 ptr->set_data (pr); |
|
2472 } |
|
2473 |
|
2474 void |
|
2475 mxSetPi (mxArray *ptr, double *pi) |
|
2476 { |
|
2477 ptr->set_imag_data (pi); |
5864
|
2478 } |
|
2479 |
5900
|
2480 void |
|
2481 mxSetData (mxArray *ptr, void *pr) |
|
2482 { |
|
2483 ptr->set_data (pr); |
|
2484 } |
|
2485 |
|
2486 void |
|
2487 mxSetImagData (mxArray *ptr, void *pi) |
|
2488 { |
|
2489 ptr->set_imag_data (pi); |
|
2490 } |
|
2491 |
|
2492 // Classes. |
|
2493 mxClassID |
|
2494 mxGetClassID (const mxArray *ptr) |
|
2495 { |
|
2496 return ptr->get_class_id (); |
|
2497 } |
|
2498 |
|
2499 const char * |
|
2500 mxGetClassName (const mxArray *ptr) |
|
2501 { |
|
2502 return ptr->get_class_name (); |
|
2503 } |
|
2504 |
|
2505 void |
|
2506 mxSetClassName (mxArray *ptr, const char *name) |
|
2507 { |
|
2508 ptr->set_class_name (name); |
|
2509 } |
|
2510 |
|
2511 // Cell support. |
|
2512 mxArray * |
|
2513 mxGetCell (const mxArray *ptr, int idx) |
|
2514 { |
|
2515 return ptr->get_cell (idx); |
|
2516 } |
|
2517 |
|
2518 void |
|
2519 mxSetCell (mxArray *ptr, int idx, mxArray *val) |
|
2520 { |
|
2521 ptr->set_cell (idx, val); |
|
2522 } |
|
2523 |
|
2524 // Sparse support. |
|
2525 int * |
|
2526 mxGetIr (const mxArray *ptr) |
|
2527 { |
|
2528 return ptr->get_ir (); |
|
2529 } |
|
2530 |
|
2531 int * |
|
2532 mxGetJc (const mxArray *ptr) |
|
2533 { |
|
2534 return ptr->get_jc (); |
|
2535 } |
|
2536 |
|
2537 int |
|
2538 mxGetNzmax (const mxArray *ptr) |
|
2539 { |
|
2540 return ptr->get_nzmax (); |
|
2541 } |
|
2542 |
|
2543 void |
|
2544 mxSetIr (mxArray *ptr, int *ir) |
|
2545 { |
|
2546 ptr->set_ir (ir); |
|
2547 } |
|
2548 |
|
2549 void |
|
2550 mxSetJc (mxArray *ptr, int *jc) |
|
2551 { |
|
2552 ptr->set_jc (jc); |
|
2553 } |
|
2554 |
|
2555 void |
|
2556 mxSetNzmax (mxArray *ptr, int nzmax) |
|
2557 { |
|
2558 ptr->set_nzmax (nzmax); |
|
2559 } |
|
2560 |
|
2561 // Structure support. |
|
2562 int |
|
2563 mxAddField (mxArray *ptr, const char *key) |
|
2564 { |
|
2565 return ptr->add_field (key); |
|
2566 } |
|
2567 |
|
2568 void |
|
2569 mxRemoveField (mxArray *ptr, int key_num) |
|
2570 { |
|
2571 ptr->remove_field (key_num); |
|
2572 } |
5864
|
2573 |
|
2574 mxArray * |
5900
|
2575 mxGetField (const mxArray *ptr, int index, const char *key) |
|
2576 { |
|
2577 int key_num = mxGetFieldNumber (ptr, key); |
|
2578 return mxGetFieldByNumber (ptr, index, key_num); |
|
2579 } |
|
2580 |
|
2581 mxArray * |
|
2582 mxGetFieldByNumber (const mxArray *ptr, int index, int key_num) |
5864
|
2583 { |
5900
|
2584 return ptr->get_field_by_number (index, key_num); |
5864
|
2585 } |
|
2586 |
5900
|
2587 void |
|
2588 mxSetField (mxArray *ptr, int index, const char *key, mxArray *val) |
|
2589 { |
|
2590 int key_num = mxGetFieldNumber (ptr, key); |
|
2591 mxSetFieldByNumber (ptr, index, key_num, val); |
|
2592 } |
5864
|
2593 |
|
2594 void |
5900
|
2595 mxSetFieldByNumber (mxArray *ptr, int index, int key_num, mxArray *val) |
5864
|
2596 { |
5900
|
2597 ptr->set_field_by_number (index, key_num, val); |
|
2598 } |
|
2599 |
|
2600 int |
|
2601 mxGetNumberOfFields (const mxArray *ptr) |
|
2602 { |
|
2603 return ptr->get_number_of_fields (); |
5864
|
2604 } |
|
2605 |
5900
|
2606 const char * |
|
2607 mxGetFieldNameByNumber (const mxArray *ptr, int key_num) |
5864
|
2608 { |
5900
|
2609 return ptr->get_field_name_by_number (key_num); |
|
2610 } |
|
2611 |
|
2612 int |
|
2613 mxGetFieldNumber (const mxArray *ptr, const char *key) |
|
2614 { |
|
2615 return ptr->get_field_number (key); |
5864
|
2616 } |
|
2617 |
5900
|
2618 int |
|
2619 mxGetString (const mxArray *ptr, char *buf, int buflen) |
|
2620 { |
|
2621 return ptr->get_string (buf, buflen); |
|
2622 } |
|
2623 |
|
2624 char * |
|
2625 mxArrayToString (const mxArray *ptr) |
5864
|
2626 { |
5900
|
2627 return ptr->array_to_string (); |
|
2628 } |
|
2629 |
|
2630 int |
|
2631 mxCalcSingleSubscript (const mxArray *ptr, int nsubs, int *subs) |
|
2632 { |
|
2633 return ptr->calc_single_subscript (nsubs, subs); |
5864
|
2634 } |
5900
|
2635 |
|
2636 int |
|
2637 mxGetElementSize (const mxArray *ptr) |
|
2638 { |
|
2639 return ptr->get_element_size (); |
|
2640 } |
|
2641 |
|
2642 // ------------------------------------------------------------------ |
5864
|
2643 |
|
2644 typedef void (*cmex_fptr) (int nlhs, mxArray **plhs, int nrhs, mxArray **prhs); |
|
2645 typedef F77_RET_T (*fmex_fptr) (int& nlhs, mxArray **plhs, int& nrhs, mxArray **prhs); |
|
2646 |
|
2647 enum callstyle { use_fortran, use_C }; |
|
2648 |
|
2649 octave_value_list |
|
2650 call_mex (callstyle cs, void *f, const octave_value_list& args, int nargout) |
|
2651 { |
|
2652 #if 0 |
|
2653 // Don't bother trapping stop/exit |
|
2654 // FIXME -- should really push "mex_exit" onto the octave |
|
2655 // atexit stack before we start and pop it when we are through, but |
|
2656 // the stack handle isn't exported from toplev.cc, so we can't. mex_exit |
|
2657 // would have to be declared as DEFUN(mex_exit,,,"") of course. |
|
2658 static bool unregistered = true; |
5900
|
2659 |
5864
|
2660 if (unregistered) |
|
2661 { |
|
2662 atexit (mex_exit); |
|
2663 unregistered = false; |
|
2664 } |
|
2665 #endif |
|
2666 |
5900
|
2667 // Use at least 1 for nargout since even for zero specified args, |
|
2668 // still want to be able to return an ans. |
5864
|
2669 |
|
2670 int nargin = args.length (); |
5900
|
2671 OCTAVE_LOCAL_BUFFER (mxArray *, argin, nargin); |
5864
|
2672 for (int i = 0; i < nargin; i++) |
|
2673 argin[i] = 0; |
|
2674 |
|
2675 int nout = nargout == 0 ? 1 : nargout; |
5900
|
2676 OCTAVE_LOCAL_BUFFER (mxArray *, argout, nout); |
5864
|
2677 for (int i = 0; i < nout; i++) |
|
2678 argout[i] = 0; |
|
2679 |
|
2680 mex context; |
5900
|
2681 |
|
2682 unwind_protect::add (mex::cleanup, static_cast<void *> (&context)); |
5864
|
2683 |
|
2684 for (int i = 0; i < nargin; i++) |
|
2685 argin[i] = context.make_value (args(i)); |
|
2686 |
|
2687 // Save old mex pointer. |
5900
|
2688 unwind_protect_ptr (mex_context); |
5864
|
2689 |
|
2690 if (setjmp (context.jump) == 0) |
|
2691 { |
5900
|
2692 mex_context = &context; |
5864
|
2693 |
|
2694 if (cs == use_fortran) |
|
2695 { |
|
2696 fmex_fptr fcn = FCN_PTR_CAST (fmex_fptr, f); |
|
2697 |
|
2698 int tmp_nargout = nargout; |
|
2699 int tmp_nargin = nargin; |
|
2700 |
|
2701 fcn (tmp_nargout, argout, tmp_nargin, argin); |
|
2702 } |
|
2703 else |
|
2704 { |
|
2705 cmex_fptr fcn = FCN_PTR_CAST (cmex_fptr, f); |
|
2706 |
|
2707 fcn (nargout, argout, nargin, argin); |
|
2708 } |
|
2709 } |
|
2710 |
|
2711 // Restore old mex pointer. |
|
2712 unwind_protect::run (); |
|
2713 |
|
2714 // Convert returned array entries back into octave values. |
|
2715 |
|
2716 octave_value_list retval; |
|
2717 |
|
2718 if (! error_state) |
|
2719 { |
|
2720 if (nargout == 0 && argout[0]) |
|
2721 { |
5900
|
2722 // We have something for ans. |
|
2723 nargout = 1; |
|
2724 } |
|
2725 |
|
2726 retval.resize (nargout); |
|
2727 |
|
2728 for (int i = 0; i < nargout; i++) |
|
2729 { |
|
2730 if (argout[i]) |
|
2731 { |
5902
|
2732 mxArray *t = argout[i]; |
|
2733 retval(i) = t ? t->as_octave_value () : octave_value (Matrix ()); |
5900
|
2734 } |
5864
|
2735 } |
|
2736 } |
|
2737 |
|
2738 // Clean up mex resources. |
|
2739 unwind_protect::run (); |
|
2740 |
|
2741 return retval; |
|
2742 } |
|
2743 |
|
2744 octave_value_list |
|
2745 Fortran_mex (void *f, const octave_value_list& args, int nargout) |
|
2746 { |
|
2747 return call_mex (use_fortran, f, args, nargout); |
|
2748 } |
|
2749 |
|
2750 octave_value_list |
|
2751 C_mex (void *f, const octave_value_list& args, int nargout) |
|
2752 { |
|
2753 return call_mex (use_C, f, args, nargout); |
|
2754 } |
|
2755 |
|
2756 // C interface to mex functions: |
|
2757 |
|
2758 const char * |
|
2759 mexFunctionName (void) |
|
2760 { |
5900
|
2761 return mex_context ? mex_context->function_name () : "unknown"; |
|
2762 } |
|
2763 |
|
2764 int |
|
2765 mexCallMATLAB (int nargout, mxArray *argout[], int nargin, mxArray *argin[], |
|
2766 const char *fname) |
|
2767 { |
|
2768 octave_value_list args; |
|
2769 |
|
2770 // FIXME -- do we need unwind protect to clean up args? Off hand, I |
|
2771 // would say that this problem is endemic to Octave and we will |
|
2772 // continue to have memory leaks after Ctrl-C until proper exception |
|
2773 // handling is implemented. longjmp() only clears the stack, so any |
|
2774 // class which allocates data on the heap is going to leak. |
|
2775 |
|
2776 args.resize (nargin); |
|
2777 |
|
2778 for (int i = 0; i < nargin; i++) |
5902
|
2779 { |
|
2780 mxArray *t = argin[i]; |
|
2781 args(i) = t ? t->as_octave_value () : octave_value (Matrix ()); |
|
2782 } |
5900
|
2783 |
|
2784 octave_value_list retval = feval (fname, args, nargout); |
|
2785 |
|
2786 if (error_state && mex_context->trap_feval_error == 0) |
5864
|
2787 { |
5900
|
2788 // FIXME -- is this the correct way to clean up? abort() is |
|
2789 // going to trigger a long jump, so the normal class destructors |
|
2790 // will not be called. Hopefully this will reduce things to a |
|
2791 // tiny leak. Maybe create a new octave memory tracer type |
|
2792 // which prints a friendly message every time it is |
|
2793 // created/copied/deleted to check this. |
|
2794 |
|
2795 args.resize (0); |
|
2796 retval.resize (0); |
|
2797 mex_context->abort (); |
|
2798 } |
|
2799 |
|
2800 int num_to_copy = retval.length (); |
|
2801 |
|
2802 if (nargout < retval.length ()) |
|
2803 num_to_copy = nargout; |
|
2804 |
|
2805 for (int i = 0; i < num_to_copy; i++) |
|
2806 { |
|
2807 // FIXME -- it would be nice to avoid copying the value here, |
|
2808 // but there is no way to steal memory from a matrix, never mind |
|
2809 // that matrix memory is allocated by new[] and mxArray memory |
|
2810 // is allocated by malloc(). |
|
2811 argout[i] = mex_context->make_value (retval (i)); |
|
2812 } |
|
2813 |
|
2814 while (num_to_copy < nargout) |
|
2815 argout[num_to_copy++] = 0; |
|
2816 |
|
2817 if (error_state) |
|
2818 { |
|
2819 error_state = 0; |
|
2820 return 1; |
5864
|
2821 } |
|
2822 else |
5900
|
2823 return 0; |
|
2824 } |
|
2825 |
|
2826 void |
|
2827 mexSetTrapFlag (int flag) |
|
2828 { |
|
2829 if (mex_context) |
|
2830 mex_context->trap_feval_error = flag; |
|
2831 } |
|
2832 |
|
2833 int |
|
2834 mexEvalString (const char *s) |
|
2835 { |
|
2836 int retval = 0; |
|
2837 |
|
2838 int parse_status; |
|
2839 |
|
2840 octave_value_list ret; |
|
2841 |
|
2842 ret = eval_string (s, false, parse_status, 0); |
|
2843 |
|
2844 if (parse_status || error_state) |
|
2845 { |
|
2846 error_state = 0; |
|
2847 |
|
2848 retval = 1; |
|
2849 } |
5864
|
2850 |
|
2851 return retval; |
|
2852 } |
|
2853 |
|
2854 void |
|
2855 mexErrMsgTxt (const char *s) |
|
2856 { |
|
2857 if (s && strlen (s) > 0) |
5879
|
2858 error ("%s: %s", mexFunctionName (), s); |
5864
|
2859 else |
|
2860 // Just set the error state; don't print msg. |
|
2861 error (""); |
|
2862 |
5900
|
2863 mex_context->abort (); |
5864
|
2864 } |
|
2865 |
5879
|
2866 void |
|
2867 mexErrMsgIdAndTxt (const char *id, const char *s) |
|
2868 { |
|
2869 if (s && strlen (s) > 0) |
|
2870 error_with_id (id, "%s: %s", mexFunctionName (), s); |
|
2871 else |
|
2872 // Just set the error state; don't print msg. |
|
2873 error (""); |
|
2874 |
5900
|
2875 mex_context->abort (); |
5879
|
2876 } |
|
2877 |
|
2878 void |
|
2879 mexWarnMsgTxt (const char *s) |
|
2880 { |
|
2881 warning ("%s", s); |
|
2882 } |
|
2883 |
|
2884 void |
|
2885 mexWarnMsgIdAndTxt (const char *id, const char *s) |
|
2886 { |
|
2887 warning_with_id (id, "%s", s); |
|
2888 } |
5864
|
2889 |
|
2890 void |
|
2891 mexPrintf (const char *fmt, ...) |
|
2892 { |
|
2893 va_list args; |
|
2894 va_start (args, fmt); |
|
2895 octave_vformat (octave_stdout, fmt, args); |
|
2896 va_end (args); |
|
2897 } |
|
2898 |
|
2899 mxArray * |
5879
|
2900 mexGetVariable (const char *space, const char *name) |
5864
|
2901 { |
|
2902 mxArray *retval = 0; |
|
2903 |
|
2904 // FIXME -- this should be in variable.cc, but the correct |
|
2905 // functionality is not exported. Particularly, get_global_value() |
|
2906 // generates an error if the symbol is undefined. |
|
2907 |
|
2908 symbol_record *sr = 0; |
|
2909 |
|
2910 if (! strcmp (space, "global")) |
|
2911 sr = global_sym_tab->lookup (name); |
|
2912 else if (! strcmp (space, "caller")) |
|
2913 sr = curr_sym_tab->lookup (name); |
|
2914 else if (! strcmp (space, "base")) |
5900
|
2915 sr = top_level_sym_tab->lookup (name); |
5864
|
2916 else |
5879
|
2917 mexErrMsgTxt ("mexGetVariable: symbol table does not exist"); |
5864
|
2918 |
|
2919 if (sr) |
|
2920 { |
|
2921 octave_value sr_def = sr->def (); |
|
2922 |
|
2923 if (sr_def.is_defined ()) |
|
2924 { |
5900
|
2925 retval = mex_context->make_value (sr_def); |
|
2926 |
|
2927 retval->set_name (name); |
5864
|
2928 } |
|
2929 } |
|
2930 |
|
2931 return retval; |
|
2932 } |
|
2933 |
5879
|
2934 const mxArray * |
|
2935 mexGetVariablePtr (const char *space, const char *name) |
5864
|
2936 { |
5879
|
2937 return mexGetVariable (space, name); |
5864
|
2938 } |
|
2939 |
5900
|
2940 int |
|
2941 mexPutVariable (const char *space, const char *name, mxArray *ptr) |
5864
|
2942 { |
5900
|
2943 if (! ptr) |
|
2944 return 1; |
|
2945 |
|
2946 if (! name) |
|
2947 return 1; |
|
2948 |
|
2949 if (name[0] == '\0') |
|
2950 name = ptr->get_name (); |
|
2951 |
|
2952 if (! name || name[0] == '\0') |
|
2953 return 1; |
|
2954 |
|
2955 if (! strcmp (space, "global")) |
|
2956 set_global_value (name, ptr->as_octave_value ()); |
|
2957 else |
|
2958 { |
|
2959 // FIXME -- this belongs in variables.cc. |
|
2960 |
|
2961 symbol_record *sr = 0; |
|
2962 |
|
2963 if (! strcmp (space, "caller")) |
|
2964 sr = curr_sym_tab->lookup (name, true); |
|
2965 else if (! strcmp (space, "base")) |
|
2966 sr = top_level_sym_tab->lookup (name, true); |
|
2967 else |
|
2968 mexErrMsgTxt ("mexPutVariable: symbol table does not exist"); |
|
2969 |
|
2970 if (sr) |
|
2971 sr->define (ptr->as_octave_value ()); |
|
2972 else |
|
2973 panic_impossible (); |
|
2974 } |
|
2975 |
|
2976 return 0; |
5864
|
2977 } |
|
2978 |
|
2979 void |
5900
|
2980 mexMakeArrayPersistent (mxArray *ptr) |
5864
|
2981 { |
5900
|
2982 if (mex_context) |
|
2983 mex_context->persistent (ptr); |
5864
|
2984 } |
5879
|
2985 |
5864
|
2986 void |
5900
|
2987 mexMakeMemoryPersistent (void *ptr) |
5864
|
2988 { |
5900
|
2989 if (mex_context) |
|
2990 mex_context->persistent (ptr); |
5864
|
2991 } |
|
2992 |
5900
|
2993 int |
|
2994 mexAtExit (void (*/*f*/) (void)) |
5864
|
2995 { |
5900
|
2996 // FIXME |
|
2997 error ("mexAtExit: not implemented"); |
|
2998 return 0; |
5864
|
2999 } |
|
3000 |
5900
|
3001 const mxArray * |
|
3002 mexGet (double /*handle*/, const char */*property*/) |
5864
|
3003 { |
5900
|
3004 // FIXME |
|
3005 error ("mexGet: not implemented"); |
|
3006 return 0; |
5864
|
3007 } |
|
3008 |
5900
|
3009 int |
|
3010 mexIsGlobal (const mxArray *ptr) |
5864
|
3011 { |
5900
|
3012 return mxIsFromGlobalWS (ptr); |
5864
|
3013 } |
|
3014 |
5900
|
3015 int |
|
3016 mexIsLocked (void) |
5864
|
3017 { |
5900
|
3018 int retval = 0; |
|
3019 |
|
3020 if (mex_context) |
|
3021 { |
|
3022 const char *fname = mexFunctionName (); |
|
3023 |
|
3024 retval = mislocked (fname); |
|
3025 } |
|
3026 |
|
3027 return retval; |
5864
|
3028 } |
|
3029 |
5900
|
3030 std::map<std::string,int> mex_lock_count; |
|
3031 |
|
3032 void |
|
3033 mexLock (void) |
5864
|
3034 { |
5900
|
3035 if (mex_context) |
5864
|
3036 { |
5900
|
3037 const char *fname = mexFunctionName (); |
|
3038 |
|
3039 if (mex_lock_count.find (fname) == mex_lock_count.end ()) |
|
3040 mex_lock_count[fname] = 1; |
|
3041 else |
|
3042 mex_lock_count[fname]++; |
|
3043 |
|
3044 mlock (fname); |
5864
|
3045 } |
|
3046 } |
|
3047 |
5900
|
3048 int |
|
3049 mexSet (double /*handle*/, const char */*property*/, mxArray */*val*/) |
|
3050 { |
|
3051 // FIXME |
|
3052 error ("mexSet: not implemented"); |
|
3053 return 0; |
|
3054 } |
|
3055 |
|
3056 void |
|
3057 mexUnlock (void) |
5864
|
3058 { |
5900
|
3059 if (mex_context) |
5864
|
3060 { |
5900
|
3061 const char *fname = mexFunctionName (); |
|
3062 |
|
3063 if (mex_lock_count.find (fname) == mex_lock_count.end ()) |
|
3064 warning ("mexUnlock: funtion `%s' is not locked", fname); |
|
3065 else |
|
3066 { |
|
3067 int count = --mex_lock_count[fname]; |
|
3068 |
|
3069 if (count == 0) |
|
3070 { |
|
3071 munlock (fname); |
|
3072 |
|
3073 mex_lock_count.erase (fname); |
|
3074 } |
|
3075 } |
5864
|
3076 } |
|
3077 } |