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