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