changeset 29869:5411391a4cc0

allow typed mx get data functions to work without mutation in more cases * mex.cc (GET_DATA_METHOD): New macro. (mxArray_octave_value::get_data<T>): Don't request mutation if object is real or if using interleaved complex data. (mxArray_octave_value::get_doubles, mxArray_octave_value::get_singles, mxArray_octave_value::get_int8s, mxArray_octave_value::get_int16s, mxArray_octave_value::get_int32s, mxArray_octave_value::get_int64s, mxArray_octave_value::get_uint8s, mxArray_octave_value::get_uint16s, mxArray_octave_value::get_uint32s, mxArray_octave_value::get_uint64s, mxArray_octave_value::get_complex_doubles, mxArray_octave_value::get_complex_singles, mxArray_octave_value::get_complex_int8s, mxArray_octave_value::get_complex_int16s, mxArray_octave_value::get_complex_int32s, mxArray_octave_value::get_complex_int64s, mxArray_octave_value::get_complex_uint8s, mxArray_octave_value::get_complex_uint16s, mxArray_octave_value::get_complex_uint32s, mxArray_octave_value::get_complex_uint64s): Define using GET_DATA_METHOD macro. * ov-base-mat.h (octave_base_matrix<T>::mex_get_data): New function. * ov-bool-mat.h (octave_bool_matrix::mex_get_data): Delete. * ov-ch-mat.h (octave_char_matrix::mex_get_data): Delete. * ov-flt-re-mat.h (octave_float_matrix::mex_get_data): Delete. * ov-re-mat.h (octave_matrix::mex_get_data): Delete. * ov-intx.h (octave_base_int_matrix<T>::mex_get_data, octave_base_int_scalar<T>::mex_get_data): Delete.
author John W. Eaton <jwe@octave.org>
date Mon, 12 Jul 2021 16:22:33 -0400
parents 6829836f86c2
children 79fe39e852db
files libinterp/corefcn/mex.cc libinterp/octave-value/ov-base-mat.h libinterp/octave-value/ov-bool-mat.h libinterp/octave-value/ov-ch-mat.h libinterp/octave-value/ov-flt-re-mat.h libinterp/octave-value/ov-intx.h libinterp/octave-value/ov-re-mat.h
diffstat 7 files changed, 53 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/mex.cc	Mon Jul 12 00:41:18 2021 -0400
+++ b/libinterp/corefcn/mex.cc	Mon Jul 12 16:22:33 2021 -0400
@@ -364,6 +364,9 @@
 #define CONST_MUTATION_METHOD(RET_TYPE, FCN_NAME, ARG_LIST, RET_VAL)    \
   RET_TYPE FCN_NAME ARG_LIST const { request_mutation (); return RET_VAL; }
 
+#define GET_DATA_METHOD(RT, FCN_NAME, ID, COMPLEXITY)                   \
+  RT * FCN_NAME (void) const { return get_data<RT> (ID, COMPLEXITY); }
+
 class mxArray_octave_value : public mxArray_base
 {
 public:
@@ -657,54 +660,57 @@
   {
     T *retval = static_cast<T *> (val.mex_get_data (class_id, complexity));
 
-    if (retval)
-      maybe_mark_foreign (retval);
-    else
-      request_mutation ();
-
-    return retval;
+    if (retval && (complexity == mxREAL || m_interleaved))
+      {
+        maybe_mark_foreign (retval);
+        return retval;
+      }
+
+    request_mutation ();
+    return nullptr;
   }
 
-  CONST_MUTATION_METHOD (mxDouble *, get_doubles, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxSingle *, get_singles, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxInt8 *, get_int8s, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxInt16 *, get_int16s, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxInt32 *, get_int32s, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxInt64 *, get_int64s, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxUint8 *, get_uint8s, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxUint16 *, get_uint16s, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxUint32 *, get_uint32s, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxUint64 *, get_uint64s, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxComplexDouble *, get_complex_doubles, (void), nullptr);
-  CONST_MUTATION_METHOD (mxComplexSingle *, get_complex_singles, (void), nullptr);
+  GET_DATA_METHOD (mxDouble, get_doubles, mxDOUBLE_CLASS, mxREAL);
+
+  GET_DATA_METHOD (mxSingle, get_singles, mxSINGLE_CLASS, mxREAL);
+
+  GET_DATA_METHOD (mxInt8, get_int8s, mxINT8_CLASS, mxREAL);
+
+  GET_DATA_METHOD (mxInt16, get_int16s, mxINT16_CLASS, mxREAL);
+
+  GET_DATA_METHOD (mxInt32, get_int32s, mxINT32_CLASS, mxREAL);
+
+  GET_DATA_METHOD (mxInt64, get_int64s, mxINT64_CLASS, mxREAL);
+
+  GET_DATA_METHOD (mxUint8, get_uint8s, mxUINT8_CLASS, mxREAL);
+
+  GET_DATA_METHOD (mxUint16, get_uint16s, mxUINT16_CLASS, mxREAL);
+
+  GET_DATA_METHOD (mxUint32, get_uint32s, mxUINT32_CLASS, mxREAL);
+
+  GET_DATA_METHOD (mxUint64, get_uint64s, mxUINT64_CLASS, mxREAL);
+
+  GET_DATA_METHOD (mxComplexDouble, get_complex_doubles, mxDOUBLE_CLASS, mxCOMPLEX);
+
+  GET_DATA_METHOD (mxComplexSingle, get_complex_singles, mxDOUBLE_CLASS, mxCOMPLEX);
 
 #if 0
   /* We don't have these yet. */
-  CONST_MUTATION_METHOD (mxComplexInt8 *, get_complex_int8s, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxComplexInt16 *, get_complex_int16s, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxComplexInt32 *, get_complex_int32s, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxComplexInt64 *, get_complex_int64s, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxComplexUint8 *, get_complex_uint8s, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxComplexUint16 *, get_complex_uint16s, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxComplexUint32 *, get_complex_uint32s, (void), nullptr);
-
-  CONST_MUTATION_METHOD (mxComplexUint64 *, get_complex_uint64s, (void), nullptr);
+  GET_DATA_METHOD (mxComplexInt8 *, get_complex_int8s, (void), nullptr);
+
+  GET_DATA_METHOD (mxComplexInt16 *, get_complex_int16s, (void), nullptr);
+
+  GET_DATA_METHOD (mxComplexInt32 *, get_complex_int32s, (void), nullptr);
+
+  GET_DATA_METHOD (mxComplexInt64 *, get_complex_int64s, (void), nullptr);
+
+  GET_DATA_METHOD (mxComplexUint8 *, get_complex_uint8s, (void), nullptr);
+
+  GET_DATA_METHOD (mxComplexUint16 *, get_complex_uint16s, (void), nullptr);
+
+  GET_DATA_METHOD (mxComplexUint32 *, get_complex_uint32s, (void), nullptr);
+
+  GET_DATA_METHOD (mxComplexUint64 *, get_complex_uint64s, (void), nullptr);
 #endif
 
   void * get_imag_data (void) const
--- a/libinterp/octave-value/ov-base-mat.h	Mon Jul 12 00:41:18 2021 -0400
+++ b/libinterp/octave-value/ov-base-mat.h	Mon Jul 12 16:22:33 2021 -0400
@@ -198,6 +198,10 @@
   OCTINTERP_API bool
   fast_elem_insert (octave_idx_type n, const octave_value& x);
 
+  // Unsafe.  This function exists to support the MEX interface.
+  // You should not use it anywhere else.
+  void * mex_get_data (void) const { return matrix.mex_get_data (); }
+
 protected:
 
   MT matrix;
--- a/libinterp/octave-value/ov-bool-mat.h	Mon Jul 12 00:41:18 2021 -0400
+++ b/libinterp/octave-value/ov-bool-mat.h	Mon Jul 12 16:22:33 2021 -0400
@@ -229,10 +229,6 @@
              octave::mach_info::float_format flt_fmt) const
   { return os.write (matrix, block_size, output_type, skip, flt_fmt); }
 
-  // Unsafe.  This function exists to support the MEX interface.
-  // You should not use it anywhere else.
-  void * mex_get_data (void) const { return matrix.mex_get_data (); }
-
   mxArray * as_mxArray (bool interleaved) const;
 
   // Mapper functions are converted to double for treatment
--- a/libinterp/octave-value/ov-ch-mat.h	Mon Jul 12 00:41:18 2021 -0400
+++ b/libinterp/octave-value/ov-ch-mat.h	Mon Jul 12 16:22:33 2021 -0400
@@ -163,10 +163,6 @@
 
   void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
 
-  // Unsafe.  This function exists to support the MEX interface.
-  // You should not use it anywhere else.
-  void * mex_get_data (void) const { return matrix.mex_get_data (); }
-
   mxArray * as_mxArray (bool interleaved) const;
 
   octave_value map (unary_mapper_t umap) const;
--- a/libinterp/octave-value/ov-flt-re-mat.h	Mon Jul 12 00:41:18 2021 -0400
+++ b/libinterp/octave-value/ov-flt-re-mat.h	Mon Jul 12 16:22:33 2021 -0400
@@ -213,10 +213,6 @@
              octave::mach_info::float_format flt_fmt) const
   { return os.write (matrix, block_size, output_type, skip, flt_fmt); }
 
-  // Unsafe.  This function exists to support the MEX interface.
-  // You should not use it anywhere else.
-  void * mex_get_data (void) const { return matrix.mex_get_data (); }
-
   mxArray * as_mxArray (bool interleaved) const;
 
   octave_value map (unary_mapper_t umap) const;
--- a/libinterp/octave-value/ov-intx.h	Mon Jul 12 00:41:18 2021 -0400
+++ b/libinterp/octave-value/ov-intx.h	Mon Jul 12 16:22:33 2021 -0400
@@ -311,10 +311,6 @@
              octave::mach_info::float_format flt_fmt) const
   { return os.write (matrix, block_size, output_type, skip, flt_fmt); }
 
-  // Unsafe.  This function exists to support the MEX interface.
-  // You should not use it anywhere else.
-  void * mex_get_data (void) const { return matrix.mex_get_data (); }
-
   mxArray * as_mxArray (bool interleaved) const
   {
     mxArray *retval = new mxArray (interleaved, OCTAVE_INT_MX_CLASS, dims (),
@@ -635,10 +631,6 @@
                      block_size, output_type, skip, flt_fmt);
   }
 
-  // Unsafe.  This function exists to support the MEX interface.
-  // You should not use it anywhere else.
-  void * mex_get_data (void) const { return scalar.mex_get_data (); }
-
   mxArray * as_mxArray (bool interleaved) const
   {
     mxArray *retval = new mxArray (interleaved, OCTAVE_INT_MX_CLASS, 1, 1,
--- a/libinterp/octave-value/ov-re-mat.h	Mon Jul 12 00:41:18 2021 -0400
+++ b/libinterp/octave-value/ov-re-mat.h	Mon Jul 12 16:22:33 2021 -0400
@@ -237,10 +237,6 @@
              octave::mach_info::float_format flt_fmt) const
   { return os.write (matrix, block_size, output_type, skip, flt_fmt); }
 
-  // Unsafe.  This function exists to support the MEX interface.
-  // You should not use it anywhere else.
-  void * mex_get_data (void) const { return matrix.mex_get_data (); }
-
   mxArray * as_mxArray (bool interleaved) const;
 
   octave_value map (unary_mapper_t umap) const;