5900
|
1 /* |
|
2 |
|
3 Copyright (C) 2001, 2006 Paul Kienzle |
|
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 2, or (at your option) any |
|
10 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, write to the Free |
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
|
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, |
|
75 mxFUNCTION_CLASS, |
|
76 } |
|
77 mxClassID; |
|
78 |
|
79 typedef int mxLogical; |
|
80 |
|
81 /* typedef Uint16 mxChar; */ |
|
82 typedef unsigned short mxChar; |
|
83 |
|
84 #if ! defined (MXARRAY_TYPEDEFS_ONLY) |
|
85 |
|
86 class octave_value; |
|
87 |
|
88 #define DO_MUTABLE_METHOD(RET_T, METHOD_CALL) \ |
|
89 RET_T retval = rep->METHOD_CALL; \ |
|
90 \ |
|
91 if (rep->mutation_needed ()) \ |
|
92 { \ |
|
93 maybe_mutate (); \ |
|
94 retval = rep->METHOD_CALL; \ |
|
95 } \ |
|
96 \ |
|
97 return retval |
|
98 |
|
99 #define DO_VOID_MUTABLE_METHOD(METHOD_CALL) \ |
|
100 rep->METHOD_CALL; \ |
|
101 \ |
|
102 if (rep->mutation_needed ()) \ |
|
103 { \ |
|
104 maybe_mutate (); \ |
|
105 rep->METHOD_CALL; \ |
|
106 } |
|
107 |
|
108 // This just provides a way to avoid infinite recursion when building |
|
109 // mxArray objects. |
|
110 |
|
111 struct |
|
112 xmxArray |
|
113 { |
|
114 xmxArray (void) { } |
|
115 }; |
|
116 |
|
117 // The main interface class. The representation can be based on an |
|
118 // octave_value object or a separate object that tries to reproduce |
|
119 // the semantics of mxArray objects in Matlab more directly. |
|
120 |
|
121 class mxArray |
|
122 { |
|
123 public: |
|
124 |
|
125 mxArray (const octave_value& ov); |
|
126 |
|
127 mxArray (mxClassID id, int ndims, const int *dims, |
|
128 mxComplexity flag = mxREAL); |
|
129 |
|
130 mxArray (mxClassID id, const dim_vector& dv, mxComplexity flag = mxREAL); |
|
131 |
|
132 mxArray (mxClassID id, int m, int n, mxComplexity flag = mxREAL); |
|
133 |
|
134 mxArray (mxClassID id, double val); |
|
135 |
|
136 mxArray (mxClassID id, mxLogical val); |
|
137 |
|
138 mxArray (const char *str); |
|
139 |
|
140 mxArray (int m, const char **str); |
|
141 |
|
142 mxArray (mxClassID id, int m, int n, int nzmax, mxComplexity flag = mxREAL); |
|
143 |
|
144 mxArray (int ndims, const int *dims, int num_keys, const char **keys); |
|
145 |
|
146 mxArray (const dim_vector& dv, int num_keys, const char **keys); |
|
147 |
|
148 mxArray (int m, int n, int num_keys, const char **keys); |
|
149 |
|
150 mxArray (int ndims, const int *dims); |
|
151 |
|
152 mxArray (const dim_vector& dv); |
|
153 |
|
154 mxArray (int m, int n); |
|
155 |
|
156 virtual mxArray *clone (void) const |
|
157 { |
|
158 mxArray *new_rep = rep->clone (); |
|
159 |
|
160 return new mxArray (new_rep, name, persistent); |
|
161 } |
|
162 |
|
163 virtual ~mxArray (void); |
|
164 |
|
165 void mark_persistent (void) const { persistent = true; } |
|
166 |
|
167 void unmark_persistent (void) const { persistent = false; } |
|
168 |
|
169 bool is_persistent (void) const { return persistent; } |
|
170 |
|
171 virtual bool is_octave_value (void) const { return rep->is_octave_value (); } |
|
172 |
|
173 virtual int is_cell (void) const { return rep->is_cell (); } |
|
174 |
|
175 virtual int is_char (void) const { return rep->is_char (); } |
|
176 |
|
177 virtual int is_class (const char *name_arg) const { return rep->is_class (name_arg); } |
|
178 |
|
179 virtual int is_complex (void) const { return rep->is_complex (); } |
|
180 |
|
181 virtual int is_double (void) const { return rep->is_double (); } |
|
182 |
|
183 virtual int is_int16 (void) const { return rep->is_int16 (); } |
|
184 |
|
185 virtual int is_int32 (void) const { return rep->is_int32 (); } |
|
186 |
|
187 virtual int is_int64 (void) const { return rep->is_int64 (); } |
|
188 |
|
189 virtual int is_int8 (void) const { return rep->is_int8 (); } |
|
190 |
|
191 virtual int is_logical (void) const { return rep->is_logical (); } |
|
192 |
|
193 virtual int is_numeric (void) const { return rep->is_numeric (); } |
|
194 |
|
195 virtual int is_single (void) const { return rep->is_single (); } |
|
196 |
|
197 virtual int is_sparse (void) const { return rep->is_sparse (); } |
|
198 |
|
199 virtual int is_struct (void) const { return rep->is_struct (); } |
|
200 |
|
201 virtual int is_uint16 (void) const { return rep->is_uint16 (); } |
|
202 |
|
203 virtual int is_uint32 (void) const { return rep->is_uint32 (); } |
|
204 |
|
205 virtual int is_uint64 (void) const { return rep->is_uint64 (); } |
|
206 |
|
207 virtual int is_uint8 (void) const { return rep->is_uint8 (); } |
|
208 |
|
209 virtual int is_logical_scalar (void) const { return rep->is_logical_scalar (); } |
|
210 |
|
211 virtual int is_logical_scalar_true (void) const { return rep->is_logical_scalar_true (); } |
|
212 |
|
213 virtual int get_m (void) const { return rep->get_m (); } |
|
214 |
|
215 virtual int get_n (void) const { return rep->get_n (); } |
|
216 |
|
217 virtual int *get_dimensions (void) const { return rep->get_dimensions (); } |
|
218 |
|
219 virtual int get_number_of_dimensions (void) const { return rep->get_number_of_dimensions (); } |
|
220 |
|
221 virtual void set_m (int m) { rep->set_m (m); } |
|
222 |
|
223 virtual void set_n (int n) { rep->set_n (n); } |
|
224 |
|
225 virtual void set_dimensions (int *dims_arg, int ndims_arg) { rep->set_dimensions (dims_arg, ndims_arg); } |
|
226 |
|
227 virtual int get_number_of_elements (void) const { return rep->get_number_of_elements (); } |
|
228 |
|
229 virtual int is_empty (void) const { return get_number_of_elements () == 0; } |
|
230 |
|
231 const char *get_name (void) const { return name; } |
|
232 |
|
233 void set_name (const char *name_arg); |
|
234 |
|
235 virtual mxClassID get_class_id (void) const { return rep->get_class_id (); } |
|
236 |
|
237 virtual const char *get_class_name (void) const { return rep->get_class_name (); } |
|
238 |
|
239 virtual void set_class_name (const char *name_arg) { DO_VOID_MUTABLE_METHOD (set_class_name (name_arg)); } |
|
240 |
|
241 virtual mxArray *get_cell (int idx) const { DO_MUTABLE_METHOD (mxArray *, get_cell (idx)); } |
|
242 |
|
243 virtual void set_cell (int idx, mxArray *val) { DO_VOID_MUTABLE_METHOD (set_cell (idx, val)); } |
|
244 |
|
245 virtual void *get_data (void) const { DO_MUTABLE_METHOD (void *, get_data ()); } |
|
246 |
|
247 virtual void *get_imag_data (void) const { DO_MUTABLE_METHOD (void *, get_imag_data ()); } |
|
248 |
|
249 virtual void set_data (void *pr) { DO_VOID_MUTABLE_METHOD (set_data (pr)); } |
|
250 |
|
251 virtual void set_imag_data (void *pi) { DO_VOID_MUTABLE_METHOD (set_imag_data (pi)); } |
|
252 |
|
253 virtual int *get_ir (void) const { DO_MUTABLE_METHOD (int *, get_ir ()); } |
|
254 |
|
255 virtual int *get_jc (void) const { DO_MUTABLE_METHOD (int *, get_jc ()); } |
|
256 |
|
257 virtual int get_nzmax (void) const { return rep->get_nzmax (); } |
|
258 |
|
259 virtual void set_ir (int *ir) { DO_VOID_MUTABLE_METHOD (set_ir (ir)); } |
|
260 |
|
261 virtual void set_jc (int *jc) { DO_VOID_MUTABLE_METHOD (set_jc (jc)); } |
|
262 |
|
263 virtual void set_nzmax (int nzmax) { DO_VOID_MUTABLE_METHOD (set_nzmax (nzmax)); } |
|
264 |
|
265 virtual int add_field (const char *key) { DO_MUTABLE_METHOD (int, add_field (key)); } |
|
266 |
|
267 virtual void remove_field (int key_num) { DO_VOID_MUTABLE_METHOD (remove_field (key_num)); } |
|
268 |
|
269 virtual mxArray *get_field_by_number (int index, int key_num) const { DO_MUTABLE_METHOD (mxArray *, get_field_by_number (index, key_num)); } |
|
270 |
|
271 virtual void set_field_by_number (int index, int key_num, mxArray *val) { DO_VOID_MUTABLE_METHOD (set_field_by_number (index, key_num, val)); } |
|
272 |
|
273 virtual int get_number_of_fields (void) const { return rep->get_number_of_fields (); } |
|
274 |
|
275 virtual const char *get_field_name_by_number (int key_num) const { DO_MUTABLE_METHOD (const char*, get_field_name_by_number (key_num)); } |
|
276 |
|
277 virtual int get_field_number (const char *key) const { DO_MUTABLE_METHOD (int, get_field_number (key)); } |
|
278 |
|
279 virtual int get_string (char *buf, int buflen) const { return rep->get_string (buf, buflen); } |
|
280 |
|
281 virtual char *array_to_string (void) const { return rep->array_to_string (); } |
|
282 |
|
283 virtual int calc_single_subscript (int nsubs, int *subs) const { return rep->calc_single_subscript (nsubs, subs); } |
|
284 |
|
285 virtual int get_element_size (void) const { return rep->get_element_size (); } |
|
286 |
|
287 virtual bool mutation_needed (void) const { return rep->mutation_needed (); } |
|
288 |
|
289 virtual mxArray *mutate (void) const { return rep->mutate (); } |
|
290 |
|
291 static void *malloc (size_t n); |
|
292 |
|
293 static void *calloc (size_t n, size_t t); |
|
294 |
|
295 static char *strsave (const char *str) |
|
296 { |
|
297 char *retval = 0; |
|
298 |
|
299 if (str) |
|
300 { |
|
301 int sz = sizeof (mxChar) * (strlen (str) + 1); |
|
302 retval = static_cast<char *> (mxArray::malloc (sz)); |
|
303 strcpy (retval, str); |
|
304 } |
|
305 |
|
306 return retval; |
|
307 } |
|
308 |
5907
|
309 static octave_value as_octave_value (mxArray *ptr); |
|
310 |
5900
|
311 protected: |
|
312 |
5907
|
313 virtual octave_value as_octave_value (void) const; |
|
314 |
5900
|
315 mxArray (const xmxArray&) : rep (0), name (0), persistent (false) { } |
|
316 |
|
317 private: |
|
318 |
|
319 mutable mxArray *rep; |
|
320 |
|
321 char *name; |
|
322 |
|
323 mutable bool persistent; |
|
324 |
|
325 mxArray (mxArray *r, const char *n, bool p) |
|
326 : rep (r), name (strsave (n)), persistent (p) { } |
|
327 |
|
328 void maybe_mutate (void) const; |
|
329 |
|
330 // No copying! |
|
331 |
|
332 mxArray (const mxArray&); |
|
333 |
|
334 mxArray& operator = (const mxArray&); |
|
335 }; |
|
336 |
|
337 #undef DO_MUTABLE_METHOD |
|
338 #undef DO_VOID_MUTABLE_METHOD |
|
339 |
|
340 #endif |
|
341 #endif |