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