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