GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
mxarray.in.h
Go to the documentation of this file.
1 // %NO_EDIT_WARNING%
2 /*
3 
4 Copyright (C) 2001-2013 Paul Kienzle
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
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
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
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
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,
54 }
56 
57 typedef enum
58 {
76 }
77 mxClassID;
78 
79 typedef unsigned char mxLogical;
80 
81 /* typedef Uint16 mxChar; */
82 typedef char mxChar;
83 
84 /*
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  */
89 typedef %OCTAVE_IDX_TYPE% mwSize;
90 typedef %OCTAVE_IDX_TYPE% mwIndex;
91 typedef %OCTAVE_IDX_TYPE% mwSignedIndex;
92 
93 #if ! defined (MXARRAY_TYPEDEFS_ONLY)
94 
95 #include <cstring>
96 
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 // A class to provide the default implemenation of some of the virtual
120 // functions declared in the mxArray class.
121 
122 class mxArray;
123 
124 class mxArray_base
125 {
126 protected:
127 
128  mxArray_base (void) { }
129 
130 public:
131 
132  virtual mxArray_base *dup (void) const = 0;
133 
134  virtual mxArray *as_mxArray (void) const { return 0; }
135 
136  virtual ~mxArray_base (void) { }
137 
138  virtual bool is_octave_value (void) const { return false; }
139 
140  virtual int is_cell (void) const = 0;
141 
142  virtual int is_char (void) const = 0;
143 
144  virtual int is_class (const char *name_arg) const
145  {
146  int retval = 0;
147 
148  const char *cname = get_class_name ();
149 
150  if (cname && name_arg)
151  retval = ! strcmp (cname, name_arg);
152 
153  return retval;
154  }
155 
156  virtual int is_complex (void) const = 0;
157 
158  virtual int is_double (void) const = 0;
159 
160  virtual int is_function_handle (void) const = 0;
161 
162  virtual int is_int16 (void) const = 0;
163 
164  virtual int is_int32 (void) const = 0;
165 
166  virtual int is_int64 (void) const = 0;
167 
168  virtual int is_int8 (void) const = 0;
169 
170  virtual int is_logical (void) const = 0;
171 
172  virtual int is_numeric (void) const = 0;
173 
174  virtual int is_single (void) const = 0;
175 
176  virtual int is_sparse (void) const = 0;
177 
178  virtual int is_struct (void) const = 0;
179 
180  virtual int is_uint16 (void) const = 0;
181 
182  virtual int is_uint32 (void) const = 0;
183 
184  virtual int is_uint64 (void) const = 0;
185 
186  virtual int is_uint8 (void) const = 0;
187 
188  virtual int is_logical_scalar (void) const
189  {
190  return is_logical () && get_number_of_elements () == 1;
191  }
192 
193  virtual int is_logical_scalar_true (void) const = 0;
194 
195  virtual mwSize get_m (void) const = 0;
196 
197  virtual mwSize get_n (void) const = 0;
198 
199  virtual mwSize *get_dimensions (void) const = 0;
200 
201  virtual mwSize get_number_of_dimensions (void) const = 0;
202 
203  virtual void set_m (mwSize m) = 0;
204 
205  virtual void set_n (mwSize n) = 0;
206 
207  virtual void set_dimensions (mwSize *dims_arg, mwSize ndims_arg) = 0;
208 
209  virtual mwSize get_number_of_elements (void) const = 0;
210 
211  virtual int is_empty (void) const = 0;
212 
213  virtual mxClassID get_class_id (void) const = 0;
214 
215  virtual const char *get_class_name (void) const = 0;
216 
217  virtual void set_class_name (const char *name_arg) = 0;
218 
219  virtual mxArray *get_cell (mwIndex /*idx*/) const
220  {
222  return 0;
223  }
224 
225  virtual void set_cell (mwIndex idx, mxArray *val) = 0;
226 
227  virtual double get_scalar (void) const = 0;
228 
229  virtual void *get_data (void) const = 0;
230 
231  virtual void *get_imag_data (void) const = 0;
232 
233  virtual void set_data (void *pr) = 0;
234 
235  virtual void set_imag_data (void *pi) = 0;
236 
237  virtual mwIndex *get_ir (void) const = 0;
238 
239  virtual mwIndex *get_jc (void) const = 0;
240 
241  virtual mwSize get_nzmax (void) const = 0;
242 
243  virtual void set_ir (mwIndex *ir) = 0;
244 
245  virtual void set_jc (mwIndex *jc) = 0;
246 
247  virtual void set_nzmax (mwSize nzmax) = 0;
248 
249  virtual int add_field (const char *key) = 0;
250 
251  virtual void remove_field (int key_num) = 0;
252 
253  virtual mxArray *get_field_by_number (mwIndex index, int key_num) const = 0;
254 
255  virtual void
256  set_field_by_number (mwIndex index, int key_num, mxArray *val) = 0;
257 
258  virtual int get_number_of_fields (void) const = 0;
259 
260  virtual const char *get_field_name_by_number (int key_num) const = 0;
261 
262  virtual int get_field_number (const char *key) const = 0;
263 
264  virtual int get_string (char *buf, mwSize buflen) const = 0;
265 
266  virtual char *array_to_string (void) const = 0;
267 
268  virtual mwIndex calc_single_subscript (mwSize nsubs, mwIndex *subs) const = 0;
269 
270  virtual size_t get_element_size (void) const = 0;
271 
272  virtual bool mutation_needed (void) const { return false; }
273 
274  virtual mxArray *mutate (void) const { return 0; }
275 
276  virtual octave_value as_octave_value (void) const = 0;
277 
278 protected:
279 
281 
282  void invalid_type_error (void) const
283  {
284  error ("invalid type for operation");
285  }
286 
287  void error (const char *msg) const;
288 };
289 
290 // The main interface class. The representation can be based on an
291 // octave_value object or a separate object that tries to reproduce
292 // the semantics of mxArray objects in Matlab more directly.
293 
294 class mxArray
295 {
296 public:
297 
298  mxArray (const octave_value& ov);
299 
300  mxArray (mxClassID id, mwSize ndims, const mwSize *dims,
301  mxComplexity flag = mxREAL);
302 
303  mxArray (mxClassID id, const dim_vector& dv, mxComplexity flag = mxREAL);
304 
305  mxArray (mxClassID id, mwSize m, mwSize n, mxComplexity flag = mxREAL);
306 
307  mxArray (mxClassID id, double val);
308 
309  mxArray (mxClassID id, mxLogical val);
310 
311  mxArray (const char *str);
312 
313  mxArray (mwSize m, const char **str);
314 
315  mxArray (mxClassID id, mwSize m, mwSize n, mwSize nzmax,
316  mxComplexity flag = mxREAL);
317 
318  mxArray (mwSize ndims, const mwSize *dims, int num_keys, const char **keys);
319 
320  mxArray (const dim_vector& dv, int num_keys, const char **keys);
321 
322  mxArray (mwSize m, mwSize n, int num_keys, const char **keys);
323 
324  mxArray (mwSize ndims, const mwSize *dims);
325 
326  mxArray (const dim_vector& dv);
327 
328  mxArray (mwSize m, mwSize n);
329 
330  mxArray *dup (void) const
331  {
332  mxArray *retval = rep->as_mxArray ();
333 
334  if (retval)
335  retval->set_name (name);
336  else
337  {
338  mxArray_base *new_rep = rep->dup ();
339 
340  retval = new mxArray (new_rep, name);
341  }
342 
343  return retval;
344  }
345 
346  ~mxArray (void);
347 
348  bool is_octave_value (void) const { return rep->is_octave_value (); }
349 
350  int is_cell (void) const { return rep->is_cell (); }
351 
352  int is_char (void) const { return rep->is_char (); }
353 
354  int is_class (const char *name_arg) const { return rep->is_class (name_arg); }
355 
356  int is_complex (void) const { return rep->is_complex (); }
357 
358  int is_double (void) const { return rep->is_double (); }
359 
360  int is_function_handle (void) const { return rep->is_function_handle (); }
361 
362  int is_int16 (void) const { return rep->is_int16 (); }
363 
364  int is_int32 (void) const { return rep->is_int32 (); }
365 
366  int is_int64 (void) const { return rep->is_int64 (); }
367 
368  int is_int8 (void) const { return rep->is_int8 (); }
369 
370  int is_logical (void) const { return rep->is_logical (); }
371 
372  int is_numeric (void) const { return rep->is_numeric (); }
373 
374  int is_single (void) const { return rep->is_single (); }
375 
376  int is_sparse (void) const { return rep->is_sparse (); }
377 
378  int is_struct (void) const { return rep->is_struct (); }
379 
380  int is_uint16 (void) const { return rep->is_uint16 (); }
381 
382  int is_uint32 (void) const { return rep->is_uint32 (); }
383 
384  int is_uint64 (void) const { return rep->is_uint64 (); }
385 
386  int is_uint8 (void) const { return rep->is_uint8 (); }
387 
388  int is_logical_scalar (void) const { return rep->is_logical_scalar (); }
389 
390  int is_logical_scalar_true (void) const
391  { return rep->is_logical_scalar_true (); }
392 
393  mwSize get_m (void) const { return rep->get_m (); }
394 
395  mwSize get_n (void) const { return rep->get_n (); }
396 
397  mwSize *get_dimensions (void) const { return rep->get_dimensions (); }
398 
400  { return rep->get_number_of_dimensions (); }
401 
403 
405 
406  void set_dimensions (mwSize *dims_arg, mwSize ndims_arg)
407  { DO_VOID_MUTABLE_METHOD (set_dimensions (dims_arg, ndims_arg)); }
408 
410  { return rep->get_number_of_elements (); }
411 
412  int is_empty (void) const { return get_number_of_elements () == 0; }
413 
414  const char *get_name (void) const { return name; }
415 
416  void set_name (const char *name_arg);
417 
418  mxClassID get_class_id (void) const { return rep->get_class_id (); }
419 
420  const char *get_class_name (void) const { return rep->get_class_name (); }
421 
422  void set_class_name (const char *name_arg)
423  { DO_VOID_MUTABLE_METHOD (set_class_name (name_arg)); }
424 
425  mxArray *get_cell (mwIndex idx) const
426  { DO_MUTABLE_METHOD (mxArray *, get_cell (idx)); }
427 
428  void set_cell (mwIndex idx, mxArray *val)
429  { DO_VOID_MUTABLE_METHOD (set_cell (idx, val)); }
430 
431  double get_scalar (void) const { return rep->get_scalar (); }
432 
433  void *get_data (void) const { DO_MUTABLE_METHOD (void *, get_data ()); }
434 
435  void *get_imag_data (void) const
436  { DO_MUTABLE_METHOD (void *, get_imag_data ()); }
437 
438  void set_data (void *pr) { DO_VOID_MUTABLE_METHOD (set_data (pr)); }
439 
441 
442  mwIndex *get_ir (void) const { DO_MUTABLE_METHOD (mwIndex *, get_ir ()); }
443 
444  mwIndex *get_jc (void) const { DO_MUTABLE_METHOD (mwIndex *, get_jc ()); }
445 
446  mwSize get_nzmax (void) const { return rep->get_nzmax (); }
447 
449 
451 
452  void set_nzmax (mwSize nzmax) { DO_VOID_MUTABLE_METHOD (set_nzmax (nzmax)); }
453 
454  int add_field (const char *key) { DO_MUTABLE_METHOD (int, add_field (key)); }
455 
456  void remove_field (int key_num)
457  { DO_VOID_MUTABLE_METHOD (remove_field (key_num)); }
458 
459  mxArray *get_field_by_number (mwIndex index, int key_num) const
460  { DO_MUTABLE_METHOD (mxArray *, get_field_by_number (index, key_num)); }
461 
462  void set_field_by_number (mwIndex index, int key_num, mxArray *val)
463  { DO_VOID_MUTABLE_METHOD (set_field_by_number (index, key_num, val)); }
464 
465  int get_number_of_fields (void) const { return rep->get_number_of_fields (); }
466 
467  const char *get_field_name_by_number (int key_num) const
468  { DO_MUTABLE_METHOD (const char*, get_field_name_by_number (key_num)); }
469 
470  int get_field_number (const char *key) const
471  { DO_MUTABLE_METHOD (int, get_field_number (key)); }
472 
473  int get_string (char *buf, mwSize buflen) const
474  { return rep->get_string (buf, buflen); }
475 
476  char *array_to_string (void) const { return rep->array_to_string (); }
477 
479  { return rep->calc_single_subscript (nsubs, subs); }
480 
481  size_t get_element_size (void) const { return rep->get_element_size (); }
482 
483  bool mutation_needed (void) const { return rep->mutation_needed (); }
484 
485  mxArray *mutate (void) const { return rep->mutate (); }
486 
487  static void *malloc (size_t n);
488 
489  static void *calloc (size_t n, size_t t);
490 
491  static char *strsave (const char *str)
492  {
493  char *retval = 0;
494 
495  if (str)
496  {
497  mwSize sz = sizeof (mxChar) * (strlen (str) + 1);
498  retval = static_cast<char *> (mxArray::malloc (sz));
499  strcpy (retval, str);
500  }
501 
502  return retval;
503  }
504 
505  static octave_value as_octave_value (const mxArray *ptr);
506 
507 protected:
508 
509  octave_value as_octave_value (void) const;
510 
511 private:
512 
513  mutable mxArray_base *rep;
514 
515  char *name;
516 
517  mxArray (mxArray_base *r, const char *n)
518  : rep (r), name (mxArray::strsave (n)) { }
519 
520  void maybe_mutate (void) const;
521 
522  // No copying!
523 
524  mxArray (const mxArray&);
525 
526  mxArray& operator = (const mxArray&);
527 };
528 
529 #undef DO_MUTABLE_METHOD
530 #undef DO_VOID_MUTABLE_METHOD
531 
532 #endif
533 #endif