Mercurial > hg > octave-nkf
annotate libinterp/interp-core/mxarray.in.h @ 15240:a23693fa5ba9
Add mwSignedIndex type for better mex compatibility with Matlab (bug #37133)
mxarray.in.h: Add mwSignedIndex type which is a typedef for OCTAVE_IDX_TYPE.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 26 Aug 2012 21:41:28 -0700 |
parents | 2fc554ffbc28 |
children | a291f850743c |
rev | line source |
---|---|
15100
8523df595d42
maint: Add DO NOT EDIT messages to build system for src/*.in.h.
Rik <rik@octave.org>
parents:
15096
diff
changeset
|
1 // %NO_EDIT_WARNING% |
6686 | 2 /* |
3 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
4 Copyright (C) 2001-2012 Paul Kienzle |
6686 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
6686 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
6686 | 21 |
22 */ | |
23 | |
24 /* | |
25 | |
26 Part of this code was originally distributed as part of Octave Forge under | |
27 the following terms: | |
28 | |
29 Author: Paul Kienzle | |
30 I grant this code to the public domain. | |
31 2001-03-22 | |
32 | |
33 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
34 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
35 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
36 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
37 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
38 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
39 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
40 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
41 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
42 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
43 SUCH DAMAGE. | |
44 | |
45 */ | |
46 | |
47 #if ! defined (MXARRAY_H) | |
48 #define MXARRAY_H | |
49 | |
50 typedef enum | |
51 { | |
52 mxREAL = 0, | |
53 mxCOMPLEX = 1 | |
54 } | |
55 mxComplexity; | |
56 | |
57 typedef enum | |
58 { | |
59 mxUNKNOWN_CLASS = 0, | |
60 mxCELL_CLASS, | |
61 mxSTRUCT_CLASS, | |
62 mxLOGICAL_CLASS, | |
63 mxCHAR_CLASS, | |
64 mxUNUSED_CLASS, | |
65 mxDOUBLE_CLASS, | |
66 mxSINGLE_CLASS, | |
67 mxINT8_CLASS, | |
68 mxUINT8_CLASS, | |
69 mxINT16_CLASS, | |
70 mxUINT16_CLASS, | |
71 mxINT32_CLASS, | |
72 mxUINT32_CLASS, | |
73 mxINT64_CLASS, | |
74 mxUINT64_CLASS, | |
7056 | 75 mxFUNCTION_CLASS |
6686 | 76 } |
77 mxClassID; | |
78 | |
7574
9669b69e34ef
define mxLogical as unsigned char instead of int
John W. Eaton <jwe@octave.org>
parents:
7417
diff
changeset
|
79 typedef unsigned char mxLogical; |
6686 | 80 |
81 /* typedef Uint16 mxChar; */ | |
82 typedef char mxChar; | |
83 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
84 /* |
6686 | 85 * FIXME? Mathworks says these should be size_t on 64-bit system and when |
86 * mex is used with the -largearraydims flag, but why do that? Its better | |
87 * to conform to the same indexing as the rest of Octave | |
88 */ | |
9906 | 89 typedef %OCTAVE_IDX_TYPE% mwSize; |
90 typedef %OCTAVE_IDX_TYPE% mwIndex; | |
15240
a23693fa5ba9
Add mwSignedIndex type for better mex compatibility with Matlab (bug #37133)
Rik <rik@octave.org>
parents:
15195
diff
changeset
|
91 typedef %OCTAVE_IDX_TYPE% mwSignedIndex; |
6686 | 92 |
93 #if ! defined (MXARRAY_TYPEDEFS_ONLY) | |
94 | |
7417 | 95 #include <cstring> |
96 | |
6686 | 97 class octave_value; |
98 | |
99 #define DO_MUTABLE_METHOD(RET_T, METHOD_CALL) \ | |
100 RET_T retval = rep->METHOD_CALL; \ | |
101 \ | |
102 if (rep->mutation_needed ()) \ | |
103 { \ | |
104 maybe_mutate (); \ | |
105 retval = rep->METHOD_CALL; \ | |
106 } \ | |
107 \ | |
108 return retval | |
109 | |
110 #define DO_VOID_MUTABLE_METHOD(METHOD_CALL) \ | |
111 rep->METHOD_CALL; \ | |
112 \ | |
113 if (rep->mutation_needed ()) \ | |
114 { \ | |
115 maybe_mutate (); \ | |
116 rep->METHOD_CALL; \ | |
117 } | |
118 | |
119 // This just provides a way to avoid infinite recursion when building | |
120 // mxArray objects. | |
121 | |
122 struct | |
123 xmxArray | |
124 { | |
125 xmxArray (void) { } | |
126 }; | |
127 | |
128 // The main interface class. The representation can be based on an | |
129 // octave_value object or a separate object that tries to reproduce | |
130 // the semantics of mxArray objects in Matlab more directly. | |
131 | |
132 class mxArray | |
133 { | |
134 public: | |
135 | |
136 mxArray (const octave_value& ov); | |
137 | |
138 mxArray (mxClassID id, mwSize ndims, const mwSize *dims, | |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
9906
diff
changeset
|
139 mxComplexity flag = mxREAL); |
6686 | 140 |
141 mxArray (mxClassID id, const dim_vector& dv, mxComplexity flag = mxREAL); | |
142 | |
143 mxArray (mxClassID id, mwSize m, mwSize n, mxComplexity flag = mxREAL); | |
144 | |
145 mxArray (mxClassID id, double val); | |
146 | |
147 mxArray (mxClassID id, mxLogical val); | |
148 | |
149 mxArray (const char *str); | |
150 | |
6806 | 151 mxArray (mwSize m, const char **str); |
6686 | 152 |
6806 | 153 mxArray (mxClassID id, mwSize m, mwSize n, mwSize nzmax, |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
9906
diff
changeset
|
154 mxComplexity flag = mxREAL); |
6686 | 155 |
156 mxArray (mwSize ndims, const mwSize *dims, int num_keys, const char **keys); | |
157 | |
158 mxArray (const dim_vector& dv, int num_keys, const char **keys); | |
159 | |
160 mxArray (mwSize m, mwSize n, int num_keys, const char **keys); | |
161 | |
162 mxArray (mwSize ndims, const mwSize *dims); | |
163 | |
164 mxArray (const dim_vector& dv); | |
165 | |
166 mxArray (mwSize m, mwSize n); | |
167 | |
11277
7d5351fc575a
mex.cc: convert mxArray_octave_value to matlab style representation when duplicating
John W. Eaton <jwe@octave.org>
parents:
11100
diff
changeset
|
168 virtual mxArray *dup (void) const |
6686 | 169 { |
11277
7d5351fc575a
mex.cc: convert mxArray_octave_value to matlab style representation when duplicating
John W. Eaton <jwe@octave.org>
parents:
11100
diff
changeset
|
170 mxArray *new_rep = rep->dup (); |
6686 | 171 |
172 return new mxArray (new_rep, name); | |
173 } | |
174 | |
175 virtual ~mxArray (void); | |
176 | |
177 virtual bool is_octave_value (void) const { return rep->is_octave_value (); } | |
178 | |
179 virtual int is_cell (void) const { return rep->is_cell (); } | |
180 | |
181 virtual int is_char (void) const { return rep->is_char (); } | |
182 | |
183 virtual int is_class (const char *name_arg) const { return rep->is_class (name_arg); } | |
184 | |
185 virtual int is_complex (void) const { return rep->is_complex (); } | |
186 | |
187 virtual int is_double (void) const { return rep->is_double (); } | |
188 | |
11100
cdf940db26a0
provide mxIsFunctionHandle MEX interface function
John W. Eaton <jwe@octave.org>
parents:
10317
diff
changeset
|
189 virtual int is_function_handle (void) const { return rep->is_function_handle (); } |
cdf940db26a0
provide mxIsFunctionHandle MEX interface function
John W. Eaton <jwe@octave.org>
parents:
10317
diff
changeset
|
190 |
6686 | 191 virtual int is_int16 (void) const { return rep->is_int16 (); } |
192 | |
193 virtual int is_int32 (void) const { return rep->is_int32 (); } | |
194 | |
195 virtual int is_int64 (void) const { return rep->is_int64 (); } | |
196 | |
197 virtual int is_int8 (void) const { return rep->is_int8 (); } | |
198 | |
199 virtual int is_logical (void) const { return rep->is_logical (); } | |
200 | |
201 virtual int is_numeric (void) const { return rep->is_numeric (); } | |
202 | |
203 virtual int is_single (void) const { return rep->is_single (); } | |
204 | |
205 virtual int is_sparse (void) const { return rep->is_sparse (); } | |
206 | |
207 virtual int is_struct (void) const { return rep->is_struct (); } | |
208 | |
209 virtual int is_uint16 (void) const { return rep->is_uint16 (); } | |
210 | |
211 virtual int is_uint32 (void) const { return rep->is_uint32 (); } | |
212 | |
213 virtual int is_uint64 (void) const { return rep->is_uint64 (); } | |
214 | |
215 virtual int is_uint8 (void) const { return rep->is_uint8 (); } | |
216 | |
217 virtual int is_logical_scalar (void) const { return rep->is_logical_scalar (); } | |
218 | |
219 virtual int is_logical_scalar_true (void) const { return rep->is_logical_scalar_true (); } | |
220 | |
221 virtual mwSize get_m (void) const { return rep->get_m (); } | |
222 | |
223 virtual mwSize get_n (void) const { return rep->get_n (); } | |
224 | |
225 virtual mwSize *get_dimensions (void) const { return rep->get_dimensions (); } | |
226 | |
227 virtual mwSize get_number_of_dimensions (void) const { return rep->get_number_of_dimensions (); } | |
228 | |
229 virtual void set_m (mwSize m) { DO_VOID_MUTABLE_METHOD (set_m (m)); } | |
230 | |
231 virtual void set_n (mwSize n) { DO_VOID_MUTABLE_METHOD (set_n (n)); } | |
232 | |
233 virtual void set_dimensions (mwSize *dims_arg, mwSize ndims_arg) { DO_VOID_MUTABLE_METHOD (set_dimensions (dims_arg, ndims_arg)); } | |
234 | |
235 virtual mwSize get_number_of_elements (void) const { return rep->get_number_of_elements (); } | |
236 | |
237 virtual int is_empty (void) const { return get_number_of_elements () == 0; } | |
238 | |
239 const char *get_name (void) const { return name; } | |
240 | |
241 void set_name (const char *name_arg); | |
242 | |
243 virtual mxClassID get_class_id (void) const { return rep->get_class_id (); } | |
244 | |
245 virtual const char *get_class_name (void) const { return rep->get_class_name (); } | |
246 | |
247 virtual void set_class_name (const char *name_arg) { DO_VOID_MUTABLE_METHOD (set_class_name (name_arg)); } | |
248 | |
249 virtual mxArray *get_cell (mwIndex idx) const { DO_MUTABLE_METHOD (mxArray *, get_cell (idx)); } | |
250 | |
251 virtual void set_cell (mwIndex idx, mxArray *val) { DO_VOID_MUTABLE_METHOD (set_cell (idx, val)); } | |
252 | |
253 virtual double get_scalar (void) const { return rep->get_scalar (); } | |
254 | |
255 virtual void *get_data (void) const { DO_MUTABLE_METHOD (void *, get_data ()); } | |
256 | |
257 virtual void *get_imag_data (void) const { DO_MUTABLE_METHOD (void *, get_imag_data ()); } | |
258 | |
259 virtual void set_data (void *pr) { DO_VOID_MUTABLE_METHOD (set_data (pr)); } | |
260 | |
261 virtual void set_imag_data (void *pi) { DO_VOID_MUTABLE_METHOD (set_imag_data (pi)); } | |
262 | |
263 virtual mwIndex *get_ir (void) const { DO_MUTABLE_METHOD (mwIndex *, get_ir ()); } | |
264 | |
265 virtual mwIndex *get_jc (void) const { DO_MUTABLE_METHOD (mwIndex *, get_jc ()); } | |
266 | |
267 virtual mwSize get_nzmax (void) const { return rep->get_nzmax (); } | |
268 | |
269 virtual void set_ir (mwIndex *ir) { DO_VOID_MUTABLE_METHOD (set_ir (ir)); } | |
270 | |
271 virtual void set_jc (mwIndex *jc) { DO_VOID_MUTABLE_METHOD (set_jc (jc)); } | |
272 | |
273 virtual void set_nzmax (mwSize nzmax) { DO_VOID_MUTABLE_METHOD (set_nzmax (nzmax)); } | |
274 | |
275 virtual int add_field (const char *key) { DO_MUTABLE_METHOD (int, add_field (key)); } | |
276 | |
277 virtual void remove_field (int key_num) { DO_VOID_MUTABLE_METHOD (remove_field (key_num)); } | |
278 | |
279 virtual mxArray *get_field_by_number (mwIndex index, int key_num) const { DO_MUTABLE_METHOD (mxArray *, get_field_by_number (index, key_num)); } | |
280 | |
281 virtual void set_field_by_number (mwIndex index, int key_num, mxArray *val) { DO_VOID_MUTABLE_METHOD (set_field_by_number (index, key_num, val)); } | |
282 | |
283 virtual int get_number_of_fields (void) const { return rep->get_number_of_fields (); } | |
284 | |
285 virtual const char *get_field_name_by_number (int key_num) const { DO_MUTABLE_METHOD (const char*, get_field_name_by_number (key_num)); } | |
286 | |
287 virtual int get_field_number (const char *key) const { DO_MUTABLE_METHOD (int, get_field_number (key)); } | |
288 | |
6806 | 289 virtual int get_string (char *buf, mwSize buflen) const { return rep->get_string (buf, buflen); } |
6686 | 290 |
291 virtual char *array_to_string (void) const { return rep->array_to_string (); } | |
292 | |
293 virtual mwIndex calc_single_subscript (mwSize nsubs, mwIndex *subs) const { return rep->calc_single_subscript (nsubs, subs); } | |
294 | |
295 virtual size_t get_element_size (void) const { return rep->get_element_size (); } | |
296 | |
297 virtual bool mutation_needed (void) const { return rep->mutation_needed (); } | |
298 | |
299 virtual mxArray *mutate (void) const { return rep->mutate (); } | |
300 | |
301 static void *malloc (size_t n); | |
302 | |
303 static void *calloc (size_t n, size_t t); | |
304 | |
305 static char *strsave (const char *str) | |
306 { | |
307 char *retval = 0; | |
308 | |
309 if (str) | |
310 { | |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
9906
diff
changeset
|
311 mwSize sz = sizeof (mxChar) * (strlen (str) + 1); |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
9906
diff
changeset
|
312 retval = static_cast<char *> (mxArray::malloc (sz)); |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
9906
diff
changeset
|
313 strcpy (retval, str); |
6686 | 314 } |
315 | |
316 return retval; | |
317 } | |
318 | |
319 static octave_value as_octave_value (mxArray *ptr); | |
320 | |
321 protected: | |
322 | |
323 virtual octave_value as_octave_value (void) const; | |
324 | |
325 mxArray (const xmxArray&) : rep (0), name (0) { } | |
326 | |
327 private: | |
328 | |
329 mutable mxArray *rep; | |
330 | |
331 char *name; | |
332 | |
333 mxArray (mxArray *r, const char *n) | |
334 : rep (r), name (strsave (n)) { } | |
335 | |
336 void maybe_mutate (void) const; | |
337 | |
338 // No copying! | |
339 | |
340 mxArray (const mxArray&); | |
341 | |
342 mxArray& operator = (const mxArray&); | |
343 }; | |
344 | |
345 #undef DO_MUTABLE_METHOD | |
346 #undef DO_VOID_MUTABLE_METHOD | |
347 | |
348 #endif | |
349 #endif |