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