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