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" |
5900
|
30 |
|
31 // #define DEBUG 1 |
|
32 |
5905
|
33 static void |
|
34 xfree (void *ptr) |
|
35 { |
|
36 ::free (ptr); |
|
37 } |
|
38 |
5900
|
39 static int |
|
40 max_str_len (int m, const char **str) |
|
41 { |
|
42 int max_len = 0; |
|
43 |
|
44 for (int i = 0; i < m; i++) |
|
45 { |
|
46 int tmp = strlen (str[i]); |
|
47 |
|
48 if (tmp > max_len) |
|
49 max_len = tmp; |
|
50 } |
|
51 |
|
52 return max_len; |
|
53 } |
|
54 |
|
55 static int |
|
56 valid_key (const char *key) |
|
57 { |
|
58 int retval = 0; |
|
59 |
|
60 int nel = strlen (key); |
|
61 |
|
62 if (nel > 0) |
|
63 { |
|
64 if (isalpha (key[0])) |
|
65 { |
|
66 for (int i = 1; i < nel; i++) |
|
67 { |
|
68 if (! (isalnum (key[i]) || key[i] == '_')) |
|
69 goto done; |
|
70 } |
|
71 |
|
72 retval = 1; |
|
73 } |
|
74 } |
|
75 |
|
76 done: |
|
77 |
|
78 return retval; |
|
79 } |
|
80 |
|
81 // ------------------------------------------------------------------ |
|
82 |
|
83 // A class to provide the default implemenation of some of the virtual |
|
84 // functions declared in the mxArray class. |
|
85 |
|
86 class mxArray_base : public mxArray |
|
87 { |
|
88 protected: |
|
89 |
|
90 mxArray_base (void) : mxArray (xmxArray ()) { } |
|
91 |
|
92 public: |
|
93 |
|
94 mxArray *clone (void) const = 0; |
|
95 |
|
96 ~mxArray_base (void) { } |
|
97 |
|
98 bool is_octave_value (void) const { return false; } |
|
99 |
|
100 int is_cell (void) const = 0; |
|
101 |
|
102 int is_char (void) const = 0; |
|
103 |
|
104 int is_class (const char *name_arg) const |
|
105 { |
|
106 int retval = 0; |
|
107 |
|
108 const char *cname = get_class_name (); |
|
109 |
|
110 if (cname && name_arg) |
|
111 retval = ! strcmp (cname, name_arg); |
|
112 |
|
113 return retval; |
|
114 } |
|
115 |
|
116 int is_complex (void) const = 0; |
|
117 |
|
118 int is_double (void) const = 0; |
|
119 |
|
120 int is_int16 (void) const = 0; |
|
121 |
|
122 int is_int32 (void) const = 0; |
|
123 |
|
124 int is_int64 (void) const = 0; |
|
125 |
|
126 int is_int8 (void) const = 0; |
|
127 |
|
128 int is_logical (void) const = 0; |
|
129 |
|
130 int is_numeric (void) const = 0; |
|
131 |
|
132 int is_single (void) const = 0; |
|
133 |
|
134 int is_sparse (void) const = 0; |
|
135 |
|
136 int is_struct (void) const = 0; |
|
137 |
|
138 int is_uint16 (void) const = 0; |
|
139 |
|
140 int is_uint32 (void) const = 0; |
|
141 |
|
142 int is_uint64 (void) const = 0; |
|
143 |
|
144 int is_uint8 (void) const = 0; |
|
145 |
|
146 int is_logical_scalar (void) const |
|
147 { |
|
148 return is_logical () && get_number_of_elements () == 1; |
|
149 } |
|
150 |
|
151 int is_logical_scalar_true (void) const = 0; |
|
152 |
|
153 int get_m (void) const = 0; |
|
154 |
|
155 int get_n (void) const = 0; |
|
156 |
|
157 int *get_dimensions (void) const = 0; |
|
158 |
|
159 int get_number_of_dimensions (void) const = 0; |
|
160 |
|
161 void set_m (int m) = 0; |
|
162 |
|
163 void set_n (int n) = 0; |
|
164 |
|
165 void set_dimensions (int *dims_arg, int ndims_arg) = 0; |
|
166 |
|
167 int get_number_of_elements (void) const = 0; |
|
168 |
|
169 int is_empty (void) const = 0; |
|
170 |
|
171 mxClassID get_class_id (void) const = 0; |
|
172 |
|
173 const char *get_class_name (void) const = 0; |
|
174 |
|
175 void set_class_name (const char *name_arg) = 0; |
|
176 |
|
177 mxArray *get_cell (int /*idx*/) const |
|
178 { |
|
179 invalid_type_error (); |
|
180 return 0; |
|
181 } |
|
182 |
|
183 void set_cell (int idx, mxArray *val) = 0; |
|
184 |
6332
|
185 double get_scalar (void) const = 0; |
|
186 |
5900
|
187 void *get_data (void) const = 0; |
|
188 |
|
189 void *get_imag_data (void) const = 0; |
|
190 |
|
191 void set_data (void *pr) = 0; |
|
192 |
|
193 void set_imag_data (void *pi) = 0; |
|
194 |
|
195 int *get_ir (void) const = 0; |
|
196 |
|
197 int *get_jc (void) const = 0; |
|
198 |
|
199 int get_nzmax (void) const = 0; |
|
200 |
|
201 void set_ir (int *ir) = 0; |
|
202 |
|
203 void set_jc (int *jc) = 0; |
|
204 |
|
205 void set_nzmax (int nzmax) = 0; |
|
206 |
|
207 int add_field (const char *key) = 0; |
|
208 |
|
209 void remove_field (int key_num) = 0; |
|
210 |
|
211 mxArray *get_field_by_number (int index, int key_num) const = 0; |
|
212 |
|
213 void set_field_by_number (int index, int key_num, mxArray *val) = 0; |
|
214 |
|
215 int get_number_of_fields (void) const = 0; |
|
216 |
|
217 const char *get_field_name_by_number (int key_num) const = 0; |
|
218 |
|
219 int get_field_number (const char *key) const = 0; |
|
220 |
|
221 int get_string (char *buf, int buflen) const = 0; |
|
222 |
|
223 char *array_to_string (void) const = 0; |
|
224 |
|
225 int calc_single_subscript (int nsubs, int *subs) const = 0; |
|
226 |
|
227 int get_element_size (void) const = 0; |
|
228 |
|
229 bool mutation_needed (void) const { return false; } |
|
230 |
|
231 mxArray *mutate (void) const { return 0; } |
|
232 |
|
233 protected: |
|
234 |
5907
|
235 octave_value as_octave_value (void) const = 0; |
|
236 |
5900
|
237 mxArray_base (const mxArray_base&) : mxArray (xmxArray ()) { } |
|
238 |
|
239 void invalid_type_error (void) const |
|
240 { |
|
241 error ("invalid type for operation"); |
|
242 } |
|
243 |
|
244 void error (const char *msg) const |
|
245 { |
|
246 // FIXME |
|
247 ::error ("%s", msg); |
|
248 } |
|
249 }; |
|
250 |
|
251 // The object that handles values pass to MEX files from Octave. Some |
|
252 // methods in this class may set mutate_flag to TRUE to tell the |
|
253 // mxArray class to convert to the Matlab-style representation and |
|
254 // then invoke the method on that object instead (for example, getting |
|
255 // a pointer to real or imaginary data from a complex object requires |
|
256 // a mutation but getting a pointer to real data from a real object |
|
257 // does not). Changing the representation causes a copy so we try to |
|
258 // avoid it unless it is really necessary. Once the conversion |
|
259 // happens, we delete this representation, so the conversion can only |
|
260 // happen once per call to a MEX file. |
|
261 |
|
262 class mxArray_octave_value : public mxArray_base |
|
263 { |
|
264 public: |
|
265 |
|
266 mxArray_octave_value (const octave_value& ov) |
|
267 : mxArray_base (), val (ov), mutate_flag (false), |
|
268 id (mxUNKNOWN_CLASS), class_name (0), ndims (-1), dims (0) { } |
|
269 |
|
270 mxArray *clone (void) const { return new mxArray_octave_value (*this); } |
|
271 |
|
272 ~mxArray_octave_value (void) |
|
273 { |
|
274 mxFree (class_name); |
|
275 mxFree (dims); |
|
276 } |
|
277 |
|
278 bool is_octave_value (void) const { return true; } |
|
279 |
|
280 int is_cell (void) const { return val.is_cell (); } |
|
281 |
|
282 int is_char (void) const { return val.is_string (); } |
|
283 |
|
284 int is_complex (void) const { return val.is_complex_type (); } |
|
285 |
|
286 int is_double (void) const { return val.is_double_type (); } |
|
287 |
|
288 int is_int16 (void) const { return val.is_int16_type (); } |
|
289 |
|
290 int is_int32 (void) const { return val.is_int32_type (); } |
|
291 |
|
292 int is_int64 (void) const { return val.is_int64_type (); } |
|
293 |
|
294 int is_int8 (void) const { return val.is_int8_type (); } |
|
295 |
|
296 int is_logical (void) const { return val.is_bool_type (); } |
|
297 |
|
298 int is_numeric (void) const { return val.is_numeric_type (); } |
|
299 |
|
300 int is_single (void) const { return val.is_single_type (); } |
|
301 |
|
302 int is_sparse (void) const { return val.is_sparse_type (); } |
|
303 |
|
304 int is_struct (void) const { return val.is_map (); } |
|
305 |
|
306 int is_uint16 (void) const { return val.is_uint16_type (); } |
|
307 |
6069
|
308 int is_uint32 (void) const { return val.is_uint32_type (); } |
|
309 |
|
310 int is_uint64 (void) const { return val.is_uint64_type (); } |
|
311 |
|
312 int is_uint8 (void) const { return val.is_uint8_type (); } |
5900
|
313 |
|
314 int is_range (void) const { return val.is_range (); } |
|
315 |
|
316 int is_real_type (void) const { return val.is_real_type (); } |
|
317 |
|
318 int is_logical_scalar_true (void) const |
|
319 { |
|
320 return (is_logical_scalar () && val.is_true ()); |
|
321 } |
|
322 |
|
323 int get_m (void) const { return val.rows (); } |
|
324 |
6187
|
325 int get_n (void) const |
|
326 { |
|
327 int n = 1; |
|
328 |
|
329 // Force dims and ndims to be cached. |
|
330 get_dimensions(); |
|
331 |
|
332 for (int i = ndims - 1; i > 0; i--) |
|
333 n *= dims[i]; |
|
334 |
|
335 return n; |
|
336 } |
5900
|
337 |
|
338 int *get_dimensions (void) const |
|
339 { |
|
340 if (! dims) |
|
341 { |
6332
|
342 ndims = val.ndims (); |
5900
|
343 |
|
344 dims = static_cast<int *> (malloc (ndims * sizeof (int))); |
|
345 |
|
346 dim_vector dv = val.dims (); |
|
347 |
|
348 for (int i = 0; i < ndims; i++) |
|
349 dims[i] = dv(i); |
|
350 } |
|
351 |
|
352 return dims; |
|
353 } |
|
354 |
|
355 int get_number_of_dimensions (void) const |
|
356 { |
6332
|
357 // Force dims and ndims to be cached. |
|
358 get_dimensions (); |
5900
|
359 |
|
360 return ndims; |
|
361 } |
|
362 |
|
363 void set_m (int /*m*/) { panic_impossible (); } |
|
364 |
|
365 void set_n (int /*n*/) { panic_impossible (); } |
|
366 |
|
367 void set_dimensions (int */*dims_arg*/, int /*ndims_arg*/) |
|
368 { |
|
369 panic_impossible (); |
|
370 } |
|
371 |
|
372 int get_number_of_elements (void) const { return val.numel (); } |
|
373 |
|
374 int is_empty (void) const { return val.is_empty (); } |
|
375 |
|
376 mxClassID get_class_id (void) const |
|
377 { |
|
378 id = mxUNKNOWN_CLASS; |
|
379 |
|
380 std::string cn = val.class_name (); |
|
381 |
|
382 if (cn == "cell") |
|
383 id = mxCELL_CLASS; |
|
384 else if (cn == "struct") |
|
385 id = mxSTRUCT_CLASS; |
|
386 else if (cn == "logical") |
|
387 id = mxLOGICAL_CLASS; |
|
388 else if (cn == "char") |
|
389 id = mxCHAR_CLASS; |
|
390 else if (cn == "double") |
|
391 id = mxDOUBLE_CLASS; |
5903
|
392 else if (cn == "sparse") |
|
393 { |
|
394 if (val.is_bool_type ()) |
|
395 id = mxLOGICAL_CLASS; |
|
396 else |
|
397 id = mxDOUBLE_CLASS; |
|
398 } |
5900
|
399 else if (cn == "single") |
|
400 id = mxSINGLE_CLASS; |
|
401 else if (cn == "int8") |
|
402 id = mxINT8_CLASS; |
|
403 else if (cn == "uint8") |
|
404 id = mxUINT8_CLASS; |
|
405 else if (cn == "int16") |
|
406 id = mxINT16_CLASS; |
|
407 else if (cn == "uint16") |
|
408 id = mxUINT16_CLASS; |
|
409 else if (cn == "int32") |
|
410 id = mxINT32_CLASS; |
|
411 else if (cn == "uint32") |
|
412 id = mxUINT32_CLASS; |
|
413 else if (cn == "int64") |
|
414 id = mxINT64_CLASS; |
|
415 else if (cn == "uint64") |
|
416 id = mxUINT64_CLASS; |
6218
|
417 else if (cn == "function_handle") |
5900
|
418 id = mxFUNCTION_CLASS; |
|
419 |
|
420 return id; |
|
421 } |
|
422 |
|
423 const char *get_class_name (void) const |
|
424 { |
|
425 if (! class_name) |
|
426 { |
|
427 std::string s = val.class_name (); |
|
428 class_name = strsave (s.c_str ()); |
|
429 } |
|
430 |
|
431 return class_name; |
|
432 } |
|
433 |
|
434 // Not allowed. |
|
435 void set_class_name (const char */*name_arg*/) { panic_impossible (); } |
|
436 |
|
437 mxArray *get_cell (int /*idx*/) const |
|
438 { |
|
439 request_mutation (); |
|
440 return 0; |
|
441 } |
|
442 |
|
443 // Not allowed. |
|
444 void set_cell (int /*idx*/, mxArray */*val*/) { panic_impossible (); } |
|
445 |
6332
|
446 double get_scalar (void) const { return val.scalar_value (true); } |
|
447 |
5900
|
448 void *get_data (void) const |
|
449 { |
|
450 void *retval = 0; |
|
451 |
|
452 if (is_char () |
|
453 || (is_numeric () && is_real_type () && ! is_range ())) |
|
454 retval = val.mex_get_data (); |
|
455 else |
|
456 request_mutation (); |
|
457 |
|
458 return retval; |
|
459 } |
|
460 |
|
461 void *get_imag_data (void) const |
|
462 { |
|
463 void *retval = 0; |
|
464 |
|
465 if (is_numeric () && is_real_type ()) |
|
466 retval = 0; |
|
467 else |
|
468 request_mutation (); |
|
469 |
|
470 return retval; |
|
471 } |
|
472 |
|
473 // Not allowed. |
|
474 void set_data (void */*pr*/) { panic_impossible (); } |
|
475 |
|
476 // Not allowed. |
|
477 void set_imag_data (void */*pi*/) { panic_impossible (); } |
|
478 |
|
479 int *get_ir (void) const |
|
480 { |
|
481 #if SIZEOF_OCTAVE_IDX_TYPE == SIZEOF_INT |
5902
|
482 return val.mex_get_ir (); |
5900
|
483 #else |
|
484 request_mutation (); |
|
485 return 0; |
|
486 #endif |
|
487 } |
|
488 |
|
489 int *get_jc (void) const |
|
490 { |
|
491 #if SIZEOF_OCTAVE_IDX_TYPE == SIZEOF_INT |
|
492 return val.mex_get_jc (); |
|
493 #else |
|
494 request_mutation (); |
|
495 return 0; |
|
496 #endif |
|
497 } |
|
498 |
|
499 int get_nzmax (void) const { return val.nzmax (); } |
|
500 |
|
501 // Not allowed. |
|
502 void set_ir (int */*ir*/) { panic_impossible (); } |
|
503 |
|
504 // Not allowed. |
|
505 void set_jc (int */*jc*/) { panic_impossible (); } |
|
506 |
|
507 // Not allowed. |
|
508 void set_nzmax (int /*nzmax*/) { panic_impossible (); } |
|
509 |
|
510 // Not allowed. |
|
511 int add_field (const char */*key*/) |
|
512 { |
|
513 panic_impossible (); |
|
514 return -1; |
|
515 } |
|
516 |
|
517 // Not allowed. |
|
518 void remove_field (int /*key_num*/) { panic_impossible (); } |
|
519 |
|
520 mxArray *get_field_by_number (int /*index*/, int /*key_num*/) const |
|
521 { |
|
522 request_mutation (); |
|
523 return 0; |
|
524 } |
|
525 |
|
526 // Not allowed. |
|
527 void set_field_by_number (int /*index*/, int /*key_num*/, mxArray */*val*/) |
|
528 { |
|
529 panic_impossible (); |
|
530 } |
|
531 |
|
532 int get_number_of_fields (void) const { return val.nfields (); } |
|
533 |
|
534 const char *get_field_name_by_number (int /*key_num*/) const |
|
535 { |
|
536 request_mutation (); |
|
537 return 0; |
|
538 } |
|
539 |
|
540 int get_field_number (const char */*key*/) const |
|
541 { |
|
542 request_mutation (); |
|
543 return 0; |
|
544 } |
|
545 |
|
546 int get_string (char *buf, int buflen) const |
|
547 { |
|
548 int retval = 1; |
|
549 |
|
550 int nel = get_number_of_elements (); |
|
551 |
|
552 if (val.is_string () && nel < buflen) |
|
553 { |
|
554 charNDArray tmp = val.char_array_value (); |
|
555 |
|
556 const char *p = tmp.data (); |
|
557 |
|
558 for (int i = 0; i < buflen; i++) |
|
559 buf[i] = p[i]; |
|
560 |
|
561 buf[nel] = 0; |
|
562 |
|
563 retval = 0; |
|
564 } |
|
565 |
|
566 return retval; |
|
567 } |
|
568 |
|
569 char *array_to_string (void) const |
|
570 { |
|
571 // FIXME -- this is suposed to handle multi-byte character |
|
572 // strings. |
|
573 |
|
574 char *buf = 0; |
|
575 |
|
576 if (val.is_string ()) |
|
577 { |
|
578 int nel = get_number_of_elements (); |
|
579 |
|
580 buf = static_cast<char *> (malloc (nel + 1)); |
|
581 |
|
582 if (buf) |
|
583 { |
|
584 charNDArray tmp = val.char_array_value (); |
|
585 |
|
586 const char *p = tmp.data (); |
|
587 |
|
588 for (int i = 0; i < nel; i++) |
|
589 buf[i] = p[i]; |
|
590 |
|
591 buf[nel] = '\0'; |
|
592 } |
|
593 } |
|
594 |
|
595 return buf; |
|
596 } |
|
597 |
|
598 int calc_single_subscript (int nsubs, int *subs) const |
|
599 { |
|
600 int retval = 0; |
|
601 |
|
602 // Force ndims, dims to be cached. |
|
603 get_dimensions (); |
|
604 |
|
605 int n = nsubs <= ndims ? nsubs : ndims; |
|
606 |
|
607 while (--n > 0) |
|
608 retval = retval * dims[n] + subs[n]; |
|
609 |
|
610 return retval; |
|
611 } |
|
612 |
|
613 int get_element_size (void) const |
|
614 { |
|
615 // Force id to be cached. |
|
616 get_class_id (); |
|
617 |
|
618 switch (id) |
|
619 { |
|
620 case mxCELL_CLASS: return sizeof (mxArray *); |
|
621 case mxSTRUCT_CLASS: return sizeof (mxArray *); |
|
622 case mxLOGICAL_CLASS: return sizeof (mxLogical); |
|
623 case mxCHAR_CLASS: return sizeof (mxChar); |
|
624 case mxDOUBLE_CLASS: return sizeof (double); |
|
625 case mxSINGLE_CLASS: return sizeof (float); |
|
626 case mxINT8_CLASS: return 1; |
|
627 case mxUINT8_CLASS: return 1; |
|
628 case mxINT16_CLASS: return 2; |
|
629 case mxUINT16_CLASS: return 2; |
|
630 case mxINT32_CLASS: return 4; |
|
631 case mxUINT32_CLASS: return 4; |
|
632 case mxINT64_CLASS: return 8; |
|
633 case mxUINT64_CLASS: return 8; |
|
634 case mxFUNCTION_CLASS: return 0; |
|
635 default: return 0; |
|
636 } |
|
637 } |
|
638 |
|
639 bool mutation_needed (void) const { return mutate_flag; } |
|
640 |
|
641 void request_mutation (void) const |
|
642 { |
|
643 if (mutate_flag) |
|
644 panic_impossible (); |
|
645 |
|
646 mutate_flag = true; |
|
647 } |
|
648 |
|
649 mxArray *mutate (void) const { return val.as_mxArray (); } |
|
650 |
|
651 protected: |
|
652 |
5907
|
653 octave_value as_octave_value (void) const { return val; } |
|
654 |
5900
|
655 mxArray_octave_value (const mxArray_octave_value& arg) |
|
656 : mxArray_base (arg), val (arg.val), mutate_flag (arg.mutate_flag), |
|
657 id (arg.id), class_name (strsave (arg.class_name)), ndims (arg.ndims), |
|
658 dims (ndims > 0 ? static_cast<int *> (malloc (ndims * sizeof (int))) : 0) |
|
659 { |
|
660 if (dims) |
|
661 { |
|
662 for (int i = 0; i < ndims; i++) |
|
663 dims[i] = arg.dims[i]; |
|
664 } |
|
665 } |
|
666 |
|
667 private: |
|
668 |
|
669 octave_value val; |
|
670 |
|
671 mutable bool mutate_flag; |
|
672 |
|
673 // Caching these does not cost much or lead to much duplicated |
|
674 // code. For other things, we just request mutation to a |
|
675 // Matlab-style mxArray object. |
|
676 |
|
677 mutable mxClassID id; |
|
678 mutable char *class_name; |
|
679 mutable int ndims; |
|
680 mutable int *dims; |
|
681 }; |
|
682 |
|
683 // The base class for the Matlab-style representation, used to handle |
|
684 // things that are common to all Matlab-style objects. |
|
685 |
|
686 class mxArray_matlab : public mxArray_base |
|
687 { |
|
688 protected: |
|
689 |
|
690 mxArray_matlab (mxClassID id_arg = mxUNKNOWN_CLASS) |
|
691 : mxArray_base (), class_name (0), id (id_arg), ndims (0), dims (0) { } |
|
692 |
|
693 mxArray_matlab (mxClassID id_arg, int ndims_arg, const int *dims_arg) |
|
694 : mxArray_base (), class_name (0), id (id_arg), |
|
695 ndims (ndims_arg < 2 ? 2 : ndims_arg), |
|
696 dims (static_cast<int *> (malloc (ndims * sizeof (int)))) |
|
697 { |
|
698 if (ndims_arg < 2) |
|
699 { |
|
700 dims[0] = 1; |
|
701 dims[1] = 1; |
|
702 } |
|
703 |
|
704 for (int i = 0; i < ndims_arg; i++) |
|
705 dims[i] = dims_arg[i]; |
|
706 |
|
707 for (int i = ndims - 1; i > 1; i--) |
|
708 { |
|
709 if (dims[i] == 1) |
|
710 ndims--; |
|
711 else |
|
712 break; |
|
713 } |
|
714 } |
|
715 |
|
716 mxArray_matlab (mxClassID id_arg, const dim_vector& dv) |
|
717 : mxArray_base (), class_name (0), id (id_arg), |
|
718 ndims (dv.length ()), |
|
719 dims (static_cast<int *> (malloc (ndims * sizeof (int)))) |
|
720 { |
|
721 for (int i = 0; i < ndims; i++) |
|
722 dims[i] = dv(i); |
|
723 |
|
724 for (int i = ndims - 1; i > 1; i--) |
|
725 { |
|
726 if (dims[i] == 1) |
|
727 ndims--; |
|
728 else |
|
729 break; |
|
730 } |
|
731 } |
|
732 |
|
733 mxArray_matlab (mxClassID id_arg, int m, int n) |
|
734 : mxArray_base (), class_name (0), id (id_arg), ndims (2), |
|
735 dims (static_cast<int *> (malloc (ndims * sizeof (int)))) |
|
736 { |
|
737 dims[0] = m; |
|
738 dims[1] = n; |
|
739 } |
|
740 |
|
741 public: |
|
742 |
|
743 ~mxArray_matlab (void) |
|
744 { |
|
745 mxFree (class_name); |
|
746 mxFree (dims); |
|
747 } |
|
748 |
|
749 int is_cell (void) const { return id == mxCELL_CLASS; } |
|
750 |
|
751 int is_char (void) const { return id == mxCHAR_CLASS; } |
|
752 |
|
753 int is_complex (void) const { return 0; } |
|
754 |
|
755 int is_double (void) const { return id == mxDOUBLE_CLASS; } |
|
756 |
|
757 int is_int16 (void) const { return id == mxINT16_CLASS; } |
|
758 |
|
759 int is_int32 (void) const { return id == mxINT32_CLASS; } |
|
760 |
|
761 int is_int64 (void) const { return id == mxINT64_CLASS; } |
|
762 |
|
763 int is_int8 (void) const { return id == mxINT8_CLASS; } |
|
764 |
|
765 int is_logical (void) const { return id == mxLOGICAL_CLASS; } |
|
766 |
|
767 int is_numeric (void) const |
|
768 { |
|
769 return (id == mxDOUBLE_CLASS || id == mxSINGLE_CLASS |
|
770 || id == mxINT8_CLASS || id == mxUINT8_CLASS |
|
771 || id == mxINT16_CLASS || id == mxUINT16_CLASS |
|
772 || id == mxINT32_CLASS || id == mxUINT32_CLASS |
|
773 || id == mxINT64_CLASS || id == mxUINT64_CLASS); |
|
774 } |
|
775 |
|
776 int is_single (void) const { return id == mxSINGLE_CLASS; } |
|
777 |
|
778 int is_sparse (void) const { return 0; } |
|
779 |
|
780 int is_struct (void) const { return id == mxSTRUCT_CLASS; } |
|
781 |
|
782 int is_uint16 (void) const { return id == mxUINT16_CLASS; } |
|
783 |
|
784 int is_uint32 (void) const { return id == mxUINT32_CLASS; } |
|
785 |
|
786 int is_uint64 (void) const { return id == mxUINT64_CLASS; } |
|
787 |
|
788 int is_uint8 (void) const { return id == mxUINT8_CLASS; } |
|
789 |
|
790 int is_logical_scalar_true (void) const |
|
791 { |
|
792 return (is_logical_scalar () |
|
793 && static_cast<mxLogical *> (get_data ())[0] != 0); |
|
794 } |
|
795 |
|
796 int get_m (void) const { return dims[0]; } |
|
797 |
6187
|
798 int get_n (void) const |
|
799 { |
|
800 int n = 1; |
|
801 |
|
802 for (int i = ndims - 1 ; i > 0 ; i--) |
|
803 n *= dims[i]; |
|
804 |
|
805 return n; |
|
806 } |
5900
|
807 |
|
808 int *get_dimensions (void) const { return dims; } |
|
809 |
|
810 int get_number_of_dimensions (void) const { return ndims; } |
|
811 |
|
812 void set_m (int m) { dims[0] = m; } |
|
813 |
|
814 void set_n (int n) { dims[1] = n; } |
|
815 |
|
816 void set_dimensions (int *dims_arg, int ndims_arg) |
|
817 { |
|
818 dims = dims_arg; |
|
819 ndims = ndims_arg; |
|
820 } |
|
821 |
|
822 int get_number_of_elements (void) const |
|
823 { |
|
824 int retval = dims[0]; |
|
825 |
|
826 for (int i = 1; i < ndims; i++) |
|
827 retval *= dims[i]; |
|
828 |
|
829 return retval; |
|
830 } |
|
831 |
|
832 int is_empty (void) const { return get_number_of_elements () == 0; } |
|
833 |
|
834 mxClassID get_class_id (void) const { return id; } |
|
835 |
|
836 const char *get_class_name (void) const |
|
837 { |
|
838 switch (id) |
|
839 { |
|
840 case mxCELL_CLASS: return "cell"; |
|
841 case mxSTRUCT_CLASS: return "struct"; |
|
842 case mxLOGICAL_CLASS: return "logical"; |
|
843 case mxCHAR_CLASS: return "char"; |
|
844 case mxDOUBLE_CLASS: return "double"; |
|
845 case mxSINGLE_CLASS: return "single"; |
|
846 case mxINT8_CLASS: return "int8"; |
|
847 case mxUINT8_CLASS: return "uint8"; |
|
848 case mxINT16_CLASS: return "int16"; |
|
849 case mxUINT16_CLASS: return "uint16"; |
|
850 case mxINT32_CLASS: return "int32"; |
|
851 case mxUINT32_CLASS: return "uint32"; |
|
852 case mxINT64_CLASS: return "int64"; |
|
853 case mxUINT64_CLASS: return "uint64"; |
6218
|
854 case mxFUNCTION_CLASS: return "function_handle"; |
5900
|
855 default: return "unknown"; |
|
856 } |
|
857 } |
|
858 |
|
859 void set_class_name (const char *name_arg) |
|
860 { |
|
861 mxFree (class_name); |
|
862 class_name = static_cast<char *> (malloc (strlen (name_arg) + 1)); |
|
863 strcpy (class_name, name_arg); |
|
864 } |
|
865 |
|
866 mxArray *get_cell (int /*idx*/) const |
|
867 { |
|
868 invalid_type_error (); |
|
869 return 0; |
|
870 } |
|
871 |
|
872 void set_cell (int /*idx*/, mxArray */*val*/) |
|
873 { |
|
874 invalid_type_error (); |
|
875 } |
|
876 |
6332
|
877 double get_scalar (void) const |
|
878 { |
|
879 invalid_type_error (); |
|
880 return 0; |
|
881 } |
|
882 |
5900
|
883 void *get_data (void) const |
|
884 { |
|
885 invalid_type_error (); |
|
886 return 0; |
|
887 } |
|
888 |
|
889 void *get_imag_data (void) const |
|
890 { |
|
891 invalid_type_error (); |
|
892 return 0; |
|
893 } |
|
894 |
|
895 void set_data (void */*pr*/) |
|
896 { |
|
897 invalid_type_error (); |
|
898 } |
|
899 |
|
900 void set_imag_data (void */*pi*/) |
|
901 { |
|
902 invalid_type_error (); |
|
903 } |
|
904 |
|
905 int *get_ir (void) const |
|
906 { |
|
907 invalid_type_error (); |
|
908 return 0; |
|
909 } |
|
910 |
|
911 int *get_jc (void) const |
|
912 { |
|
913 invalid_type_error (); |
|
914 return 0; |
|
915 } |
|
916 |
|
917 int get_nzmax (void) const |
|
918 { |
|
919 invalid_type_error (); |
|
920 return 0; |
|
921 } |
|
922 |
|
923 void set_ir (int */*ir*/) |
|
924 { |
|
925 invalid_type_error (); |
|
926 } |
|
927 |
|
928 void set_jc (int */*jc*/) |
|
929 { |
|
930 invalid_type_error (); |
|
931 } |
|
932 |
|
933 void set_nzmax (int /*nzmax*/) |
|
934 { |
|
935 invalid_type_error (); |
|
936 } |
|
937 |
|
938 int add_field (const char */*key*/) |
|
939 { |
|
940 invalid_type_error (); |
|
941 return -1; |
|
942 } |
|
943 |
|
944 void remove_field (int /*key_num*/) |
|
945 { |
|
946 invalid_type_error (); |
|
947 } |
|
948 |
|
949 mxArray *get_field_by_number (int /*index*/, int /*key_num*/) const |
|
950 { |
|
951 invalid_type_error (); |
|
952 return 0; |
|
953 } |
|
954 |
|
955 void set_field_by_number (int /*index*/, int /*key_num*/, mxArray */*val*/) |
|
956 { |
|
957 invalid_type_error (); |
|
958 } |
|
959 |
|
960 int get_number_of_fields (void) const |
|
961 { |
|
962 invalid_type_error (); |
|
963 return 0; |
|
964 } |
|
965 |
|
966 const char *get_field_name_by_number (int /*key_num*/) const |
|
967 { |
|
968 invalid_type_error (); |
|
969 return 0; |
|
970 } |
|
971 |
|
972 int get_field_number (const char */*key*/) const |
|
973 { |
|
974 return -1; |
|
975 } |
|
976 |
|
977 int get_string (char */*buf*/, int /*buflen*/) const |
|
978 { |
|
979 invalid_type_error (); |
|
980 return 0; |
|
981 } |
|
982 |
|
983 char *array_to_string (void) const |
|
984 { |
|
985 invalid_type_error (); |
|
986 return 0; |
|
987 } |
|
988 |
|
989 int calc_single_subscript (int nsubs, int *subs) const |
|
990 { |
|
991 int retval = 0; |
|
992 |
|
993 int n = nsubs <= ndims ? nsubs : ndims; |
|
994 |
|
995 while (--n > 0) |
|
996 retval = retval * dims[n] + subs[n]; |
|
997 |
|
998 return retval; |
|
999 } |
|
1000 |
|
1001 int get_element_size (void) const |
|
1002 { |
|
1003 switch (id) |
|
1004 { |
|
1005 case mxCELL_CLASS: return sizeof (mxArray *); |
|
1006 case mxSTRUCT_CLASS: return sizeof (mxArray *); |
|
1007 case mxLOGICAL_CLASS: return sizeof (mxLogical); |
|
1008 case mxCHAR_CLASS: return sizeof (mxChar); |
|
1009 case mxDOUBLE_CLASS: return sizeof (double); |
|
1010 case mxSINGLE_CLASS: return sizeof (float); |
|
1011 case mxINT8_CLASS: return 1; |
|
1012 case mxUINT8_CLASS: return 1; |
|
1013 case mxINT16_CLASS: return 2; |
|
1014 case mxUINT16_CLASS: return 2; |
|
1015 case mxINT32_CLASS: return 4; |
|
1016 case mxUINT32_CLASS: return 4; |
|
1017 case mxINT64_CLASS: return 8; |
|
1018 case mxUINT64_CLASS: return 8; |
|
1019 case mxFUNCTION_CLASS: return 0; |
|
1020 default: return 0; |
|
1021 } |
|
1022 } |
|
1023 |
|
1024 protected: |
|
1025 |
|
1026 mxArray_matlab (const mxArray_matlab& val) |
|
1027 : mxArray_base (val), class_name (strsave (val.class_name)), |
|
1028 id (val.id), ndims (val.ndims), |
|
1029 dims (static_cast<int *> (malloc (ndims * sizeof (int)))) |
|
1030 { |
|
1031 for (int i = 0; i < ndims; i++) |
|
1032 dims[i] = val.dims[i]; |
|
1033 } |
|
1034 |
|
1035 dim_vector |
|
1036 dims_to_dim_vector (void) const |
|
1037 { |
|
1038 int nd = get_number_of_dimensions (); |
|
1039 |
|
1040 int *d = get_dimensions (); |
|
1041 |
|
1042 dim_vector dv; |
|
1043 dv.resize (nd); |
|
1044 |
|
1045 for (int i = 0; i < nd; i++) |
|
1046 dv(i) = d[i]; |
|
1047 |
|
1048 return dv; |
|
1049 } |
|
1050 |
|
1051 private: |
|
1052 |
|
1053 char *class_name; |
|
1054 |
|
1055 mxClassID id; |
|
1056 |
|
1057 int ndims; |
|
1058 int *dims; |
|
1059 |
|
1060 void invalid_type_error (void) const |
|
1061 { |
|
1062 error ("invalid type for operation"); |
|
1063 } |
|
1064 }; |
|
1065 |
|
1066 // Matlab-style numeric, character, and logical data. |
|
1067 |
|
1068 class mxArray_number : public mxArray_matlab |
|
1069 { |
|
1070 public: |
|
1071 |
|
1072 mxArray_number (mxClassID id_arg, int ndims_arg, const int *dims_arg, |
|
1073 mxComplexity flag = mxREAL) |
|
1074 : mxArray_matlab (id_arg, ndims_arg, dims_arg), |
|
1075 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1076 pi (flag == mxCOMPLEX ? calloc (get_number_of_elements (), get_element_size ()) : 0) { } |
|
1077 |
|
1078 mxArray_number (mxClassID id_arg, const dim_vector& dv, |
|
1079 mxComplexity flag = mxREAL) |
|
1080 : mxArray_matlab (id_arg, dv), |
|
1081 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1082 pi (flag == mxCOMPLEX ? calloc (get_number_of_elements (), get_element_size ()) : 0) { } |
|
1083 |
|
1084 mxArray_number (mxClassID id_arg, int m, int n, mxComplexity flag = mxREAL) |
|
1085 : mxArray_matlab (id_arg, m, n), |
|
1086 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1087 pi (flag == mxCOMPLEX ? calloc (get_number_of_elements (), get_element_size ()) : 0) { } |
|
1088 |
|
1089 mxArray_number (mxClassID id_arg, double val) |
|
1090 : mxArray_matlab (id_arg, 1, 1), |
|
1091 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1092 pi (0) |
|
1093 { |
|
1094 double *dpr = static_cast<double *> (pr); |
|
1095 dpr[0] = val; |
|
1096 } |
|
1097 |
|
1098 mxArray_number (mxClassID id_arg, mxLogical val) |
|
1099 : mxArray_matlab (id_arg, 1, 1), |
|
1100 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1101 pi (0) |
|
1102 { |
|
1103 mxLogical *lpr = static_cast<mxLogical *> (pr); |
|
1104 lpr[0] = val; |
|
1105 } |
|
1106 |
|
1107 mxArray_number (const char *str) |
|
1108 : mxArray_matlab (mxCHAR_CLASS, 1, strlen (str)), |
|
1109 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1110 pi (0) |
|
1111 { |
|
1112 mxChar *cpr = static_cast<mxChar *> (pr); |
|
1113 int nel = get_number_of_elements (); |
|
1114 for (int i = 0; i < nel; i++) |
|
1115 cpr[i] = str[i]; |
|
1116 } |
|
1117 |
|
1118 mxArray_number (int m, const char **str) |
|
1119 : mxArray_matlab (mxCHAR_CLASS, m, max_str_len (m, str)), |
|
1120 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1121 pi (0) |
|
1122 { |
|
1123 mxChar *cpr = static_cast<mxChar *> (pr); |
|
1124 |
|
1125 int *dv = get_dimensions (); |
|
1126 |
|
1127 int nc = dv[1]; |
|
1128 |
|
1129 for (int j = 0; j < m; j++) |
|
1130 { |
|
1131 const char *ptr = str[j]; |
|
1132 |
|
1133 int tmp_len = strlen (ptr); |
|
1134 |
|
1135 for (int i = 0; i < tmp_len; i++) |
6224
|
1136 cpr[m*i+j] = static_cast<mxChar> (ptr[i]); |
5900
|
1137 |
|
1138 for (int i = tmp_len; i < nc; i++) |
6224
|
1139 cpr[m*i+j] = static_cast<mxChar> (' '); |
5900
|
1140 } |
|
1141 } |
|
1142 |
|
1143 mxArray_number *clone (void) const { return new mxArray_number (*this); } |
|
1144 |
|
1145 ~mxArray_number (void) |
|
1146 { |
|
1147 mxFree (pr); |
|
1148 mxFree (pi); |
|
1149 } |
|
1150 |
5907
|
1151 int is_complex (void) const { return pi != 0; } |
|
1152 |
6332
|
1153 double get_scalar (void) const |
|
1154 { |
|
1155 double retval = 0; |
|
1156 |
|
1157 switch (get_class_id ()) |
|
1158 { |
|
1159 case mxLOGICAL_CLASS: |
|
1160 retval = *(static_cast<bool *> (pr)); |
|
1161 break; |
|
1162 |
|
1163 case mxCHAR_CLASS: |
|
1164 retval = *(static_cast<mxChar *> (pr)); |
|
1165 break; |
|
1166 |
|
1167 case mxSINGLE_CLASS: |
|
1168 retval = *(static_cast<float *> (pr)); |
|
1169 break; |
|
1170 |
|
1171 case mxDOUBLE_CLASS: |
|
1172 retval = *(static_cast<double *> (pr)); |
|
1173 break; |
|
1174 |
|
1175 case mxINT8_CLASS: |
|
1176 retval = *(static_cast<int8_t *> (pr)); |
|
1177 break; |
|
1178 |
|
1179 case mxUINT8_CLASS: |
|
1180 retval = *(static_cast<uint8_t *> (pr)); |
|
1181 break; |
|
1182 |
|
1183 case mxINT16_CLASS: |
|
1184 retval = *(static_cast<int16_t *> (pr)); |
|
1185 break; |
|
1186 |
|
1187 case mxUINT16_CLASS: |
|
1188 retval = *(static_cast<uint16_t *> (pr)); |
|
1189 break; |
|
1190 |
|
1191 case mxINT32_CLASS: |
|
1192 retval = *(static_cast<int32_t *> (pr)); |
|
1193 break; |
|
1194 |
|
1195 case mxUINT32_CLASS: |
|
1196 retval = *(static_cast<uint32_t *> (pr)); |
|
1197 break; |
|
1198 |
|
1199 case mxINT64_CLASS: |
|
1200 retval = *(static_cast<int64_t *> (pr)); |
|
1201 break; |
|
1202 |
|
1203 case mxUINT64_CLASS: |
|
1204 retval = *(static_cast<uint64_t *> (pr)); |
|
1205 break; |
|
1206 |
|
1207 default: |
|
1208 panic_impossible (); |
|
1209 } |
|
1210 |
|
1211 return retval; |
|
1212 } |
|
1213 |
5907
|
1214 void *get_data (void) const { return pr; } |
|
1215 |
|
1216 void *get_imag_data (void) const { return pi; } |
|
1217 |
|
1218 void set_data (void *pr_arg) { pr = pr_arg; } |
|
1219 |
|
1220 void set_imag_data (void *pi_arg) { pi = pi_arg; } |
|
1221 |
|
1222 int get_string (char *buf, int buflen) const |
|
1223 { |
|
1224 int retval = 1; |
|
1225 |
|
1226 int n = get_number_of_elements (); |
|
1227 |
|
1228 if (n < buflen) |
|
1229 { |
|
1230 mxChar *ptr = static_cast<mxChar *> (pr); |
|
1231 |
|
1232 for (int i = 0; i < n; i++) |
|
1233 buf[i] = static_cast<char> (ptr[i]); |
|
1234 |
|
1235 buf[n] = 0; |
|
1236 } |
|
1237 |
|
1238 return retval; |
|
1239 } |
|
1240 |
|
1241 char *array_to_string (void) const |
|
1242 { |
|
1243 // FIXME -- this is suposed to handle multi-byte character |
|
1244 // strings. |
|
1245 |
|
1246 int nel = get_number_of_elements (); |
|
1247 |
|
1248 char *buf = static_cast<char *> (malloc (nel + 1)); |
|
1249 |
|
1250 if (buf) |
|
1251 { |
|
1252 mxChar *ptr = static_cast<mxChar *> (pr); |
|
1253 |
|
1254 for (int i = 0; i < nel; i++) |
|
1255 buf[i] = static_cast<char> (ptr[i]); |
|
1256 |
|
1257 buf[nel] = '\0'; |
|
1258 } |
|
1259 |
|
1260 return buf; |
|
1261 } |
|
1262 |
|
1263 protected: |
|
1264 |
5900
|
1265 template <typename ELT_T, typename ARRAY_T, typename ARRAY_ELT_T> |
|
1266 octave_value |
|
1267 int_to_ov (const dim_vector& dv) const |
|
1268 { |
|
1269 octave_value retval; |
|
1270 |
|
1271 int nel = get_number_of_elements (); |
|
1272 |
|
1273 ELT_T *ppr = static_cast<ELT_T *> (pr); |
|
1274 |
|
1275 if (pi) |
|
1276 error ("complex integer types are not supported"); |
|
1277 else |
|
1278 { |
|
1279 ARRAY_T val (dv); |
|
1280 |
|
1281 ARRAY_ELT_T *ptr = val.fortran_vec (); |
|
1282 |
|
1283 for (int i = 0; i < nel; i++) |
|
1284 ptr[i] = ppr[i]; |
|
1285 |
|
1286 retval = val; |
|
1287 } |
|
1288 |
|
1289 return retval; |
|
1290 } |
|
1291 |
|
1292 octave_value as_octave_value (void) const |
|
1293 { |
|
1294 octave_value retval; |
|
1295 |
|
1296 dim_vector dv = dims_to_dim_vector (); |
|
1297 |
|
1298 switch (get_class_id ()) |
|
1299 { |
|
1300 case mxLOGICAL_CLASS: |
|
1301 retval = int_to_ov<bool, boolNDArray, bool> (dv); |
|
1302 break; |
|
1303 |
|
1304 case mxCHAR_CLASS: |
|
1305 { |
|
1306 int nel = get_number_of_elements (); |
|
1307 |
|
1308 mxChar *ppr = static_cast<mxChar *> (pr); |
|
1309 |
|
1310 charNDArray val (dv); |
|
1311 |
|
1312 char *ptr = val.fortran_vec (); |
|
1313 |
|
1314 for (int i = 0; i < nel; i++) |
|
1315 ptr[i] = static_cast<char> (ppr[i]); |
|
1316 |
|
1317 retval = octave_value (val, true, '\''); |
|
1318 } |
|
1319 break; |
|
1320 |
|
1321 case mxSINGLE_CLASS: |
6271
|
1322 { |
|
1323 int nel = get_number_of_elements (); |
|
1324 |
|
1325 float *ppr = static_cast<float *> (pr); |
|
1326 |
|
1327 if (pi) |
|
1328 { |
|
1329 ComplexNDArray val (dv); |
|
1330 |
|
1331 Complex *ptr = val.fortran_vec (); |
|
1332 |
|
1333 float *ppi = static_cast<float *> (pi); |
|
1334 |
|
1335 for (int i = 0; i < nel; i++) |
|
1336 ptr[i] = Complex (ppr[i], ppi[i]); |
|
1337 |
|
1338 retval = val; |
|
1339 } |
|
1340 else |
|
1341 { |
|
1342 NDArray val (dv); |
|
1343 |
|
1344 double *ptr = val.fortran_vec (); |
|
1345 |
|
1346 for (int i = 0; i < nel; i++) |
|
1347 ptr[i] = ppr[i]; |
|
1348 |
|
1349 retval = val; |
|
1350 } |
|
1351 } |
5900
|
1352 break; |
|
1353 |
|
1354 case mxDOUBLE_CLASS: |
|
1355 { |
|
1356 int nel = get_number_of_elements (); |
|
1357 |
|
1358 double *ppr = static_cast<double *> (pr); |
|
1359 |
|
1360 if (pi) |
|
1361 { |
|
1362 ComplexNDArray val (dv); |
|
1363 |
|
1364 Complex *ptr = val.fortran_vec (); |
|
1365 |
|
1366 double *ppi = static_cast<double *> (pi); |
|
1367 |
|
1368 for (int i = 0; i < nel; i++) |
|
1369 ptr[i] = Complex (ppr[i], ppi[i]); |
|
1370 |
|
1371 retval = val; |
|
1372 } |
|
1373 else |
|
1374 { |
|
1375 NDArray val (dv); |
|
1376 |
|
1377 double *ptr = val.fortran_vec (); |
|
1378 |
|
1379 for (int i = 0; i < nel; i++) |
|
1380 ptr[i] = ppr[i]; |
|
1381 |
|
1382 retval = val; |
|
1383 } |
|
1384 } |
|
1385 break; |
|
1386 |
|
1387 case mxINT8_CLASS: |
|
1388 retval = int_to_ov<int8_t, int8NDArray, octave_int8> (dv); |
|
1389 break; |
|
1390 |
|
1391 case mxUINT8_CLASS: |
|
1392 retval = int_to_ov<uint8_t, uint8NDArray, octave_uint8> (dv); |
|
1393 break; |
|
1394 |
|
1395 case mxINT16_CLASS: |
|
1396 retval = int_to_ov<int16_t, int16NDArray, octave_int16> (dv); |
|
1397 break; |
|
1398 |
|
1399 case mxUINT16_CLASS: |
|
1400 retval = int_to_ov<uint16_t, uint16NDArray, octave_uint16> (dv); |
|
1401 break; |
|
1402 |
|
1403 case mxINT32_CLASS: |
|
1404 retval = int_to_ov<int32_t, int32NDArray, octave_int32> (dv); |
|
1405 break; |
|
1406 |
|
1407 case mxUINT32_CLASS: |
|
1408 retval = int_to_ov<uint32_t, uint32NDArray, octave_uint32> (dv); |
|
1409 break; |
|
1410 |
|
1411 case mxINT64_CLASS: |
|
1412 retval = int_to_ov<int64_t, int64NDArray, octave_int64> (dv); |
|
1413 break; |
|
1414 |
|
1415 case mxUINT64_CLASS: |
|
1416 retval = int_to_ov<uint64_t, uint64NDArray, octave_uint64> (dv); |
|
1417 break; |
|
1418 |
|
1419 default: |
|
1420 panic_impossible (); |
|
1421 } |
|
1422 |
|
1423 return retval; |
|
1424 } |
|
1425 |
|
1426 mxArray_number (const mxArray_number& val) |
|
1427 : mxArray_matlab (val), |
|
1428 pr (malloc (get_number_of_elements () * get_element_size ())), |
|
1429 pi (val.pi ? malloc (get_number_of_elements () * get_element_size ()) : 0) |
|
1430 { |
5907
|
1431 size_t nbytes = get_number_of_elements () * get_element_size (); |
|
1432 |
|
1433 if (pr) |
|
1434 memcpy (pr, val.pr, nbytes); |
5900
|
1435 |
|
1436 if (pi) |
5907
|
1437 memcpy (pi, val.pi, nbytes); |
5900
|
1438 } |
|
1439 |
|
1440 private: |
|
1441 |
|
1442 void *pr; |
|
1443 void *pi; |
|
1444 }; |
|
1445 |
|
1446 // Matlab-style sparse arrays. |
|
1447 |
5903
|
1448 class mxArray_sparse : public mxArray_matlab |
5900
|
1449 { |
|
1450 public: |
|
1451 |
|
1452 mxArray_sparse (mxClassID id_arg, int m, int n, int nzmax_arg, |
|
1453 mxComplexity flag = mxREAL) |
5903
|
1454 : mxArray_matlab (id_arg, m, n), nzmax (nzmax_arg) |
5900
|
1455 { |
5903
|
1456 pr = (calloc (nzmax, get_element_size ())); |
|
1457 pi = (flag == mxCOMPLEX ? calloc (nzmax, get_element_size ()) : 0); |
5900
|
1458 ir = static_cast<int *> (calloc (nzmax, sizeof (int))); |
5903
|
1459 jc = static_cast<int *> (calloc (n + 1, sizeof (int))); |
5900
|
1460 } |
|
1461 |
|
1462 mxArray_sparse *clone (void) const { return new mxArray_sparse (*this); } |
|
1463 |
|
1464 ~mxArray_sparse (void) |
|
1465 { |
5903
|
1466 mxFree (pr); |
|
1467 mxFree (pi); |
5900
|
1468 mxFree (ir); |
|
1469 mxFree (jc); |
|
1470 } |
|
1471 |
5907
|
1472 int is_complex (void) const { return pi != 0; } |
|
1473 |
|
1474 int is_sparse (void) const { return 1; } |
|
1475 |
|
1476 void *get_data (void) const { return pr; } |
|
1477 |
|
1478 void *get_imag_data (void) const { return pi; } |
|
1479 |
|
1480 void set_data (void *pr_arg) { pr = pr_arg; } |
|
1481 |
|
1482 void set_imag_data (void *pi_arg) { pi = pi_arg; } |
|
1483 |
|
1484 int *get_ir (void) const { return ir; } |
|
1485 |
|
1486 int *get_jc (void) const { return jc; } |
|
1487 |
|
1488 int get_nzmax (void) const { return nzmax; } |
|
1489 |
|
1490 void set_ir (int *ir_arg) { ir = ir_arg; } |
|
1491 |
|
1492 void set_jc (int *jc_arg) { jc = jc_arg; } |
|
1493 |
|
1494 void set_nzmax (int nzmax_arg) { nzmax = nzmax_arg; } |
|
1495 |
|
1496 protected: |
|
1497 |
5900
|
1498 octave_value as_octave_value (void) const |
|
1499 { |
5903
|
1500 octave_value retval; |
|
1501 |
|
1502 dim_vector dv = dims_to_dim_vector (); |
|
1503 |
|
1504 switch (get_class_id ()) |
|
1505 { |
|
1506 case mxLOGICAL_CLASS: |
|
1507 { |
|
1508 bool *ppr = static_cast<bool *> (pr); |
|
1509 |
5982
|
1510 SparseBoolMatrix val (get_m (), get_n (), |
|
1511 static_cast<octave_idx_type> (nzmax)); |
5903
|
1512 |
|
1513 for (int i = 0; i < nzmax; i++) |
|
1514 { |
|
1515 val.xdata(i) = ppr[i]; |
|
1516 val.xridx(i) = ir[i]; |
|
1517 } |
|
1518 |
|
1519 for (int i = 0; i < get_n () + 1; i++) |
|
1520 val.xcidx(i) = jc[i]; |
|
1521 |
|
1522 retval = val; |
|
1523 } |
|
1524 break; |
|
1525 |
|
1526 case mxSINGLE_CLASS: |
6271
|
1527 error ("single precision sparse data type not supported"); |
5903
|
1528 break; |
|
1529 |
|
1530 case mxDOUBLE_CLASS: |
|
1531 { |
|
1532 if (pi) |
|
1533 { |
|
1534 double *ppr = static_cast<double *> (pr); |
|
1535 double *ppi = static_cast<double *> (pi); |
|
1536 |
5982
|
1537 SparseComplexMatrix val (get_m (), get_n (), |
|
1538 static_cast<octave_idx_type> (nzmax)); |
5903
|
1539 |
|
1540 for (int i = 0; i < nzmax; i++) |
|
1541 { |
|
1542 val.xdata(i) = Complex (ppr[i], ppi[i]); |
|
1543 val.xridx(i) = ir[i]; |
|
1544 } |
|
1545 |
|
1546 for (int i = 0; i < get_n () + 1; i++) |
|
1547 val.xcidx(i) = jc[i]; |
|
1548 |
|
1549 retval = val; |
|
1550 } |
|
1551 else |
|
1552 { |
|
1553 double *ppr = static_cast<double *> (pr); |
|
1554 |
5982
|
1555 SparseMatrix val (get_m (), get_n (), |
|
1556 static_cast<octave_idx_type> (nzmax)); |
5903
|
1557 |
|
1558 for (int i = 0; i < nzmax; i++) |
|
1559 { |
|
1560 val.xdata(i) = ppr[i]; |
|
1561 val.xridx(i) = ir[i]; |
|
1562 } |
|
1563 |
|
1564 for (int i = 0; i < get_n () + 1; i++) |
|
1565 val.xcidx(i) = jc[i]; |
|
1566 |
|
1567 retval = val; |
|
1568 } |
|
1569 } |
|
1570 break; |
|
1571 |
|
1572 default: |
|
1573 panic_impossible (); |
|
1574 } |
|
1575 |
|
1576 return retval; |
5900
|
1577 } |
|
1578 |
|
1579 private: |
|
1580 |
|
1581 int nzmax; |
|
1582 |
5903
|
1583 void *pr; |
|
1584 void *pi; |
5900
|
1585 int *ir; |
|
1586 int *jc; |
|
1587 |
|
1588 mxArray_sparse (const mxArray_sparse& val) |
5903
|
1589 : mxArray_matlab (val), nzmax (val.nzmax), |
5900
|
1590 ir (static_cast<int *> (malloc (nzmax * sizeof (int)))), |
|
1591 jc (static_cast<int *> (malloc (nzmax * sizeof (int)))) |
|
1592 { |
5907
|
1593 size_t nbytes = nzmax * get_element_size (); |
|
1594 |
|
1595 if (pr) |
|
1596 memcpy (pr, val.pr, nbytes); |
|
1597 |
5903
|
1598 if (pi) |
5907
|
1599 memcpy (pi, val.pi, nbytes); |
|
1600 |
|
1601 if (ir) |
|
1602 memcpy (ir, val.ir, nzmax * sizeof (int)); |
|
1603 |
|
1604 if (jc) |
|
1605 memcpy (jc, val.jc, (val.get_n () + 1) * sizeof (int)); |
5900
|
1606 } |
|
1607 }; |
|
1608 |
|
1609 // Matlab-style struct arrays. |
|
1610 |
|
1611 class mxArray_struct : public mxArray_matlab |
|
1612 { |
|
1613 public: |
|
1614 |
|
1615 mxArray_struct (int ndims_arg, const int *dims_arg, int num_keys_arg, |
|
1616 const char **keys) |
|
1617 : mxArray_matlab (mxSTRUCT_CLASS, ndims_arg, dims_arg), nfields (num_keys_arg), |
|
1618 fields (static_cast<char **> (calloc (nfields, sizeof (char *)))), |
|
1619 data (static_cast<mxArray **> (calloc (nfields * get_number_of_elements (), sizeof (mxArray *)))) |
|
1620 { |
|
1621 init (keys); |
|
1622 } |
|
1623 |
|
1624 mxArray_struct (const dim_vector& dv, int num_keys_arg, const char **keys) |
|
1625 : mxArray_matlab (mxSTRUCT_CLASS, dv), nfields (num_keys_arg), |
|
1626 fields (static_cast<char **> (calloc (nfields, sizeof (char *)))), |
|
1627 data (static_cast<mxArray **> (calloc (nfields * get_number_of_elements (), sizeof (mxArray *)))) |
|
1628 { |
|
1629 init (keys); |
|
1630 } |
|
1631 |
|
1632 mxArray_struct (int m, int n, int num_keys_arg, const char **keys) |
|
1633 : mxArray_matlab (mxSTRUCT_CLASS, m, n), nfields (num_keys_arg), |
|
1634 fields (static_cast<char **> (calloc (nfields, sizeof (char *)))), |
|
1635 data (static_cast<mxArray **> (calloc (nfields * get_number_of_elements (), sizeof (mxArray *)))) |
|
1636 { |
|
1637 init (keys); |
|
1638 } |
|
1639 |
|
1640 void init (const char **keys) |
|
1641 { |
|
1642 for (int i = 0; i < nfields; i++) |
|
1643 fields[i] = strsave (keys[i]); |
|
1644 } |
|
1645 |
|
1646 mxArray_struct *clone (void) const { return new mxArray_struct (*this); } |
|
1647 |
|
1648 ~mxArray_struct (void) |
|
1649 { |
|
1650 for (int i = 0; i < nfields; i++) |
|
1651 mxFree (fields[i]); |
|
1652 |
|
1653 mxFree (fields); |
|
1654 |
|
1655 int ntot = nfields * get_number_of_elements (); |
|
1656 |
|
1657 for (int i = 0; i < ntot; i++) |
5905
|
1658 delete data[i]; |
5900
|
1659 |
|
1660 mxFree (data); |
|
1661 } |
|
1662 |
|
1663 int add_field (const char *key) |
|
1664 { |
|
1665 int retval = -1; |
|
1666 |
|
1667 if (valid_key (key)) |
|
1668 { |
|
1669 nfields++; |
|
1670 |
|
1671 fields = static_cast<char **> (mxRealloc (fields, nfields * sizeof (char *))); |
|
1672 |
|
1673 if (fields) |
|
1674 { |
|
1675 fields[nfields-1] = strsave (key); |
|
1676 |
|
1677 int nel = get_number_of_elements (); |
|
1678 |
|
1679 int ntot = nfields * nel; |
|
1680 |
|
1681 mxArray **new_data = static_cast<mxArray **> (malloc (ntot * sizeof (mxArray *))); |
|
1682 |
|
1683 if (new_data) |
|
1684 { |
|
1685 int j = 0; |
|
1686 int k = 0; |
|
1687 int n = 0; |
|
1688 |
|
1689 for (int i = 0; i < ntot; i++) |
|
1690 { |
|
1691 if (++n == nfields) |
|
1692 { |
|
1693 new_data[j++] = 0; |
|
1694 n = 0; |
|
1695 } |
|
1696 else |
|
1697 new_data[j++] = data[k++]; |
|
1698 } |
|
1699 |
|
1700 mxFree (data); |
|
1701 |
|
1702 data = new_data; |
|
1703 |
|
1704 retval = nfields - 1; |
|
1705 } |
|
1706 } |
|
1707 } |
|
1708 |
|
1709 return retval; |
|
1710 } |
|
1711 |
|
1712 void remove_field (int key_num) |
|
1713 { |
|
1714 if (key_num >= 0 && key_num < nfields) |
|
1715 { |
|
1716 int nel = get_number_of_elements (); |
|
1717 |
|
1718 int ntot = nfields * nel; |
|
1719 |
|
1720 int new_nfields = nfields - 1; |
|
1721 |
|
1722 char **new_fields = static_cast<char **> (malloc (new_nfields * sizeof (char *))); |
|
1723 |
|
1724 mxArray **new_data = static_cast<mxArray **> (malloc (new_nfields * nel * sizeof (mxArray *))); |
|
1725 |
|
1726 for (int i = 0; i < key_num; i++) |
|
1727 new_fields[i] = fields[i]; |
|
1728 |
|
1729 for (int i = key_num + 1; i < nfields; i++) |
|
1730 new_fields[i-1] = fields[i]; |
|
1731 |
|
1732 if (new_nfields > 0) |
|
1733 { |
|
1734 int j = 0; |
|
1735 int k = 0; |
|
1736 int n = 0; |
|
1737 |
|
1738 for (int i = 0; i < ntot; i++) |
|
1739 { |
|
1740 if (n == key_num) |
|
1741 k++; |
|
1742 else |
|
1743 new_data[j++] = data[k++]; |
|
1744 |
|
1745 if (++n == nfields) |
|
1746 n = 0; |
|
1747 } |
|
1748 } |
|
1749 |
|
1750 nfields = new_nfields; |
|
1751 |
|
1752 mxFree (fields); |
|
1753 mxFree (data); |
|
1754 |
|
1755 fields = new_fields; |
|
1756 data = new_data; |
|
1757 } |
|
1758 } |
|
1759 |
|
1760 mxArray *get_field_by_number (int index, int key_num) const |
|
1761 { |
6187
|
1762 return key_num >= 0 && key_num < nfields |
6188
|
1763 ? data[nfields * index + key_num] : 0; |
5900
|
1764 } |
|
1765 |
6071
|
1766 void set_field_by_number (int index, int key_num, mxArray *val); |
5900
|
1767 |
|
1768 int get_number_of_fields (void) const { return nfields; } |
|
1769 |
|
1770 const char *get_field_name_by_number (int key_num) const |
|
1771 { |
|
1772 return key_num >= 0 && key_num < nfields ? fields[key_num] : 0; |
|
1773 } |
|
1774 |
|
1775 int get_field_number (const char *key) const |
|
1776 { |
|
1777 int retval = -1; |
|
1778 |
|
1779 for (int i = 0; i < nfields; i++) |
|
1780 { |
|
1781 if (! strcmp (key, fields[i])) |
|
1782 { |
|
1783 retval = i; |
|
1784 break; |
|
1785 } |
|
1786 } |
|
1787 |
|
1788 return retval; |
|
1789 } |
|
1790 |
|
1791 void *get_data (void) const { return data; } |
|
1792 |
|
1793 void set_data (void *data_arg) { data = static_cast<mxArray **> (data_arg); } |
|
1794 |
5907
|
1795 protected: |
|
1796 |
|
1797 octave_value as_octave_value (void) const |
|
1798 { |
|
1799 dim_vector dv = dims_to_dim_vector (); |
|
1800 |
|
1801 string_vector keys (fields, nfields); |
|
1802 |
|
1803 Octave_map m; |
|
1804 |
|
1805 int ntot = nfields * get_number_of_elements (); |
|
1806 |
|
1807 for (int i = 0; i < nfields; i++) |
|
1808 { |
|
1809 Cell c (dv); |
|
1810 |
|
1811 octave_value *p = c.fortran_vec (); |
|
1812 |
|
1813 int k = 0; |
|
1814 for (int j = i; j < ntot; j += nfields) |
|
1815 p[k++] = mxArray::as_octave_value (data[j]); |
|
1816 |
|
1817 m.assign (keys[i], c); |
|
1818 } |
|
1819 |
|
1820 return m; |
|
1821 } |
|
1822 |
5900
|
1823 private: |
|
1824 |
|
1825 int nfields; |
|
1826 |
|
1827 char **fields; |
|
1828 |
|
1829 mxArray **data; |
|
1830 |
|
1831 mxArray_struct (const mxArray_struct& val) |
|
1832 : mxArray_matlab (val), nfields (val.nfields), |
|
1833 fields (static_cast<char **> (malloc (nfields * sizeof (char *)))), |
|
1834 data (static_cast<mxArray **> (malloc (nfields * get_number_of_elements () * sizeof (mxArray *)))) |
|
1835 { |
|
1836 for (int i = 0; i < nfields; i++) |
|
1837 fields[i] = strsave (val.fields[i]); |
|
1838 |
|
1839 int nel = get_number_of_elements (); |
|
1840 |
|
1841 for (int i = 0; i < nel * nfields; i++) |
|
1842 data[i] = val.data[i]->clone (); |
|
1843 } |
|
1844 }; |
|
1845 |
|
1846 // Matlab-style cell arrays. |
|
1847 |
|
1848 class mxArray_cell : public mxArray_matlab |
|
1849 { |
|
1850 public: |
|
1851 |
|
1852 mxArray_cell (int ndims_arg, const int *dims_arg) |
|
1853 : mxArray_matlab (mxCELL_CLASS, ndims_arg, dims_arg), |
|
1854 data (static_cast<mxArray **> (calloc (get_number_of_elements (), sizeof (mxArray *)))) { } |
|
1855 |
|
1856 mxArray_cell (const dim_vector& dv) |
|
1857 : mxArray_matlab (mxCELL_CLASS, dv), |
|
1858 data (static_cast<mxArray **> (calloc (get_number_of_elements (), sizeof (mxArray *)))) { } |
|
1859 |
|
1860 mxArray_cell (int m, int n) |
|
1861 : mxArray_matlab (mxCELL_CLASS, m, n), |
|
1862 data (static_cast<mxArray **> (calloc (get_number_of_elements (), sizeof (mxArray *)))) { } |
|
1863 |
|
1864 mxArray_cell *clone (void) const { return new mxArray_cell (*this); } |
|
1865 |
|
1866 ~mxArray_cell (void) |
|
1867 { |
|
1868 int nel = get_number_of_elements (); |
|
1869 |
|
1870 for (int i = 0; i < nel; i++) |
5905
|
1871 delete data[i]; |
5900
|
1872 |
|
1873 mxFree (data); |
|
1874 } |
|
1875 |
6187
|
1876 mxArray *get_cell (int idx) const |
|
1877 { |
|
1878 return idx >= 0 && idx < get_number_of_elements () ? data[idx] : 0; |
|
1879 } |
5907
|
1880 |
6071
|
1881 void set_cell (int idx, mxArray *val); |
5907
|
1882 |
|
1883 void *get_data (void) const { return data; } |
|
1884 |
|
1885 void set_data (void *data_arg) { data = static_cast<mxArray **> (data_arg); } |
|
1886 |
|
1887 protected: |
|
1888 |
5900
|
1889 octave_value as_octave_value (void) const |
|
1890 { |
|
1891 dim_vector dv = dims_to_dim_vector (); |
|
1892 |
|
1893 Cell c (dv); |
|
1894 |
|
1895 int nel = get_number_of_elements (); |
|
1896 |
|
1897 octave_value *p = c.fortran_vec (); |
|
1898 |
|
1899 for (int i = 0; i < nel; i++) |
5907
|
1900 p[i] = mxArray::as_octave_value (data[i]); |
5900
|
1901 |
|
1902 return c; |
|
1903 } |
|
1904 |
|
1905 private: |
|
1906 |
|
1907 mxArray **data; |
|
1908 |
|
1909 mxArray_cell (const mxArray_cell& val) |
|
1910 : mxArray_matlab (val), |
|
1911 data (static_cast<mxArray **> (malloc (get_number_of_elements () * sizeof (mxArray *)))) |
|
1912 { |
|
1913 int nel = get_number_of_elements (); |
|
1914 |
|
1915 for (int i = 0; i < nel; i++) |
|
1916 data[i] = val.data[i]->clone (); |
|
1917 } |
|
1918 }; |
|
1919 |
|
1920 // ------------------------------------------------------------------ |
|
1921 |
|
1922 mxArray::mxArray (const octave_value& ov) |
6065
|
1923 : rep (new mxArray_octave_value (ov)), name (0) { } |
5900
|
1924 |
|
1925 mxArray::mxArray (mxClassID id, int ndims, const int *dims, mxComplexity flag) |
6065
|
1926 : rep (new mxArray_number (id, ndims, dims, flag)), name (0) { } |
5900
|
1927 |
|
1928 mxArray::mxArray (mxClassID id, const dim_vector& dv, mxComplexity flag) |
6065
|
1929 : rep (new mxArray_number (id, dv, flag)), name (0) { } |
5900
|
1930 |
|
1931 mxArray::mxArray (mxClassID id, int m, int n, mxComplexity flag) |
6065
|
1932 : rep (new mxArray_number (id, m, n, flag)), name (0) { } |
5900
|
1933 |
|
1934 mxArray::mxArray (mxClassID id, double val) |
6065
|
1935 : rep (new mxArray_number (id, val)), name (0) { } |
5900
|
1936 |
|
1937 mxArray::mxArray (mxClassID id, mxLogical val) |
6065
|
1938 : rep (new mxArray_number (id, val)), name (0) { } |
5900
|
1939 |
|
1940 mxArray::mxArray (const char *str) |
6065
|
1941 : rep (new mxArray_number (str)), name (0) { } |
5900
|
1942 |
|
1943 mxArray::mxArray (int m, const char **str) |
6065
|
1944 : rep (new mxArray_number (m, str)), name (0) { } |
5900
|
1945 |
|
1946 mxArray::mxArray (mxClassID id, int m, int n, int nzmax, mxComplexity flag) |
6065
|
1947 : rep (new mxArray_sparse (id, m, n, nzmax, flag)), name (0) { } |
5900
|
1948 |
|
1949 mxArray::mxArray (int ndims, const int *dims, int num_keys, const char **keys) |
6065
|
1950 : rep (new mxArray_struct (ndims, dims, num_keys, keys)), name (0) { } |
5900
|
1951 |
|
1952 mxArray::mxArray (const dim_vector& dv, int num_keys, const char **keys) |
6065
|
1953 : rep (new mxArray_struct (dv, num_keys, keys)), name (0) { } |
5900
|
1954 |
|
1955 mxArray::mxArray (int m, int n, int num_keys, const char **keys) |
6065
|
1956 : rep (new mxArray_struct (m, n, num_keys, keys)), name (0) { } |
5900
|
1957 |
|
1958 mxArray::mxArray (int ndims, const int *dims) |
6065
|
1959 : rep (new mxArray_cell (ndims, dims)), name (0) { } |
5900
|
1960 |
|
1961 mxArray::mxArray (const dim_vector& dv) |
6065
|
1962 : rep (new mxArray_cell (dv)), name (0) { } |
5900
|
1963 |
|
1964 mxArray::mxArray (int m, int n) |
6065
|
1965 : rep (new mxArray_cell (m, n)), name (0) { } |
5900
|
1966 |
|
1967 mxArray::~mxArray (void) |
|
1968 { |
|
1969 mxFree (name); |
|
1970 |
|
1971 delete rep; |
|
1972 } |
|
1973 |
|
1974 void |
|
1975 mxArray::set_name (const char *name_arg) |
|
1976 { |
|
1977 mxFree (name); |
|
1978 name = strsave (name_arg); |
|
1979 } |
|
1980 |
5907
|
1981 octave_value |
|
1982 mxArray::as_octave_value (mxArray *ptr) |
|
1983 { |
|
1984 return ptr ? ptr->as_octave_value () : octave_value (Matrix ()); |
|
1985 } |
|
1986 |
|
1987 octave_value |
|
1988 mxArray::as_octave_value (void) const |
|
1989 { |
|
1990 return rep->as_octave_value (); |
|
1991 } |
|
1992 |
5900
|
1993 void |
|
1994 mxArray::maybe_mutate (void) const |
|
1995 { |
|
1996 if (rep->is_octave_value ()) |
|
1997 { |
|
1998 // The mutate function returns a pointer to a complete new |
|
1999 // mxArray object (or 0, if no mutation happened). We just want |
|
2000 // to replace the existing rep with the rep from the new object. |
|
2001 |
|
2002 mxArray *new_val = rep->mutate (); |
|
2003 |
|
2004 if (new_val) |
|
2005 { |
|
2006 delete rep; |
|
2007 rep = new_val->rep; |
|
2008 new_val->rep = 0; |
|
2009 delete new_val; |
|
2010 } |
|
2011 } |
|
2012 } |
|
2013 |
|
2014 // ------------------------------------------------------------------ |
|
2015 |
|
2016 // A clas to manage calls to MEX functions. Mostly deals with memory |
|
2017 // management. |
5864
|
2018 |
|
2019 class mex |
|
2020 { |
|
2021 public: |
|
2022 |
6068
|
2023 mex (octave_mex_function *f) |
|
2024 : curr_mex_fcn (f), memlist (), arraylist (), fname (0) { } |
5864
|
2025 |
|
2026 ~mex (void) |
|
2027 { |
|
2028 if (! memlist.empty ()) |
5905
|
2029 error ("mex: %s: cleanup failed", function_name ()); |
5900
|
2030 |
|
2031 mxFree (fname); |
5864
|
2032 } |
|
2033 |
5900
|
2034 const char *function_name (void) const |
|
2035 { |
|
2036 if (! fname) |
|
2037 { |
|
2038 octave_function *fcn = octave_call_stack::current (); |
|
2039 |
|
2040 if (fcn) |
|
2041 { |
|
2042 std::string nm = fcn->name (); |
6065
|
2043 fname = mxArray::strsave (nm.c_str ()); |
5900
|
2044 } |
|
2045 else |
6065
|
2046 fname = mxArray::strsave ("unknown"); |
5900
|
2047 } |
|
2048 |
|
2049 return fname; |
|
2050 } |
|
2051 |
|
2052 // Free all unmarked pointers obtained from malloc and calloc. |
|
2053 static void cleanup (void *ptr) |
|
2054 { |
|
2055 mex *context = static_cast<mex *> (ptr); |
|
2056 |
5905
|
2057 // We can't use mex::free here because it modifies memlist. |
5900
|
2058 for (std::set<void *>::iterator p = context->memlist.begin (); |
|
2059 p != context->memlist.end (); p++) |
5905
|
2060 { |
|
2061 if (*p) |
|
2062 { |
|
2063 context->unmark (*p); |
|
2064 |
|
2065 xfree (*p); |
|
2066 } |
|
2067 } |
|
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 * |
|
3291 mexGet (double /*handle*/, const char */*property*/) |
5864
|
3292 { |
5900
|
3293 // FIXME |
|
3294 error ("mexGet: not implemented"); |
|
3295 return 0; |
5864
|
3296 } |
|
3297 |
5900
|
3298 int |
|
3299 mexIsGlobal (const mxArray *ptr) |
5864
|
3300 { |
5900
|
3301 return mxIsFromGlobalWS (ptr); |
5864
|
3302 } |
|
3303 |
5900
|
3304 int |
|
3305 mexIsLocked (void) |
5864
|
3306 { |
5900
|
3307 int retval = 0; |
|
3308 |
|
3309 if (mex_context) |
|
3310 { |
|
3311 const char *fname = mexFunctionName (); |
|
3312 |
|
3313 retval = mislocked (fname); |
|
3314 } |
|
3315 |
|
3316 return retval; |
5864
|
3317 } |
|
3318 |
5900
|
3319 std::map<std::string,int> mex_lock_count; |
|
3320 |
|
3321 void |
|
3322 mexLock (void) |
5864
|
3323 { |
5900
|
3324 if (mex_context) |
5864
|
3325 { |
5900
|
3326 const char *fname = mexFunctionName (); |
|
3327 |
|
3328 if (mex_lock_count.find (fname) == mex_lock_count.end ()) |
|
3329 mex_lock_count[fname] = 1; |
|
3330 else |
|
3331 mex_lock_count[fname]++; |
|
3332 |
|
3333 mlock (fname); |
5864
|
3334 } |
|
3335 } |
|
3336 |
5900
|
3337 int |
|
3338 mexSet (double /*handle*/, const char */*property*/, mxArray */*val*/) |
|
3339 { |
|
3340 // FIXME |
|
3341 error ("mexSet: not implemented"); |
|
3342 return 0; |
|
3343 } |
|
3344 |
|
3345 void |
|
3346 mexUnlock (void) |
5864
|
3347 { |
5900
|
3348 if (mex_context) |
5864
|
3349 { |
5900
|
3350 const char *fname = mexFunctionName (); |
|
3351 |
5905
|
3352 std::map<std::string,int>::iterator p = mex_lock_count.find (fname); |
|
3353 |
6062
|
3354 if (p != mex_lock_count.end ()) |
5900
|
3355 { |
|
3356 int count = --mex_lock_count[fname]; |
|
3357 |
|
3358 if (count == 0) |
|
3359 { |
|
3360 munlock (fname); |
|
3361 |
5905
|
3362 mex_lock_count.erase (p); |
5900
|
3363 } |
|
3364 } |
5864
|
3365 } |
|
3366 } |