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
ov-base.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 John W. Eaton
4 Copyright (C) 2009-2010 VZLU Prague
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 #if !defined (octave_ov_base_h)
25 #define octave_ov_base_h 1
26 
27 #include <cstdlib>
28 
29 #include <iosfwd>
30 #include <list>
31 #include <string>
32 
33 #include "Range.h"
34 #include "data-conv.h"
35 #include "mx-base.h"
36 #include "str-vec.h"
37 
38 #include "error.h"
39 #include "oct-hdf5.h"
40 
41 class Cell;
42 class mxArray;
43 class octave_map;
44 class octave_scalar_map;
45 class octave_value;
46 class octave_value_list;
47 class octave_stream;
48 class octave_function;
50 class octave_user_script;
51 class octave_user_code;
52 class octave_fcn_handle;
53 class octave_fcn_inline;
54 class octave_value_list;
55 class octave_lvalue;
56 
57 class tree_walker;
58 
60 {
80 };
81 
82 extern OCTINTERP_API std::string
84 
86 get_builtin_classes (void);
87 
88 inline bool btyp_isnumeric (builtin_type_t btyp)
89 { return btyp <= btyp_uint64; }
90 
91 inline bool btyp_isinteger (builtin_type_t btyp)
92 { return btyp >= btyp_int8 && btyp <= btyp_uint64; }
93 
94 inline bool btyp_isfloat (builtin_type_t btyp)
95 { return btyp <= btyp_float_complex; }
96 
97 inline bool btyp_isarray (builtin_type_t btyp)
98 { return btyp <= btyp_char; }
99 
100 // Compute numeric type for a possible mixed-type operation, using these rules:
101 // bool -> double
102 // single + double -> single
103 // real + complex -> complex
104 // integer + real -> integer
105 // uint + uint -> uint (the bigger one)
106 // sint + sint -> sint (the bigger one)
107 //
108 // failing otherwise.
109 
110 extern OCTINTERP_API
112 
113 template <class T>
115 {
117 };
118 
119 #define DEF_CLASS_TO_BTYP(CLASS,BTYP) \
120 template <> \
121 struct class_to_btyp<CLASS> \
122 { static const builtin_type_t btyp = BTYP; }
123 
138 
139 // T_ID is the type id of struct objects, set by register_type().
140 // T_NAME is the type name of struct objects.
141 
142 #define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA \
143  DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA2 (OCTAVE_EMPTY_CPP_ARG)
144 
145 #define DECLARE_OV_BASE_TYPEID_FUNCTIONS_AND_DATA \
146  DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA2(virtual)
147 
148 #define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA2(VIRTUAL) \
149  public: \
150  VIRTUAL int type_id (void) const { return t_id; } \
151  VIRTUAL std::string type_name (void) const { return t_name; } \
152  VIRTUAL std::string class_name (void) const { return c_name; } \
153  static int static_type_id (void) { return t_id; } \
154  static std::string static_type_name (void) { return t_name; } \
155  static std::string static_class_name (void) { return c_name; } \
156  static void register_type (void); \
157  \
158  private: \
159  static int t_id; \
160  static const std::string t_name; \
161  static const std::string c_name;
162 
163 
164 #define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c) \
165  int t::t_id (-1); \
166  const std::string t::t_name (n); \
167  const std::string t::c_name (c); \
168  void t::register_type (void) \
169  { \
170  static t exemplar; \
171  octave_value v (&exemplar, true); \
172  t_id = octave_value_typeinfo::register_type (t::t_name, t::c_name, v); \
173  }
174 
175 // A base value type, so that derived types only have to redefine what
176 // they need (if they are derived from octave_base_value instead of
177 // octave_value).
178 
179 class
182 {
183 public:
184 
185  typedef octave_base_value * (*type_conv_fcn) (const octave_base_value&);
186 
187  // type conversion, including result type information
189  {
190  public:
191  type_conv_info (type_conv_fcn f = 0, int t = -1)
192  : _fcn (f), _type_id (t) { }
193 
194  operator type_conv_fcn (void) const { return _fcn; }
195 
196  octave_base_value * operator () (const octave_base_value &v) const
197  { return (*_fcn) (v); }
198 
199  int type_id (void) const { return _type_id; }
200 
201  private:
202  type_conv_fcn _fcn;
203  int _type_id;
204  };
205 
206  friend class octave_value;
207 
208  octave_base_value (void) : count (1) { }
209 
210  octave_base_value (const octave_base_value&) : count (1) { }
211 
212  virtual ~octave_base_value (void) { }
213 
214  // Unconditional clone. Always clones.
215  virtual octave_base_value *
216  clone (void) const { return new octave_base_value (*this); }
217 
218  // Empty clone.
219  virtual octave_base_value *
220  empty_clone (void) const;
221 
222  // Unique clone. Usually clones, but may be overriden to fake the
223  // cloning when sharing copies is to be controlled from within an
224  // instance (see octave_class).
225  virtual octave_base_value *
226  unique_clone (void) { return clone (); }
227 
228  virtual type_conv_info
230  { return type_conv_info (); }
231 
232  virtual type_conv_info
234  { return type_conv_info (); }
235 
236  virtual octave_value squeeze (void) const;
237 
238  virtual octave_value full_value (void) const;
239 
240  virtual octave_base_value *try_narrowing_conversion (void) { return 0; }
241 
242  virtual void maybe_economize (void) { }
243 
244  virtual Matrix size (void);
245 
246  virtual octave_idx_type numel (const octave_value_list&);
247 
248  virtual octave_value
249  subsref (const std::string& type,
250  const std::list<octave_value_list>& idx);
251 
252  virtual octave_value_list
253  subsref (const std::string& type,
254  const std::list<octave_value_list>& idx,
255  int nargout);
256 
257  virtual octave_value
258  subsref (const std::string& type,
259  const std::list<octave_value_list>& idx,
260  bool auto_add);
261 
262  virtual octave_value_list
263  subsref (const std::string& type,
264  const std::list<octave_value_list>& idx,
265  int nargout,
266  const std::list<octave_lvalue> *lvalue_list);
267 
268  virtual octave_value
269  do_index_op (const octave_value_list& idx, bool resize_ok = false);
270 
271  virtual octave_value_list
272  do_multi_index_op (int nargout, const octave_value_list& idx);
273 
274  virtual octave_value_list
275  do_multi_index_op (int nargout, const octave_value_list& idx,
276  const std::list<octave_lvalue> *lvalue_list);
277 
278  virtual void assign (const std::string&, const octave_value&) { }
279 
280  virtual octave_value
281  subsasgn (const std::string& type,
282  const std::list<octave_value_list>& idx,
283  const octave_value& rhs);
284 
285  virtual octave_value
286  undef_subsasgn (const std::string& type,
287  const std::list<octave_value_list>& idx,
288  const octave_value& rhs);
289 
290  virtual idx_vector index_vector (void) const;
291 
292  virtual dim_vector dims (void) const { return dim_vector (); }
293 
294  octave_idx_type rows (void) const
295  {
296  const dim_vector dv = dims ();
297 
298  return dv(0);
299  }
300 
302  {
303  const dim_vector dv = dims ();
304 
305  return dv(1);
306  }
307 
308  virtual int ndims (void) const
309  { return dims ().length (); }
310 
311  virtual octave_idx_type numel (void) const { return dims ().numel (); }
312 
313  virtual octave_idx_type capacity (void) const { return numel (); }
314 
315  virtual size_t byte_size (void) const { return 0; }
316 
317  virtual octave_idx_type nnz (void) const;
318 
319  virtual octave_idx_type nzmax (void) const;
320 
321  virtual octave_idx_type nfields (void) const;
322 
323  virtual octave_value reshape (const dim_vector&) const;
324 
325  virtual octave_value permute (const Array<int>& vec, bool = false) const;
326 
327  virtual octave_value resize (const dim_vector&, bool fill = false) const;
328 
329  virtual MatrixType matrix_type (void) const;
330 
331  virtual MatrixType matrix_type (const MatrixType& typ) const;
332 
333  virtual bool is_defined (void) const { return false; }
334 
335  bool is_empty (void) const { return (dims ().any_zero ()); }
336 
337  virtual bool is_cell (void) const { return false; }
338 
339  virtual bool is_cellstr (void) const { return false; }
340 
341  virtual bool is_real_scalar (void) const { return false; }
342 
343  virtual bool is_real_matrix (void) const { return false; }
344 
345  virtual bool is_complex_scalar (void) const { return false; }
346 
347  virtual bool is_complex_matrix (void) const { return false; }
348 
349  virtual bool is_bool_scalar (void) const { return false; }
350 
351  virtual bool is_bool_matrix (void) const { return false; }
352 
353  virtual bool is_char_matrix (void) const { return false; }
354 
355  virtual bool is_diag_matrix (void) const { return false; }
356 
357  virtual bool is_perm_matrix (void) const { return false; }
358 
359  virtual bool is_string (void) const { return false; }
360 
361  virtual bool is_sq_string (void) const { return false; }
362 
363  virtual bool is_range (void) const { return false; }
364 
365  virtual bool is_map (void) const { return false; }
366 
367  virtual bool is_object (void) const { return false; }
368 
369  virtual bool is_java (void) const { return false; }
370 
371  virtual bool is_cs_list (void) const { return false; }
372 
373  virtual bool is_magic_colon (void) const { return false; }
374 
375  virtual bool is_all_va_args (void) const { return false; }
376 
377  virtual octave_value all (int = 0) const;
378 
379  virtual octave_value any (int = 0) const;
380 
381  virtual builtin_type_t builtin_type (void) const { return btyp_unknown; }
382 
383  virtual bool is_double_type (void) const { return false; }
384 
385  virtual bool is_single_type (void) const { return false; }
386 
387  virtual bool is_float_type (void) const { return false; }
388 
389  virtual bool is_int8_type (void) const { return false; }
390 
391  virtual bool is_int16_type (void) const { return false; }
392 
393  virtual bool is_int32_type (void) const { return false; }
394 
395  virtual bool is_int64_type (void) const { return false; }
396 
397  virtual bool is_uint8_type (void) const { return false; }
398 
399  virtual bool is_uint16_type (void) const { return false; }
400 
401  virtual bool is_uint32_type (void) const { return false; }
402 
403  virtual bool is_uint64_type (void) const { return false; }
404 
405  virtual bool is_bool_type (void) const { return false; }
406 
407  virtual bool is_integer_type (void) const { return false; }
408 
409  virtual bool is_real_type (void) const { return false; }
410 
411  virtual bool is_complex_type (void) const { return false; }
412 
413  // Would be nice to get rid of the next four functions:
414 
415  virtual bool is_scalar_type (void) const { return false; }
416 
417  virtual bool is_matrix_type (void) const { return false; }
418 
419  virtual bool is_numeric_type (void) const { return false; }
420 
421  virtual bool is_sparse_type (void) const { return false; }
422 
423  virtual bool is_true (void) const { return false; }
424 
425  virtual bool is_null_value (void) const { return false; }
426 
427  virtual bool is_constant (void) const { return false; }
428 
429  virtual bool is_function_handle (void) const { return false; }
430 
431  virtual bool is_anonymous_function (void) const { return false; }
432 
433  virtual bool is_inline_function (void) const { return false; }
434 
435  virtual bool is_function (void) const { return false; }
436 
437  virtual bool is_user_script (void) const { return false; }
438 
439  virtual bool is_user_function (void) const { return false; }
440 
441  virtual bool is_user_code (void) const { return false; }
442 
443  virtual bool is_builtin_function (void) const { return false; }
444 
445  virtual bool is_dld_function (void) const { return false; }
446 
447  virtual bool is_mex_function (void) const { return false; }
448 
449  virtual void erase_subfunctions (void) { }
450 
451  virtual short int short_value (bool = false, bool = false) const;
452 
453  virtual unsigned short int ushort_value (bool = false, bool = false) const;
454 
455  virtual int int_value (bool = false, bool = false) const;
456 
457  virtual unsigned int uint_value (bool = false, bool = false) const;
458 
459  virtual int nint_value (bool = false) const;
460 
461  virtual long int long_value (bool = false, bool = false) const;
462 
463  virtual unsigned long int ulong_value (bool = false, bool = false) const;
464 
465  virtual int64_t int64_value (bool = false, bool = false) const;
466 
467  virtual uint64_t uint64_value (bool = false, bool = false) const;
468 
469  virtual double double_value (bool = false) const;
470 
471  virtual float float_value (bool = false) const;
472 
473  virtual double scalar_value (bool frc_str_conv = false) const
474  { return double_value (frc_str_conv); }
475 
476  virtual float float_scalar_value (bool frc_str_conv = false) const
477  { return float_value (frc_str_conv); }
478 
479  virtual Cell cell_value (void) const;
480 
481  virtual Matrix matrix_value (bool = false) const;
482 
483  virtual FloatMatrix float_matrix_value (bool = false) const;
484 
485  virtual NDArray array_value (bool = false) const;
486 
487  virtual FloatNDArray float_array_value (bool = false) const;
488 
489  virtual Complex complex_value (bool = false) const;
490 
491  virtual FloatComplex float_complex_value (bool = false) const;
492 
493  virtual ComplexMatrix complex_matrix_value (bool = false) const;
494 
495  virtual FloatComplexMatrix float_complex_matrix_value (bool = false) const;
496 
497  virtual ComplexNDArray complex_array_value (bool = false) const;
498 
499  virtual FloatComplexNDArray float_complex_array_value (bool = false) const;
500 
501  virtual bool bool_value (bool = false) const;
502 
503  virtual boolMatrix bool_matrix_value (bool = false) const;
504 
505  virtual boolNDArray bool_array_value (bool = false) const;
506 
507  virtual charMatrix char_matrix_value (bool force = false) const;
508 
509  virtual charNDArray char_array_value (bool = false) const;
510 
511  virtual SparseMatrix sparse_matrix_value (bool = false) const;
512 
513  virtual SparseComplexMatrix sparse_complex_matrix_value (bool = false) const;
514 
515  virtual SparseBoolMatrix sparse_bool_matrix_value (bool = false) const;
516 
517  virtual DiagMatrix diag_matrix_value (bool = false) const;
518 
519  virtual FloatDiagMatrix float_diag_matrix_value (bool = false) const;
520 
521  virtual ComplexDiagMatrix complex_diag_matrix_value (bool = false) const;
522 
523  virtual FloatComplexDiagMatrix
524  float_complex_diag_matrix_value (bool = false) const;
525 
526  virtual PermMatrix perm_matrix_value (void) const;
527 
528  virtual octave_int8 int8_scalar_value (void) const;
529 
530  virtual octave_int16 int16_scalar_value (void) const;
531 
532  virtual octave_int32 int32_scalar_value (void) const;
533 
534  virtual octave_int64 int64_scalar_value (void) const;
535 
536  virtual octave_uint8 uint8_scalar_value (void) const;
537 
538  virtual octave_uint16 uint16_scalar_value (void) const;
539 
540  virtual octave_uint32 uint32_scalar_value (void) const;
541 
542  virtual octave_uint64 uint64_scalar_value (void) const;
543 
544  virtual int8NDArray int8_array_value (void) const;
545 
546  virtual int16NDArray int16_array_value (void) const;
547 
548  virtual int32NDArray int32_array_value (void) const;
549 
550  virtual int64NDArray int64_array_value (void) const;
551 
552  virtual uint8NDArray uint8_array_value (void) const;
553 
554  virtual uint16NDArray uint16_array_value (void) const;
555 
556  virtual uint32NDArray uint32_array_value (void) const;
557 
558  virtual uint64NDArray uint64_array_value (void) const;
559 
560  virtual string_vector all_strings (bool pad = false) const;
561 
562  virtual std::string string_value (bool force = false) const;
563 
564  virtual Array<std::string> cellstr_value (void) const;
565 
566  virtual Range range_value (void) const;
567 
568  virtual octave_map map_value (void) const;
569 
570  virtual octave_scalar_map scalar_map_value (void) const;
571 
572  virtual string_vector map_keys (void) const;
573 
574  virtual size_t nparents (void) const;
575 
576  virtual std::list<std::string> parent_class_name_list (void) const;
577 
578  virtual string_vector parent_class_names (void) const;
579 
580  virtual octave_base_value *find_parent_class (const std::string&)
581  { return 0; }
582 
583  virtual octave_base_value *unique_parent_class (const std::string&)
584  { return 0; }
585 
586  virtual octave_function *function_value (bool silent = false);
587 
588  virtual octave_user_function *user_function_value (bool silent = false);
589 
590  virtual octave_user_script *user_script_value (bool silent = false);
591 
592  virtual octave_user_code *user_code_value (bool silent = false);
593 
594  virtual octave_fcn_handle *fcn_handle_value (bool silent = false);
595 
596  virtual octave_fcn_inline *fcn_inline_value (bool silent = false);
597 
598  virtual octave_value_list list_value (void) const;
599 
600  virtual octave_value convert_to_str (bool pad = false, bool force = false,
601  char type = '\'') const;
602  virtual octave_value
603  convert_to_str_internal (bool pad, bool force, char type) const;
604 
605  virtual void convert_to_row_or_column_vector (void);
606 
607  virtual bool print_as_scalar (void) const { return false; }
608 
609  virtual void print (std::ostream& os, bool pr_as_read_syntax = false) const;
610 
611  virtual void
612  print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
613 
614  virtual bool
615  print_name_tag (std::ostream& os, const std::string& name) const;
616 
617  virtual void
618  print_with_name (std::ostream& output_buf, const std::string& name,
619  bool print_padding = true);
620 
621  virtual void short_disp (std::ostream& os) const { os << "..."; }
622 
623  virtual void print_info (std::ostream& os, const std::string& prefix) const;
624 
625  virtual bool save_ascii (std::ostream& os);
626 
627  virtual bool load_ascii (std::istream& is);
628 
629  virtual bool save_binary (std::ostream& os, bool& save_as_floats);
630 
631  virtual bool load_binary (std::istream& is, bool swap,
633 
634 #if defined (HAVE_HDF5)
635  virtual bool
636  save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
637 
638  virtual bool
639  load_hdf5 (hid_t loc_id, const char *name);
640 #endif
641 
642  virtual int
643  write (octave_stream& os, int block_size,
644  oct_data_conv::data_type output_type, int skip,
645  oct_mach_info::float_format flt_fmt) const;
646 
647  virtual void *mex_get_data (void) const { return 0; }
648 
649  virtual octave_idx_type *mex_get_ir (void) const { return 0; }
650 
651  virtual octave_idx_type *mex_get_jc (void) const { return 0; }
652 
653  virtual mxArray *as_mxArray (void) const;
654 
655  virtual octave_value diag (octave_idx_type k = 0) const;
656 
657  virtual octave_value diag (octave_idx_type m, octave_idx_type n) const;
658 
659  virtual octave_value sort (octave_idx_type dim = 0,
660  sortmode mode = ASCENDING) const;
662  octave_idx_type dim = 0,
663  sortmode mode = ASCENDING) const;
664 
665  virtual sortmode is_sorted (sortmode mode = UNSORTED) const;
666 
667  virtual Array<octave_idx_type>
668  sort_rows_idx (sortmode mode = ASCENDING) const;
669 
670  virtual sortmode is_sorted_rows (sortmode mode = UNSORTED) const;
671 
672  virtual void lock (void);
673 
674  virtual void unlock (void);
675 
676  virtual bool islocked (void) const { return false; }
677 
678  virtual void dump (std::ostream& os) const;
679 
680  // Standard mappers. Register new ones here.
682  {
745  num_unary_mappers = umap_unknown
746  };
747 
748  virtual octave_value map (unary_mapper_t) const;
749 
750  // These are fast indexing & assignment shortcuts for extracting
751  // or inserting a single scalar from/to an array.
752 
753  // Extract the n-th element, aka val(n). Result is undefined if val is not an
754  // array type or n is out of range. Never error.
755  virtual octave_value
757 
758  // Assign the n-th element, aka val(n) = x. Returns false if val is not an
759  // array type, x is not a matching scalar type, or n is out of range.
760  // Never error.
761  virtual bool
763 
764  // This is a helper for the above, to be overriden in scalar types. The
765  // whole point is to handle the insertion efficiently with just *two* VM
766  // calls, which is basically the theoretical minimum.
767  virtual bool
768  fast_elem_insert_self (void *where, builtin_type_t btyp) const;
769 
770  // Grab the reference count. For use by jit.
771  void
772  grab (void)
773  {
774  ++count;
775  }
776 
777  // Release the reference count. For use by jit.
778  void
779  release (void)
780  {
781  if (--count == 0)
782  delete this;
783  }
784 
785 protected:
786 
787  // This should only be called for derived types.
788 
789  octave_value numeric_assign (const std::string& type,
790  const std::list<octave_value_list>& idx,
791  const octave_value& rhs);
792 
793  void reset_indent_level (void) const
794  { curr_print_indent_level = 0; }
795 
796  void increment_indent_level (void) const
797  { curr_print_indent_level += 2; }
798 
799  void decrement_indent_level (void) const
800  { curr_print_indent_level -= 2; }
801 
802  int current_print_indent_level (void) const
803  { return curr_print_indent_level; }
804 
805  void indent (std::ostream& os) const;
806 
807  void newline (std::ostream& os) const;
808 
809  void reset (void) const;
810 
811  // A reference count.
812  // NOTE: the declaration is octave_idx_type because with 64-bit indexing,
813  // it is well possible to have more than MAX_INT copies of a single value
814  // (think of an empty cell array with >2G elements).
816 
817  static const char *get_umap_name (unary_mapper_t);
818 
819 private:
820 
822  static bool beginning_of_line;
823 
825 };
826 
827 // TRUE means to perform automatic sparse to real mutation if there
828 // is memory to be saved
830 
831 #endif