# HG changeset patch # User Jaroslav Hajek # Date 1254464864 -7200 # Node ID e793865ede6310232e3ba230eef97b962eb16dc4 # Parent c5ff5f858cfdbbdc6ae5a55439ee16f2d1baeda0 implement builtin_type diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,7 @@ +2009-10-02 Jaroslav Hajek + + * lo-traits.h (strip_template_param): New trait class. + 2009-10-01 Jaroslav Hajek * Array.cc (Array::permute): Fast case identity permutation. diff --git a/liboctave/lo-traits.h b/liboctave/lo-traits.h --- a/liboctave/lo-traits.h +++ b/liboctave/lo-traits.h @@ -81,6 +81,23 @@ typedef typename if_then_else::no, T, T const&>::result type; }; +// Will turn TemplatedClass to T, leave T otherwise. +// Useful for stripping wrapper classes, like octave_int. + +template class TemplatedClass, typename T> +class strip_template_param +{ +public: + typedef T type; +}; + +template class TemplatedClass, typename T> +class strip_template_param > +{ +public: + typedef T type; +}; + #endif /* diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,40 @@ +2009-10-02 Jaroslav Hajek + + * ov.h: Define also diag matrix extractors. + (octave_type_traits, octave_array_type_traits): Remove. + (octave_value::builtin_type): New method. + * oct-stream.cc (do_read): Update. + * data.cc (identity_matrix): Update. + * ov-base.h (builtin_type_t): New enum. + (octave_base_value::builtin_type): New method. + * ov-re-mat.h (octave_matrix::builtin_type): New method. + * ov-flt-re-mat.h (octave_float_matrix::builtin_type): New method. + * ov-cx-mat.h (octave_complex_matrix::builtin_type): New method. + * ov-flt-cx-mat.h (octave_float_complex_matrix::builtin_type): New method. + * ov-bool-mat.h (octave_bool_matrix::builtin_type): New method. + * ov-ch-mat.h (octave_char_matrix::builtin_type): New method. + * ov-scalar.h (octave_scalar::builtin_type): New method. + * ov-float.h (octave_float::builtin_type): New method. + * ov-complex.h (octave_complex::builtin_type): New method. + * ov-bool.h (octave_bool::builtin_type): New method. + * ov-flt-complex.h (octave_float_complex::builtin_type): New method. + * ov-re-diag.h (octave_diag_matrix::builtin_type): New method. + * ov-flt-re-diag.h (octave_float_diag_matrix::builtin_type): New method. + * ov-cx-diag.h (octave_complex_diag_matrix::builtin_type): New method. + * ov-flt-cx-diag.h (octave_float_complex_diag_matrix::builtin_type): New method. + * ov-re-sparse.h (octave_sparse_matrix::builtin_type): New method. + * ov-cx-sparse.h (octave_sparse_complex_matrix::builtin_type): New method. + * ov-bool-sparse.h (octave_sparse_bool_matrix::builtin_type): New method. + * ov-intx.h (octave_int_matrix::builtin_type, octave_int_scalar::builtin_type): New methods. + * ov-int8.h: Define OCTAVE_INT_BTYP. + * ov-int16.h: Define OCTAVE_INT_BTYP. + * ov-int32.h: Define OCTAVE_INT_BTYP. + * ov-int64.h: Define OCTAVE_INT_BTYP. + * ov-uint8.h: Define OCTAVE_INT_BTYP. + * ov-uint16.h: Define OCTAVE_INT_BTYP. + * ov-uint32.h: Define OCTAVE_INT_BTYP. + * ov-uint64.h: Define OCTAVE_INT_BTYP. + 2009-10-01 Jaroslav Hajek * DLD-FUNCTIONS/cellfun.cc (Fcellfun): Fix initialization, add missing diff --git a/src/data.cc b/src/data.cc --- a/src/data.cc +++ b/src/data.cc @@ -4160,7 +4160,7 @@ { octave_value retval; - typename octave_array_type_traits::element_type one (1); + typename MT::element_type one (1); if (nr == 1 && nc == 1) retval = one; @@ -4168,7 +4168,7 @@ { dim_vector dims (nr, nc); - typename octave_array_type_traits::element_type zero (0); + typename MT::element_type zero (0); MT m (dims, zero); diff --git a/src/oct-stream.cc b/src/oct-stream.cc --- a/src/oct-stream.cc +++ b/src/oct-stream.cc @@ -3072,10 +3072,10 @@ count = 0; - typename octave_array_type_traits::element_type elt_zero - = typename octave_array_type_traits::element_type (); - - typename octave_array_type_traits::element_type *dat = 0; + typename RET_T::element_type elt_zero + = typename RET_T::element_type (); + + typename RET_T::element_type *dat = 0; octave_idx_type max_size = 0; @@ -3117,8 +3117,8 @@ union { - char buf[sizeof (typename octave_type_traits::val_type)]; - typename octave_type_traits::val_type val; + char buf[sizeof (typename strip_template_param::type)]; + typename strip_template_param::type val; } u; std::istream *isp = strm.input_stream (); @@ -3144,22 +3144,22 @@ break; } - is.read (u.buf, sizeof (typename octave_type_traits::val_type)); + is.read (u.buf, sizeof (typename strip_template_param::type)); // We only swap bytes for integer types. For float // types, the format conversion will also handle byte // swapping. if (swap) - swap_bytes::val_type)> (u.buf); + swap_bytes::type)> (u.buf); else if (do_float_fmt_conv) do_float_format_conversion (u.buf, - sizeof (typename octave_type_traits::val_type), + sizeof (typename strip_template_param::type), 1, from_flt_fmt, oct_mach_info::float_format ()); - typename octave_array_type_traits::element_type tmp - = static_cast ::element_type> (u.val); + typename RET_T::element_type tmp + = static_cast (u.val); if (is) { @@ -3414,13 +3414,13 @@ void write_int (std::ostream& os, bool swap, const T& val) { - typename octave_type_traits::val_type tmp = val.value (); + typename T::val_type tmp = val.value (); if (swap) - swap_bytes::val_type)> (&tmp); + swap_bytes (&tmp); os.write (reinterpret_cast (&tmp), - sizeof (typename octave_type_traits::val_type)); + sizeof (typename T::val_type)); } template void write_int (std::ostream&, bool, const octave_int8&); diff --git a/src/ov-base.h b/src/ov-base.h --- a/src/ov-base.h +++ b/src/ov-base.h @@ -55,6 +55,51 @@ class tree_walker; +enum builtin_type_t +{ + btyp_double, + btyp_float, + btyp_complex, + btyp_float_complex, + btyp_int8, + btyp_int16, + btyp_int32, + btyp_int64, + btyp_uint8, + btyp_uint16, + btyp_uint32, + btyp_uint64, + btyp_char, + btyp_bool, + btyp_unknown +}; + +template +struct class_to_btyp +{ + static const builtin_type_t btyp = btyp_unknown; +}; + +#define DEF_CLASS_TO_BTYP(CLASS,BTYP) \ +template <> \ +struct class_to_btyp \ +{ static const builtin_type_t btyp = BTYP; } + +DEF_CLASS_TO_BTYP (double, btyp_double); +DEF_CLASS_TO_BTYP (float, btyp_float); +DEF_CLASS_TO_BTYP (Complex, btyp_complex); +DEF_CLASS_TO_BTYP (FloatComplex, btyp_float_complex); +DEF_CLASS_TO_BTYP (octave_int8, btyp_int8); +DEF_CLASS_TO_BTYP (octave_int16, btyp_int16); +DEF_CLASS_TO_BTYP (octave_int32, btyp_int32); +DEF_CLASS_TO_BTYP (octave_int64, btyp_int64); +DEF_CLASS_TO_BTYP (octave_uint8, btyp_uint8); +DEF_CLASS_TO_BTYP (octave_uint16, btyp_uint16); +DEF_CLASS_TO_BTYP (octave_uint32, btyp_uint32); +DEF_CLASS_TO_BTYP (octave_uint64, btyp_uint64); +DEF_CLASS_TO_BTYP (bool, btyp_bool); +DEF_CLASS_TO_BTYP (char, btyp_char); + // T_ID is the type id of struct objects, set by register_type(). // T_NAME is the type name of struct objects. @@ -282,6 +327,8 @@ virtual octave_value any (int = 0) const; + virtual builtin_type_t builtin_type (void) const { return btyp_unknown; } + virtual bool is_double_type (void) const { return false; } virtual bool is_single_type (void) const { return false; } diff --git a/src/ov-bool-mat.h b/src/ov-bool-mat.h --- a/src/ov-bool-mat.h +++ b/src/ov-bool-mat.h @@ -80,6 +80,8 @@ idx_vector index_vector (void) const { return idx_cache ? *idx_cache : set_idx_cache (idx_vector (matrix)); } + builtin_type_t builtin_type (void) const { return btyp_bool; } + bool is_bool_matrix (void) const { return true; } bool is_bool_type (void) const { return true; } diff --git a/src/ov-bool-sparse.h b/src/ov-bool-sparse.h --- a/src/ov-bool-sparse.h +++ b/src/ov-bool-sparse.h @@ -88,6 +88,8 @@ idx_vector index_vector (void) const { return idx_vector (bool_array_value ()); } + builtin_type_t builtin_type (void) const { return btyp_bool; } + bool is_bool_matrix (void) const { return true; } bool is_bool_type (void) const { return true; } diff --git a/src/ov-bool.h b/src/ov-bool.h --- a/src/ov-bool.h +++ b/src/ov-bool.h @@ -73,6 +73,8 @@ idx_vector index_vector (void) const { return idx_vector (scalar); } + builtin_type_t builtin_type (void) const { return btyp_bool; } + bool is_real_scalar (void) const { return true; } bool is_bool_scalar (void) const { return true; } diff --git a/src/ov-ch-mat.h b/src/ov-ch-mat.h --- a/src/ov-ch-mat.h +++ b/src/ov-ch-mat.h @@ -82,6 +82,8 @@ idx_vector index_vector (void) const; + builtin_type_t builtin_type (void) const { return btyp_char; } + bool is_char_matrix (void) const { return true; } bool is_real_matrix (void) const { return true; } diff --git a/src/ov-complex.h b/src/ov-complex.h --- a/src/ov-complex.h +++ b/src/ov-complex.h @@ -87,6 +87,8 @@ || lo_ieee_isnan (std::imag (scalar)))); } + builtin_type_t builtin_type (void) const { return btyp_complex; } + bool is_complex_scalar (void) const { return true; } bool is_complex_type (void) const { return true; } diff --git a/src/ov-cx-diag.h b/src/ov-cx-diag.h --- a/src/ov-cx-diag.h +++ b/src/ov-cx-diag.h @@ -57,6 +57,8 @@ octave_base_value *try_narrowing_conversion (void); + builtin_type_t builtin_type (void) const { return btyp_complex; } + bool is_complex_matrix (void) const { return true; } bool is_complex_type (void) const { return true; } diff --git a/src/ov-cx-mat.h b/src/ov-cx-mat.h --- a/src/ov-cx-mat.h +++ b/src/ov-cx-mat.h @@ -90,6 +90,8 @@ octave_base_value *try_narrowing_conversion (void); + builtin_type_t builtin_type (void) const { return btyp_complex; } + bool is_complex_matrix (void) const { return true; } bool is_complex_type (void) const { return true; } diff --git a/src/ov-cx-sparse.h b/src/ov-cx-sparse.h --- a/src/ov-cx-sparse.h +++ b/src/ov-cx-sparse.h @@ -97,6 +97,8 @@ void assign (const octave_value_list& idx, const SparseMatrix& rhs); + builtin_type_t builtin_type (void) const { return btyp_complex; } + bool is_complex_matrix (void) const { return true; } bool is_complex_type (void) const { return true; } diff --git a/src/ov-float.h b/src/ov-float.h --- a/src/ov-float.h +++ b/src/ov-float.h @@ -82,6 +82,8 @@ octave_value any (int = 0) const { return (scalar != 0 && ! lo_ieee_isnan (scalar)); } + builtin_type_t builtin_type (void) const { return btyp_float; } + bool is_real_scalar (void) const { return true; } bool is_real_type (void) const { return true; } diff --git a/src/ov-flt-complex.h b/src/ov-flt-complex.h --- a/src/ov-flt-complex.h +++ b/src/ov-flt-complex.h @@ -85,6 +85,8 @@ || lo_ieee_isnan (std::imag (scalar)))); } + builtin_type_t builtin_type (void) const { return btyp_float_complex; } + bool is_complex_scalar (void) const { return true; } bool is_complex_type (void) const { return true; } diff --git a/src/ov-flt-cx-diag.h b/src/ov-flt-cx-diag.h --- a/src/ov-flt-cx-diag.h +++ b/src/ov-flt-cx-diag.h @@ -55,6 +55,8 @@ octave_base_value *try_narrowing_conversion (void); + builtin_type_t builtin_type (void) const { return btyp_float_complex; } + bool is_complex_matrix (void) const { return true; } bool is_complex_type (void) const { return true; } diff --git a/src/ov-flt-cx-mat.h b/src/ov-flt-cx-mat.h --- a/src/ov-flt-cx-mat.h +++ b/src/ov-flt-cx-mat.h @@ -88,6 +88,8 @@ octave_base_value *try_narrowing_conversion (void); + builtin_type_t builtin_type (void) const { return btyp_float_complex; } + bool is_complex_matrix (void) const { return true; } bool is_complex_type (void) const { return true; } diff --git a/src/ov-flt-re-diag.h b/src/ov-flt-re-diag.h --- a/src/ov-flt-re-diag.h +++ b/src/ov-flt-re-diag.h @@ -55,6 +55,8 @@ octave_base_value *try_narrowing_conversion (void); + builtin_type_t builtin_type (void) const { return btyp_float; } + bool is_real_matrix (void) const { return true; } bool is_real_type (void) const { return true; } diff --git a/src/ov-flt-re-mat.h b/src/ov-flt-re-mat.h --- a/src/ov-flt-re-mat.h +++ b/src/ov-flt-re-mat.h @@ -91,6 +91,8 @@ idx_vector index_vector (void) const { return idx_cache ? *idx_cache : set_idx_cache (idx_vector (matrix)); } + builtin_type_t builtin_type (void) const { return btyp_float; } + bool is_real_matrix (void) const { return true; } bool is_real_type (void) const { return true; } diff --git a/src/ov-int16.h b/src/ov-int16.h --- a/src/ov-int16.h +++ b/src/ov-int16.h @@ -35,6 +35,8 @@ #define OCTAVE_INT_MX_CLASS mxINT16_CLASS +#define OCTAVE_INT_BTYP btyp_int16 + #include "ov-intx.h" #undef OCTAVE_INT_T @@ -49,6 +51,8 @@ #undef OCTAVE_INT_MX_CLASS +#undef OCTAVE_INT_BTYP + #endif /* diff --git a/src/ov-int32.h b/src/ov-int32.h --- a/src/ov-int32.h +++ b/src/ov-int32.h @@ -35,6 +35,8 @@ #define OCTAVE_INT_MX_CLASS mxINT32_CLASS +#define OCTAVE_INT_BTYP btyp_int32 + #include "ov-intx.h" #undef OCTAVE_INT_T @@ -49,6 +51,8 @@ #undef OCTAVE_INT_MX_CLASS +#undef OCTAVE_INT_BTYP + #endif /* diff --git a/src/ov-int64.h b/src/ov-int64.h --- a/src/ov-int64.h +++ b/src/ov-int64.h @@ -35,6 +35,8 @@ #define OCTAVE_INT_MX_CLASS mxINT64_CLASS +#define OCTAVE_INT_BTYP btyp_int64 + #include "ov-intx.h" #undef OCTAVE_INT_T @@ -49,6 +51,8 @@ #undef OCTAVE_INT_MX_CLASS +#undef OCTAVE_INT_BTYP + #endif /* diff --git a/src/ov-int8.h b/src/ov-int8.h --- a/src/ov-int8.h +++ b/src/ov-int8.h @@ -35,6 +35,8 @@ #define OCTAVE_INT_MX_CLASS mxINT8_CLASS +#define OCTAVE_INT_BTYP btyp_int8 + #include "ov-intx.h" #undef OCTAVE_INT_T @@ -49,6 +51,8 @@ #undef OCTAVE_INT_MX_CLASS +#undef OCTAVE_INT_BTYP + #endif /* diff --git a/src/ov-intx.h b/src/ov-intx.h --- a/src/ov-intx.h +++ b/src/ov-intx.h @@ -68,6 +68,8 @@ bool is_integer_type (void) const { return true; } + builtin_type_t builtin_type (void) const { return OCTAVE_INT_BTYP; } + private: template @@ -442,6 +444,8 @@ bool is_integer_type (void) const { return true; } + builtin_type_t builtin_type (void) const { return OCTAVE_INT_BTYP; } + private: template diff --git a/src/ov-range.h b/src/ov-range.h --- a/src/ov-range.h +++ b/src/ov-range.h @@ -158,6 +158,8 @@ sortmode is_sorted_rows (sortmode mode = UNSORTED) const { return mode ? mode : ASCENDING; } + builtin_type_t builtin_type (void) const { return btyp_double; } + bool is_real_type (void) const { return true; } bool is_double_type (void) const { return true; } diff --git a/src/ov-re-diag.h b/src/ov-re-diag.h --- a/src/ov-re-diag.h +++ b/src/ov-re-diag.h @@ -60,6 +60,8 @@ octave_value do_index_op (const octave_value_list& idx, bool resize_ok = false); + builtin_type_t builtin_type (void) const { return btyp_double; } + bool is_real_matrix (void) const { return true; } bool is_real_type (void) const { return true; } diff --git a/src/ov-re-mat.h b/src/ov-re-mat.h --- a/src/ov-re-mat.h +++ b/src/ov-re-mat.h @@ -108,6 +108,8 @@ idx_vector index_vector (void) const { return idx_cache ? *idx_cache : set_idx_cache (idx_vector (matrix)); } + builtin_type_t builtin_type (void) const { return btyp_double; } + bool is_real_matrix (void) const { return true; } bool is_real_type (void) const { return true; } diff --git a/src/ov-re-sparse.h b/src/ov-re-sparse.h --- a/src/ov-re-sparse.h +++ b/src/ov-re-sparse.h @@ -93,6 +93,8 @@ idx_vector index_vector (void) const; + builtin_type_t builtin_type (void) const { return btyp_double; } + bool is_real_matrix (void) const { return true; } bool is_real_type (void) const { return true; } diff --git a/src/ov-scalar.h b/src/ov-scalar.h --- a/src/ov-scalar.h +++ b/src/ov-scalar.h @@ -83,6 +83,8 @@ octave_value any (int = 0) const { return (scalar != 0 && ! lo_ieee_isnan (scalar)); } + builtin_type_t builtin_type (void) const { return btyp_double; } + bool is_real_scalar (void) const { return true; } bool is_real_type (void) const { return true; } diff --git a/src/ov-uint16.h b/src/ov-uint16.h --- a/src/ov-uint16.h +++ b/src/ov-uint16.h @@ -35,6 +35,8 @@ #define OCTAVE_INT_MX_CLASS mxUINT16_CLASS +#define OCTAVE_INT_BTYP btyp_uint16 + #include "ov-intx.h" #undef OCTAVE_INT_T @@ -49,6 +51,8 @@ #undef OCTAVE_INT_MX_CLASS +#undef OCTAVE_INT_BTYP + #endif /* diff --git a/src/ov-uint32.h b/src/ov-uint32.h --- a/src/ov-uint32.h +++ b/src/ov-uint32.h @@ -35,6 +35,8 @@ #define OCTAVE_INT_MX_CLASS mxUINT32_CLASS +#define OCTAVE_INT_BTYP btyp_uint32 + #include "ov-intx.h" #undef OCTAVE_INT_T @@ -49,6 +51,8 @@ #undef OCTAVE_INT_MX_CLASS +#undef OCTAVE_INT_BTYP + #endif /* diff --git a/src/ov-uint64.h b/src/ov-uint64.h --- a/src/ov-uint64.h +++ b/src/ov-uint64.h @@ -35,6 +35,8 @@ #define OCTAVE_INT_MX_CLASS mxUINT64_CLASS +#define OCTAVE_INT_BTYP btyp_uint64 + #include "ov-intx.h" #undef OCTAVE_INT_T @@ -49,6 +51,8 @@ #undef OCTAVE_INT_MX_CLASS +#undef OCTAVE_INT_BTYP + #endif /* diff --git a/src/ov-uint8.h b/src/ov-uint8.h --- a/src/ov-uint8.h +++ b/src/ov-uint8.h @@ -35,6 +35,8 @@ #define OCTAVE_INT_MX_CLASS mxUINT8_CLASS +#define OCTAVE_INT_BTYP btyp_uint8 + #include "ov-intx.h" #undef OCTAVE_INT_T @@ -49,6 +51,8 @@ #undef OCTAVE_INT_MX_CLASS +#undef OCTAVE_INT_BTYP + #endif /* diff --git a/src/ov.h b/src/ov.h --- a/src/ov.h +++ b/src/ov.h @@ -550,6 +550,9 @@ octave_value any (int dim = 0) const { return rep->any (dim); } + builtin_type_t builtin_type (void) const + { return rep->builtin_type (); } + // Floating point types. bool is_double_type (void) const @@ -1252,63 +1255,6 @@ extern OCTINTERP_API void install_types (void); -// FIXME -- these trait classes probably belong somehwere else... - -template -class -octave_type_traits -{ -public: - typedef T val_type; -}; - -#define OCTAVE_TYPE_TRAIT(T, VAL_T) \ - template <> \ - class \ - octave_type_traits \ - { \ - public: \ - typedef VAL_T val_type; \ - } - -OCTAVE_TYPE_TRAIT (octave_int8, octave_int8::val_type); -OCTAVE_TYPE_TRAIT (octave_uint8, octave_uint8::val_type); -OCTAVE_TYPE_TRAIT (octave_int16, octave_int16::val_type); -OCTAVE_TYPE_TRAIT (octave_uint16, octave_uint16::val_type); -OCTAVE_TYPE_TRAIT (octave_int32, octave_int32::val_type); -OCTAVE_TYPE_TRAIT (octave_uint32, octave_uint32::val_type); -OCTAVE_TYPE_TRAIT (octave_int64, octave_int64::val_type); -OCTAVE_TYPE_TRAIT (octave_uint64, octave_uint64::val_type); - -template -class octave_array_type_traits -{ -public: - typedef T element_type; -}; - -#define OCTAVE_ARRAY_TYPE_TRAIT(T, ELT_T) \ - template <> \ - class \ - octave_array_type_traits \ - { \ - public: \ - typedef ELT_T element_type; \ - } - -OCTAVE_ARRAY_TYPE_TRAIT (charNDArray, char); -OCTAVE_ARRAY_TYPE_TRAIT (boolNDArray, bool); -OCTAVE_ARRAY_TYPE_TRAIT (int8NDArray, octave_int8); -OCTAVE_ARRAY_TYPE_TRAIT (uint8NDArray, octave_uint8); -OCTAVE_ARRAY_TYPE_TRAIT (int16NDArray, octave_int16); -OCTAVE_ARRAY_TYPE_TRAIT (uint16NDArray, octave_uint16); -OCTAVE_ARRAY_TYPE_TRAIT (int32NDArray, octave_int32); -OCTAVE_ARRAY_TYPE_TRAIT (uint32NDArray, octave_uint32); -OCTAVE_ARRAY_TYPE_TRAIT (int64NDArray, octave_int64); -OCTAVE_ARRAY_TYPE_TRAIT (uint64NDArray, octave_uint64); -OCTAVE_ARRAY_TYPE_TRAIT (NDArray, double); -OCTAVE_ARRAY_TYPE_TRAIT (FloatNDArray, float); - // This will eventually go away, but for now it can be used to // simplify the transition to the new octave_value class hierarchy, // which uses octave_base_value instead of octave_value for the type @@ -1373,6 +1319,12 @@ DEF_VALUE_EXTRACTOR (ComplexRowVector, complex_row_vector) DEF_VALUE_EXTRACTOR (FloatComplexRowVector, float_complex_row_vector) +DEF_VALUE_EXTRACTOR (DiagMatrix, diag_matrix) +DEF_VALUE_EXTRACTOR (FloatDiagMatrix, float_diag_matrix) +DEF_VALUE_EXTRACTOR (ComplexDiagMatrix, complex_diag_matrix) +DEF_VALUE_EXTRACTOR (FloatComplexDiagMatrix, float_complex_diag_matrix) +DEF_VALUE_EXTRACTOR (PermMatrix, perm_matrix) + #undef DEF_VALUE_EXTRACTOR #endif