Mercurial > octave-nkf
comparison src/mxarray.h @ 5900:c20eb7330d13
[project @ 2006-07-22 08:31:16 by jwe]
author | jwe |
---|---|
date | Sat, 22 Jul 2006 08:31:17 +0000 |
parents | |
children | 288c341438f9 |
comparison
equal
deleted
inserted
replaced
5899:82c38ce145a7 | 5900:c20eb7330d13 |
---|---|
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 virtual octave_value as_octave_value (void) const; | |
166 | |
167 void mark_persistent (void) const { persistent = true; } | |
168 | |
169 void unmark_persistent (void) const { persistent = false; } | |
170 | |
171 bool is_persistent (void) const { return persistent; } | |
172 | |
173 virtual bool is_octave_value (void) const { return rep->is_octave_value (); } | |
174 | |
175 virtual int is_cell (void) const { return rep->is_cell (); } | |
176 | |
177 virtual int is_char (void) const { return rep->is_char (); } | |
178 | |
179 virtual int is_class (const char *name_arg) const { return rep->is_class (name_arg); } | |
180 | |
181 virtual int is_complex (void) const { return rep->is_complex (); } | |
182 | |
183 virtual int is_double (void) const { return rep->is_double (); } | |
184 | |
185 virtual int is_int16 (void) const { return rep->is_int16 (); } | |
186 | |
187 virtual int is_int32 (void) const { return rep->is_int32 (); } | |
188 | |
189 virtual int is_int64 (void) const { return rep->is_int64 (); } | |
190 | |
191 virtual int is_int8 (void) const { return rep->is_int8 (); } | |
192 | |
193 virtual int is_logical (void) const { return rep->is_logical (); } | |
194 | |
195 virtual int is_numeric (void) const { return rep->is_numeric (); } | |
196 | |
197 virtual int is_single (void) const { return rep->is_single (); } | |
198 | |
199 virtual int is_sparse (void) const { return rep->is_sparse (); } | |
200 | |
201 virtual int is_struct (void) const { return rep->is_struct (); } | |
202 | |
203 virtual int is_uint16 (void) const { return rep->is_uint16 (); } | |
204 | |
205 virtual int is_uint32 (void) const { return rep->is_uint32 (); } | |
206 | |
207 virtual int is_uint64 (void) const { return rep->is_uint64 (); } | |
208 | |
209 virtual int is_uint8 (void) const { return rep->is_uint8 (); } | |
210 | |
211 virtual int is_logical_scalar (void) const { return rep->is_logical_scalar (); } | |
212 | |
213 virtual int is_logical_scalar_true (void) const { return rep->is_logical_scalar_true (); } | |
214 | |
215 virtual int get_m (void) const { return rep->get_m (); } | |
216 | |
217 virtual int get_n (void) const { return rep->get_n (); } | |
218 | |
219 virtual int *get_dimensions (void) const { return rep->get_dimensions (); } | |
220 | |
221 virtual int get_number_of_dimensions (void) const { return rep->get_number_of_dimensions (); } | |
222 | |
223 virtual void set_m (int m) { rep->set_m (m); } | |
224 | |
225 virtual void set_n (int n) { rep->set_n (n); } | |
226 | |
227 virtual void set_dimensions (int *dims_arg, int ndims_arg) { rep->set_dimensions (dims_arg, ndims_arg); } | |
228 | |
229 virtual int get_number_of_elements (void) const { return rep->get_number_of_elements (); } | |
230 | |
231 virtual int is_empty (void) const { return get_number_of_elements () == 0; } | |
232 | |
233 const char *get_name (void) const { return name; } | |
234 | |
235 void set_name (const char *name_arg); | |
236 | |
237 virtual mxClassID get_class_id (void) const { return rep->get_class_id (); } | |
238 | |
239 virtual const char *get_class_name (void) const { return rep->get_class_name (); } | |
240 | |
241 virtual void set_class_name (const char *name_arg) { DO_VOID_MUTABLE_METHOD (set_class_name (name_arg)); } | |
242 | |
243 virtual mxArray *get_cell (int idx) const { DO_MUTABLE_METHOD (mxArray *, get_cell (idx)); } | |
244 | |
245 virtual void set_cell (int idx, mxArray *val) { DO_VOID_MUTABLE_METHOD (set_cell (idx, val)); } | |
246 | |
247 virtual void *get_data (void) const { DO_MUTABLE_METHOD (void *, get_data ()); } | |
248 | |
249 virtual void *get_imag_data (void) const { DO_MUTABLE_METHOD (void *, get_imag_data ()); } | |
250 | |
251 virtual void set_data (void *pr) { DO_VOID_MUTABLE_METHOD (set_data (pr)); } | |
252 | |
253 virtual void set_imag_data (void *pi) { DO_VOID_MUTABLE_METHOD (set_imag_data (pi)); } | |
254 | |
255 virtual int *get_ir (void) const { DO_MUTABLE_METHOD (int *, get_ir ()); } | |
256 | |
257 virtual int *get_jc (void) const { DO_MUTABLE_METHOD (int *, get_jc ()); } | |
258 | |
259 virtual int get_nzmax (void) const { return rep->get_nzmax (); } | |
260 | |
261 virtual void set_ir (int *ir) { DO_VOID_MUTABLE_METHOD (set_ir (ir)); } | |
262 | |
263 virtual void set_jc (int *jc) { DO_VOID_MUTABLE_METHOD (set_jc (jc)); } | |
264 | |
265 virtual void set_nzmax (int nzmax) { DO_VOID_MUTABLE_METHOD (set_nzmax (nzmax)); } | |
266 | |
267 virtual int add_field (const char *key) { DO_MUTABLE_METHOD (int, add_field (key)); } | |
268 | |
269 virtual void remove_field (int key_num) { DO_VOID_MUTABLE_METHOD (remove_field (key_num)); } | |
270 | |
271 virtual mxArray *get_field_by_number (int index, int key_num) const { DO_MUTABLE_METHOD (mxArray *, get_field_by_number (index, key_num)); } | |
272 | |
273 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)); } | |
274 | |
275 virtual int get_number_of_fields (void) const { return rep->get_number_of_fields (); } | |
276 | |
277 virtual const char *get_field_name_by_number (int key_num) const { DO_MUTABLE_METHOD (const char*, get_field_name_by_number (key_num)); } | |
278 | |
279 virtual int get_field_number (const char *key) const { DO_MUTABLE_METHOD (int, get_field_number (key)); } | |
280 | |
281 virtual int get_string (char *buf, int buflen) const { return rep->get_string (buf, buflen); } | |
282 | |
283 virtual char *array_to_string (void) const { return rep->array_to_string (); } | |
284 | |
285 virtual int calc_single_subscript (int nsubs, int *subs) const { return rep->calc_single_subscript (nsubs, subs); } | |
286 | |
287 virtual int get_element_size (void) const { return rep->get_element_size (); } | |
288 | |
289 virtual bool mutation_needed (void) const { return rep->mutation_needed (); } | |
290 | |
291 virtual mxArray *mutate (void) const { return rep->mutate (); } | |
292 | |
293 static void *malloc (size_t n); | |
294 | |
295 static void *calloc (size_t n, size_t t); | |
296 | |
297 static char *strsave (const char *str) | |
298 { | |
299 char *retval = 0; | |
300 | |
301 if (str) | |
302 { | |
303 int sz = sizeof (mxChar) * (strlen (str) + 1); | |
304 retval = static_cast<char *> (mxArray::malloc (sz)); | |
305 strcpy (retval, str); | |
306 } | |
307 | |
308 return retval; | |
309 } | |
310 | |
311 protected: | |
312 | |
313 mxArray (const xmxArray&) : rep (0), name (0), persistent (false) { } | |
314 | |
315 private: | |
316 | |
317 mutable mxArray *rep; | |
318 | |
319 char *name; | |
320 | |
321 mutable bool persistent; | |
322 | |
323 mxArray (mxArray *r, const char *n, bool p) | |
324 : rep (r), name (strsave (n)), persistent (p) { } | |
325 | |
326 void maybe_mutate (void) const; | |
327 | |
328 // No copying! | |
329 | |
330 mxArray (const mxArray&); | |
331 | |
332 mxArray& operator = (const mxArray&); | |
333 }; | |
334 | |
335 #undef DO_MUTABLE_METHOD | |
336 #undef DO_VOID_MUTABLE_METHOD | |
337 | |
338 #endif | |
339 #endif |