octave-maintainers
[Top][All Lists]
Advanced

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

Re: Pointers data type in Octave to handle data structures


From: Paul Kienzle
Subject: Re: Pointers data type in Octave to handle data structures
Date: Wed, 22 Jun 2005 20:51:25 -0400

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 via email to

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