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