help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Oct files: How to get read-only pointers to data for arguments of unknow


From: Stefan
Subject: Oct files: How to get read-only pointers to data for arguments of unknown type?
Date: Mon, 27 Feb 2012 10:55:17 -0800 (PST)

Hello,

I am trying to write an octave .oct wrapper to a C library that accepts any
number of inputs of any type. The library uses a simple descriptor
structure, which contains a const void* to the data. Simple and powerful.
Writing the wrapper consists of assembling the descriptor structure from the
octave_value_list "args", including the const void* to the data. Since the
library will not modify the data, which can be huge, there should be no
copying.

I have wrapped this library before in Matlab mex, where void
*mxGetData(const mxArray *) simply does the job.

There appears to be no such method in the .oct API. Instead, there are
specialized conversion constructors for each data type, which are quite
aggressively trying to copy the data. So here is my first attempt to attach
the const void* to the descriptor "D" without any copying:

if (args(0).is_int32_type()) {
    const int32NDArray tmp = args(0).int32_array_value();
    mkDescrip_data(D, tmp.data());
} else if (...) {
...
} else if (args(0).is_double_type()) {
    const NDArray tmp = args(0).array_value();
    mkDescrip_data(D, tmp.data());
}
call_library(&D);

This doesn't work. Since tmp was defined inside the scope of the switchyard,
the array_value() methods have made a copy of the data and returned a
pointer to the copy, which got already destroyed before I reach
call_library(&D). Great.

If I don't assign to a const NDArray object, array_value() makes a copy in
any case.

I have thought about this kind of hack:

// get a const object for all possible data types (this compiles)
const int32NDArray tmp32 = args(0).int32_array_value();
...
const NDArray tmpd = args(0).array_value();

if (args(0).is_int32_type()) {
    mkDescrip_data(D, tmp32.data());
} else if (...) {
...
} else if (args(0).is_double_type()) {
    mkDescrip_data(D, tmpd.data());
}

call_library(&D);

This ugly hack would work if I only had one argument from octave, but I also
have args(1), args(2), ... , args(n), all to be assigned before
call_library(&D).

I hope that I am missing something in the octave API. Or is there some C++
trick, such as working with the parent class, or deriving a class that adds
the needed functionality?

How can one do what mxGetData() does without going completely overboard in
the oct API?

Thanks for your ideas,

Stefan



--
View this message in context: 
http://octave.1599824.n4.nabble.com/Oct-files-How-to-get-read-only-pointers-to-data-for-arguments-of-unknown-type-tp4425650p4425650.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]