Pointers data type in Octave to handle data structures

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Pointers data type in Octave to handle data structures

Guillaume Schatz
Hi,

I want to implement a set of functions to perform data acquisitions
using my own hardware. I want to use handle to have a reference to my
hardware like the following instructions in Matlab

handle = analogInput('nidaq','1');
...
data = getData(handle);

Do you know how to use pointers in octave C++ files such as

DEFUN_DLD(getData,args, ,"...")
{
    handleType *myHandle;
    myHandle = args(0).???
    ...
    ...
    ...
    pthread_mutex_lock(&(myHandle->myMutex));
    ...
}

I really need a pointer to this structure to manage my different threads.

Thank you for your help

Guillaume

Reply | Threaded
Open this post in threaded view
|

Re: Pointers data type in Octave to handle data structures

Søren Hauberg
Hi,
I think you should have asked at [hidden email], but if I understand
you correctly you should have a look at

http://wiki.octave.org/wiki.pl?CodaTypes

for a tutorial on how to create a new type in Octave.

/Søren

Guillaume Schatz wrote:

> Hi,
>
> I want to implement a set of functions to perform data acquisitions
> using my own hardware. I want to use handle to have a reference to my
> hardware like the following instructions in Matlab
>
> handle = analogInput('nidaq','1');
> ...
> data = getData(handle);
>
> Do you know how to use pointers in octave C++ files such as
>
> DEFUN_DLD(getData,args, ,"...")
> {
>    handleType *myHandle;
>    myHandle = args(0).???
>    ...
>    ...
>    ...
>    pthread_mutex_lock(&(myHandle->myMutex));
>    ...
> }
>
> I really need a pointer to this structure to manage my different threads.
>
> Thank you for your help
>
> Guillaume
>

Reply | Threaded
Open this post in threaded view
|

Re: Pointers data type in Octave to handle data structures

Guillaume Schatz
Ok, thank's for the information.
Guillaume

Søren Hauberg wrote:

> Hi,
> I think you should have asked at [hidden email], but if I understand
> you correctly you should have a look at
>
> http://wiki.octave.org/wiki.pl?CodaTypes
>
> for a tutorial on how to create a new type in Octave.
>
> /Søren
>
> Guillaume Schatz wrote:
>
>> Hi,
>>
>> I want to implement a set of functions to perform data acquisitions
>> using my own hardware. I want to use handle to have a reference to my
>> hardware like the following instructions in Matlab
>>
>> handle = analogInput('nidaq','1');
>> ...
>> data = getData(handle);
>>
>> Do you know how to use pointers in octave C++ files such as
>>
>> DEFUN_DLD(getData,args, ,"...")
>> {
>>    handleType *myHandle;
>>    myHandle = args(0).???
>>    ...
>>    ...
>>    ...
>>    pthread_mutex_lock(&(myHandle->myMutex));
>>    ...
>> }
>>
>> I really need a pointer to this structure to manage my different
>> threads.
>>
>> Thank you for your help
>>
>> Guillaume
>>
>
>

--
Guillaume Schatz
ingénieur Ra&D
Ecole d'ingénieurs du Canton de Vaud
eMbedded Information Systems institute - MIS
Rue Galilée 15
CH - 1400 Yverdon-les-Bains

Phone: +41 244 239 753
Mail : [hidden email]
Web  : www.eivd.ch/mis



Reply | Threaded
Open this post in threaded view
|

Re: Pointers data type in Octave to handle data structures

Paul Kienzle
In reply to this post by Guillaume Schatz
Guillaume,

The lazy way is to simply return an int big enough to hold the pointer:

        if (sizeof(octave_int32_t) >= sizeof(void*))
          return octave_value(static_cast<octave_int32_t>(ptr))
     else
          return octave_value(static_cast<octave_int64_t>(ptr))

You can recast it as a pointer when you get it back:

        void *ptr;
     if (sizeof(octave_int32_t) >= sizeof(void *))
           ptr = static_cast<void *>(args(i).int32_scalar_value());
        else
           ptr = static_cast<void *>(args(i).int64_scalar_value());

Rather than returning a pointer you could return an integer and keep a
global map to associate handles with the pointers:

        std::map<int,void*> handles;

Return the integer handle as a double:

     return octave_value(double(h));

This way you can do proper checking for valid values of the handle and
avoid crashing the interpreter if the user calls your function
incorrectly.

Either way you will be able to have arrays of handles without a lot of
work on your part.

- Paul

On Jun 22, 2005, at 7:44 AM, Guillaume Schatz wrote:

> Hi,
>
> I want to implement a set of functions to perform data acquisitions
> using my own hardware. I want to use handle to have a reference to my
> hardware like the following instructions in Matlab
>
> handle = analogInput('nidaq','1');
> ...
> data = getData(handle);
>
> Do you know how to use pointers in octave C++ files such as
>
> DEFUN_DLD(getData,args, ,"...")
> {
>    handleType *myHandle;
>    myHandle = args(0).???
>    ...
>    ...
>    ...
>    pthread_mutex_lock(&(myHandle->myMutex));
>    ...
> }
>
> I really need a pointer to this structure to manage my different
> threads.
>
> Thank you for your help
>
> Guillaume
>

Reply | Threaded
Open this post in threaded view
|

Re: Pointers data type in Octave to handle data structures

Stéfan van der Walt
On Wed, Jun 22, 2005 at 08:51:25PM -0400, Paul Kienzle wrote:
> Guillaume,
>
> The lazy way is to simply return an int big enough to hold the pointer:
>
> if (sizeof(octave_int32_t) >= sizeof(void*))
>  return octave_value(static_cast<octave_int32_t>(ptr))
>     else
>  return octave_value(static_cast<octave_int64_t>(ptr))

I ask this in complete ignorance, but how does one guarantee that the
pointer data (ptr*) isn't re-allocated or overwritten by another part
of Octave or some other dynamic function?

Regards
Stéfan

Reply | Threaded
Open this post in threaded view
|

Re: Pointers data type in Octave to handle data structures

Paul Kienzle

On Jun 23, 2005, at 3:16 AM, Stefan van der Walt wrote:

> On Wed, Jun 22, 2005 at 08:51:25PM -0400, Paul Kienzle wrote:
>> Guillaume,
>>
>> The lazy way is to simply return an int big enough to hold the
>> pointer:
>>
>> if (sizeof(octave_int32_t) >= sizeof(void*))
>>  return octave_value(static_cast<octave_int32_t>(ptr))
>>     else
>>  return octave_value(static_cast<octave_int64_t>(ptr))
>
> I ask this in complete ignorance, but how does one guarantee that the
> pointer data (ptr*) isn't re-allocated or overwritten by another part
> of Octave or some other dynamic function?

A new pointer will be good until you delete it, or in C, a malloc'd
pointer will be good until you free it.

- Paul