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