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