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