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