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