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