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