Wrapper to use a C++ class in Octave

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Wrapper to use a C++ class in Octave

Stefan Oltmanns
Hello,

I've got a C++ class I would like to use inside Octave.

This is how I use the class inside a C++ program:

MyClass *abc;
abc = new MyClass();
abc->SomeFunction(parameter,callbackfunction,callbackdata);
...

I assume in Octave this could look like:

myclass = CreateMyClass();
MyClassFunction(myclass,parameter,@callbackfunction,callbackdata);

The class is designed for non blocking operation and everything is done
by a background thread (when result is ready the callback function is
called).

Is there a simple way to write a wrapper around the class? Is there any
example where something like this has been done?

Best regards,
Stefan

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wrapper to use a C++ class in Octave

Mike Miller-4
On Wed, Aug 09, 2017 at 23:40:49 +0200, Stefan Oltmanns wrote:
> The class is designed for non blocking operation and everything is done
> by a background thread (when result is ready the callback function is
> called).
>
> Is there a simple way to write a wrapper around the class? Is there any
> example where something like this has been done?

This is certainly doable and has been done before. I can't say if you'll
call it simple. Take a look at Octave's own audioplayer and
audiorecorder classes, which have options to call user functions when
the audio driver needs data or has data ready.

  https://hg.savannah.gnu.org/hgweb/octave/file/@/libinterp/dldfcn/audiodevinfo.cc

As a brief illustration, you would define a shim callback function that
can call a stored reference to an Octave function:

    static octave_function *octave_callback = nullptr;

    void real_callback_function (void)
    {
      if (octave_callback)
        feval (octave_callback, ovl (), 0);
    }

You would also want to translate any arguments or return values, there
are plenty of examples of that also

      octave_value_list args;
      args(0) = // translate from real_callback_function arguments
      args(1) = // ...

      octave_value_list out = feval (octave_callback, args, nout);
      // do something with return values out(0), out(1), etc.

--
mike

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wrapper to use a C++ class in Octave

Carlo de Falco-2

> On 10 Aug 2017, at 19:21, Mike Miller <[hidden email]> wrote:
>
> On Wed, Aug 09, 2017 at 23:40:49 +0200, Stefan Oltmanns wrote:
>> The class is designed for non blocking operation and everything is done
>> by a background thread (when result is ready the callback function is
>> called).
>>
>> Is there a simple way to write a wrapper around the class? Is there any
>> example where something like this has been done?
>
> This is certainly doable and has been done before. I can't say if you'll
> call it simple. Take a look at Octave's own audioplayer and
> audiorecorder classes, which have options to call user functions when
> the audio driver needs data or has data ready.
>
>  https://hg.savannah.gnu.org/hgweb/octave/file/@/libinterp/dldfcn/audiodevinfo.cc

Another example of this in Octave sources could be the implementation of ode15i
as a wrapper to the sundials IDA library:

  http://hg.savannah.gnu.org/hgweb/octave/file/c3cfd3115b78/libinterp/dldfcn/__ode15__.cc

The most simple example of storing and calling an Octave function from a C++ program
is in the examples ditributed with Octave sources:

  http://hg.savannah.gnu.org/hgweb/octave/file/c3cfd3115b78/examples/code/funcdemo.cc

HTH,
c.




_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wrapper to use a C++ class in Octave

Stefan Oltmanns
Am 12.08.2017 um 17:23 schrieb Carlo De Falco:

>
>> On 10 Aug 2017, at 19:21, Mike Miller <[hidden email]> wrote:
>>
>> On Wed, Aug 09, 2017 at 23:40:49 +0200, Stefan Oltmanns wrote:
>>> The class is designed for non blocking operation and everything is done
>>> by a background thread (when result is ready the callback function is
>>> called).
>>>
>>> Is there a simple way to write a wrapper around the class? Is there any
>>> example where something like this has been done?
>>
>> This is certainly doable and has been done before. I can't say if you'll
>> call it simple. Take a look at Octave's own audioplayer and
>> audiorecorder classes, which have options to call user functions when
>> the audio driver needs data or has data ready.
>>
>>  https://hg.savannah.gnu.org/hgweb/octave/file/@/libinterp/dldfcn/audiodevinfo.cc
>
> Another example of this in Octave sources could be the implementation of ode15i
> as a wrapper to the sundials IDA library:
>
>   http://hg.savannah.gnu.org/hgweb/octave/file/c3cfd3115b78/libinterp/dldfcn/__ode15__.cc
>
> The most simple example of storing and calling an Octave function from a C++ program
> is in the examples ditributed with Octave sources:
>
>   http://hg.savannah.gnu.org/hgweb/octave/file/c3cfd3115b78/examples/code/funcdemo.cc
>
> HTH,
> c.
>

I think there is one major problem: The class is multithreaded and the
callbacks are called from another thread. According to the infos I found
Octave is not thread-safe, so the callbacks have to be handled inside
the C++ code, right?

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wrapper to use a C++ class in Octave

Mike Miller-4
On Sat, Aug 12, 2017 at 17:34:47 +0200, Stefan Oltmanns wrote:
> I think there is one major problem: The class is multithreaded and the
> callbacks are called from another thread. According to the infos I found
> Octave is not thread-safe, so the callbacks have to be handled inside
> the C++ code, right?

I think that depends on what you intend to do within the callback
functions.

--
mike

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

signature.asc (849 bytes) Download Attachment
Loading...