Mercurial > hg > octave-terminal
changeset 5907:288c341438f9
[project @ 2006-07-27 03:21:02 by jwe]
author | jwe |
---|---|
date | Thu, 27 Jul 2006 03:21:03 +0000 |
parents | bdf16e1564e1 |
children | 9c134531c2c4 |
files | src/ChangeLog src/mex.cc src/mxarray.h |
diffstat | 3 files changed, 170 insertions(+), 153 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,16 @@ 2006-07-26 John W. Eaton <jwe@octave.org> + * mex.cc (mxArray::as_octave_value (mxArray *)): New static function. + (call_mex, mexPutVariable, mexCallMATLAB, + mxArray_cell::as_octave_value, mxArray_struct::as_octave_value): + Use static version of as_octave_value here. + (mxArray_sparse::mxArray_sparse (const mxArray_sparse&)): + Check pr, ir, and jc before calling memcpy. + (mxArray_number::mxArray_number): Check pr before calling memcpy + + * mxarray.h (mxArray::as_octave_value (mxArray *)): Provide decl. + (mxArray::as_octave_value): Now protected. + * file-io.cc (Ffeof, Fferror): Use DEFUNX instead of DEFUN. * mex.cc (xfree): New function.
--- a/src/mex.cc +++ b/src/mex.cc @@ -98,8 +98,6 @@ ~mxArray_base (void) { } - octave_value as_octave_value (void) const = 0; - bool is_octave_value (void) const { return false; } int is_cell (void) const = 0; @@ -235,6 +233,8 @@ protected: + octave_value as_octave_value (void) const = 0; + mxArray_base (const mxArray_base&) : mxArray (xmxArray ()) { } void invalid_type_error (void) const @@ -276,8 +276,6 @@ mxFree (dims); } - octave_value as_octave_value (void) const { return val; } - bool is_octave_value (void) const { return true; } int is_cell (void) const { return val.is_cell (); } @@ -641,6 +639,8 @@ protected: + octave_value as_octave_value (void) const { return val; } + mxArray_octave_value (const mxArray_octave_value& arg) : mxArray_base (arg), val (arg.val), mutate_flag (arg.mutate_flag), id (arg.id), class_name (strsave (arg.class_name)), ndims (arg.ndims), @@ -1123,6 +1123,59 @@ mxFree (pi); } + int is_complex (void) const { return pi != 0; } + + void *get_data (void) const { return pr; } + + void *get_imag_data (void) const { return pi; } + + void set_data (void *pr_arg) { pr = pr_arg; } + + void set_imag_data (void *pi_arg) { pi = pi_arg; } + + int get_string (char *buf, int buflen) const + { + int retval = 1; + + int n = get_number_of_elements (); + + if (n < buflen) + { + mxChar *ptr = static_cast<mxChar *> (pr); + + for (int i = 0; i < n; i++) + buf[i] = static_cast<char> (ptr[i]); + + buf[n] = 0; + } + + return retval; + } + + char *array_to_string (void) const + { + // FIXME -- this is suposed to handle multi-byte character + // strings. + + int nel = get_number_of_elements (); + + char *buf = static_cast<char *> (malloc (nel + 1)); + + if (buf) + { + mxChar *ptr = static_cast<mxChar *> (pr); + + for (int i = 0; i < nel; i++) + buf[i] = static_cast<char> (ptr[i]); + + buf[nel] = '\0'; + } + + return buf; + } + +protected: + template <typename ELT_T, typename ARRAY_T, typename ARRAY_ELT_T> octave_value int_to_ov (const dim_vector& dv) const @@ -1255,70 +1308,18 @@ return retval; } - int is_complex (void) const { return pi != 0; } - - void *get_data (void) const { return pr; } - - void *get_imag_data (void) const { return pi; } - - void set_data (void *pr_arg) { pr = pr_arg; } - - void set_imag_data (void *pi_arg) { pi = pi_arg; } - - int get_string (char *buf, int buflen) const - { - int retval = 1; - - int n = get_number_of_elements (); - - if (n < buflen) - { - mxChar *ptr = static_cast<mxChar *> (pr); - - for (int i = 0; i < n; i++) - buf[i] = static_cast<char> (ptr[i]); - - buf[n] = 0; - } - - return retval; - } - - char *array_to_string (void) const - { - // FIXME -- this is suposed to handle multi-byte character - // strings. - - int nel = get_number_of_elements (); - - char *buf = static_cast<char *> (malloc (nel + 1)); - - if (buf) - { - mxChar *ptr = static_cast<mxChar *> (pr); - - for (int i = 0; i < nel; i++) - buf[i] = static_cast<char> (ptr[i]); - - buf[nel] = '\0'; - } - - return buf; - } - -protected: - mxArray_number (const mxArray_number& val) : mxArray_matlab (val), pr (malloc (get_number_of_elements () * get_element_size ())), pi (val.pi ? malloc (get_number_of_elements () * get_element_size ()) : 0) { - int ntot = get_number_of_elements () * get_element_size (); - - memcpy (pr, val.pr, ntot); + size_t nbytes = get_number_of_elements () * get_element_size (); + + if (pr) + memcpy (pr, val.pr, nbytes); if (pi) - memcpy (pi, val.pi, ntot); + memcpy (pi, val.pi, nbytes); } private: @@ -1353,6 +1354,32 @@ mxFree (jc); } + int is_complex (void) const { return pi != 0; } + + int is_sparse (void) const { return 1; } + + void *get_data (void) const { return pr; } + + void *get_imag_data (void) const { return pi; } + + void set_data (void *pr_arg) { pr = pr_arg; } + + void set_imag_data (void *pi_arg) { pi = pi_arg; } + + int *get_ir (void) const { return ir; } + + int *get_jc (void) const { return jc; } + + int get_nzmax (void) const { return nzmax; } + + void set_ir (int *ir_arg) { ir = ir_arg; } + + void set_jc (int *jc_arg) { jc = jc_arg; } + + void set_nzmax (int nzmax_arg) { nzmax = nzmax_arg; } + +protected: + octave_value as_octave_value (void) const { octave_value retval; @@ -1431,30 +1458,6 @@ return retval; } - int is_complex (void) const { return pi != 0; } - - int is_sparse (void) const { return 1; } - - void *get_data (void) const { return pr; } - - void *get_imag_data (void) const { return pi; } - - void set_data (void *pr_arg) { pr = pr_arg; } - - void set_imag_data (void *pi_arg) { pi = pi_arg; } - - int *get_ir (void) const { return ir; } - - int *get_jc (void) const { return jc; } - - int get_nzmax (void) const { return nzmax; } - - void set_ir (int *ir_arg) { ir = ir_arg; } - - void set_jc (int *jc_arg) { jc = jc_arg; } - - void set_nzmax (int nzmax_arg) { nzmax = nzmax_arg; } - private: int nzmax; @@ -1469,13 +1472,19 @@ ir (static_cast<int *> (malloc (nzmax * sizeof (int)))), jc (static_cast<int *> (malloc (nzmax * sizeof (int)))) { - int ntot = nzmax * get_element_size (); - - memcpy (pr, val.pr, ntot); - memcpy (ir, val.ir, nzmax * sizeof(int)); - memcpy (jc, val.jc, (val.get_n () + 1) * sizeof (int)); + size_t nbytes = nzmax * get_element_size (); + + if (pr) + memcpy (pr, val.pr, nbytes); + if (pi) - memcpy (pi, val.pi, ntot); + memcpy (pi, val.pi, nbytes); + + if (ir) + memcpy (ir, val.ir, nzmax * sizeof (int)); + + if (jc) + memcpy (jc, val.jc, (val.get_n () + 1) * sizeof (int)); } }; @@ -1533,35 +1542,6 @@ mxFree (data); } - octave_value as_octave_value (void) const - { - dim_vector dv = dims_to_dim_vector (); - - string_vector keys (fields, nfields); - - Octave_map m; - - int ntot = nfields * get_number_of_elements (); - - for (int i = 0; i < nfields; i++) - { - Cell c (dv); - - octave_value *p = c.fortran_vec (); - - int k = 0; - for (int j = i; j < ntot; j += nfields) - { - mxArray *t = data[j]; - p[k++] = t ? t->as_octave_value () : octave_value (Matrix ()); - } - - m.assign (keys[i], c); - } - - return m; - } - int add_field (const char *key) { int retval = -1; @@ -1700,6 +1680,34 @@ void set_data (void *data_arg) { data = static_cast<mxArray **> (data_arg); } +protected: + + octave_value as_octave_value (void) const + { + dim_vector dv = dims_to_dim_vector (); + + string_vector keys (fields, nfields); + + Octave_map m; + + int ntot = nfields * get_number_of_elements (); + + for (int i = 0; i < nfields; i++) + { + Cell c (dv); + + octave_value *p = c.fortran_vec (); + + int k = 0; + for (int j = i; j < ntot; j += nfields) + p[k++] = mxArray::as_octave_value (data[j]); + + m.assign (keys[i], c); + } + + return m; + } + private: int nfields; @@ -1753,6 +1761,16 @@ mxFree (data); } + mxArray *get_cell (int idx) const { return data[idx]; } + + void set_cell (int idx, mxArray *val) { data[idx] = val; } + + void *get_data (void) const { return data; } + + void set_data (void *data_arg) { data = static_cast<mxArray **> (data_arg); } + +protected: + octave_value as_octave_value (void) const { dim_vector dv = dims_to_dim_vector (); @@ -1764,22 +1782,11 @@ octave_value *p = c.fortran_vec (); for (int i = 0; i < nel; i++) - { - mxArray *t = data[i]; - p[i] = t ? t->as_octave_value () : octave_value (Matrix ()); - } + p[i] = mxArray::as_octave_value (data[i]); return c; } - mxArray *get_cell (int idx) const { return data[idx]; } - - void set_cell (int idx, mxArray *val) { data[idx] = val; } - - void *get_data (void) const { return data; } - - void set_data (void *data_arg) { data = static_cast<mxArray **> (data_arg); } - private: mxArray **data; @@ -1849,12 +1856,6 @@ delete rep; } -octave_value -mxArray::as_octave_value (void) const -{ - return rep->as_octave_value (); -} - void mxArray::set_name (const char *name_arg) { @@ -1862,6 +1863,18 @@ name = strsave (name_arg); } +octave_value +mxArray::as_octave_value (mxArray *ptr) +{ + return ptr ? ptr->as_octave_value () : octave_value (Matrix ()); +} + +octave_value +mxArray::as_octave_value (void) const +{ + return rep->as_octave_value (); +} + void mxArray::maybe_mutate (void) const { @@ -2855,13 +2868,7 @@ retval.resize (nargout); for (int i = 0; i < nargout; i++) - { - if (argout[i]) - { - mxArray *t = argout[i]; - retval(i) = t ? t->as_octave_value () : octave_value (Matrix ()); - } - } + retval(i) = mxArray::as_octave_value (argout[i]); } // Clean up mex resources. @@ -2905,10 +2912,7 @@ args.resize (nargin); for (int i = 0; i < nargin; i++) - { - mxArray *t = argin[i]; - args(i) = t ? t->as_octave_value () : octave_value (Matrix ()); - } + args(i) = mxArray::as_octave_value (argin[i]); octave_value_list retval = feval (fname, args, nargout); @@ -3082,7 +3086,7 @@ return 1; if (! strcmp (space, "global")) - set_global_value (name, ptr->as_octave_value ()); + set_global_value (name, mxArray::as_octave_value (ptr)); else { // FIXME -- this belongs in variables.cc. @@ -3097,7 +3101,7 @@ mexErrMsgTxt ("mexPutVariable: symbol table does not exist"); if (sr) - sr->define (ptr->as_octave_value ()); + sr->define (mxArray::as_octave_value (ptr)); else panic_impossible (); }
--- a/src/mxarray.h +++ b/src/mxarray.h @@ -162,8 +162,6 @@ virtual ~mxArray (void); - virtual octave_value as_octave_value (void) const; - void mark_persistent (void) const { persistent = true; } void unmark_persistent (void) const { persistent = false; } @@ -308,8 +306,12 @@ return retval; } + static octave_value as_octave_value (mxArray *ptr); + protected: + virtual octave_value as_octave_value (void) const; + mxArray (const xmxArray&) : rep (0), name (0), persistent (false) { } private: