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