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