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