Discussion:
[Swig-devel] Octave/Matlab wrapper
Richard Crozier
2016-08-08 11:18:05 UTC
Permalink
Dear SWIG team,

Currently swig has a C++ wrapper for Octave, but not one for Matlab. The
Octave wrapper requires knowledge and access to the Octave underlying
C++ implementation, which is why it does not work for Matlab (at least
that's what I think I read somewhere). I have a suggestion for an
alternative method of wrapping C++ objects that would work unchanged for
both Matlab and Octave and be a much more natural way of wrapping C++
objects using the object orientated features now available in both
systems (the 'classdef' syntax).

Matlab, and now Octave support Object-Orientated features through classdef:

http://uk.mathworks.com/help/matlab/ref/classdef.html

Classes can derive from the 'handle' class which means they are a
reference to an object:

http://uk.mathworks.com/help/matlab/ref/handle-class.html

If you copy an object's handle variable, MATLAB® copies only the handle.
Both the original and copy refer to the same object. For example, if a
function modifies a handle object passed as an input argument, the
modification affects the original input object.

This opens up the possibility of creating a classdef wrapper around a
C++ class by holding a pointer to the C++ object in memory in the handle
class properties and ensuring it is destroyed when the Matlab class is
destroyed.

This has already been implemented and posted on the Matlab file exchange
here:

http://uk.mathworks.com/matlabcentral/fileexchange/38964-example-matlab-class-wrapper-for-a-c++-class

I have taken this a bit further and created macros for and a fuller
example in my own github repo here:

https://github.com/crobarcro/mcode-cpp-utilities

This also has examples of having the underlying classes interact as they
can have access to each other's pointers.

Someone else has also taken the idea and run with it here:

https://github.com/kyamagu/mexplus

As I said, this also has the benefit of creating a much more 'natural'
way of interacting with C++ classes, so I wondered if there would be any
interest is developing a SWIG wrapper based on this?

Unfortunately I don't know much about SWIG, so wouldn't be an awful lot
of help here, but if there was any interest, could at least help with
testing.

Best regards,

Richard

------------------------------------------------------------------------------
Ian Bell
2016-08-08 14:39:32 UTC
Permalink
Are you aware of the work that is happening on the MATLAB swig branch:
https://github.com/jaeandersson/swig ? They've resolved most of the hard
problems.
Post by Richard Crozier
Dear SWIG team,
Currently swig has a C++ wrapper for Octave, but not one for Matlab. The
Octave wrapper requires knowledge and access to the Octave underlying
C++ implementation, which is why it does not work for Matlab (at least
that's what I think I read somewhere). I have a suggestion for an
alternative method of wrapping C++ objects that would work unchanged for
both Matlab and Octave and be a much more natural way of wrapping C++
objects using the object orientated features now available in both
systems (the 'classdef' syntax).
http://uk.mathworks.com/help/matlab/ref/classdef.html
Classes can derive from the 'handle' class which means they are a
http://uk.mathworks.com/help/matlab/ref/handle-class.html
If you copy an object's handle variable, MATLAB® copies only the handle.
Both the original and copy refer to the same object. For example, if a
function modifies a handle object passed as an input argument, the
modification affects the original input object.
This opens up the possibility of creating a classdef wrapper around a
C++ class by holding a pointer to the C++ object in memory in the handle
class properties and ensuring it is destroyed when the Matlab class is
destroyed.
This has already been implemented and posted on the Matlab file exchange
http://uk.mathworks.com/matlabcentral/fileexchange/
38964-example-matlab-class-wrapper-for-a-c++-class
I have taken this a bit further and created macros for and a fuller
https://github.com/crobarcro/mcode-cpp-utilities
This also has examples of having the underlying classes interact as they
can have access to each other's pointers.
https://github.com/kyamagu/mexplus
As I said, this also has the benefit of creating a much more 'natural'
way of interacting with C++ classes, so I wondered if there would be any
interest is developing a SWIG wrapper based on this?
Unfortunately I don't know much about SWIG, so wouldn't be an awful lot
of help here, but if there was any interest, could at least help with
testing.
Best regards,
Richard
------------------------------------------------------------
------------------
_______________________________________________
Swig-devel mailing list
https://lists.sourceforge.net/lists/listinfo/swig-devel
Joel Andersson
2016-08-08 14:57:37 UTC
Permalink
Dear Richard,

Yeah, the branch Ian linked uses an approach based on classdef, handle
classes and mex. It works for MATLAB and since recently also Octave.

We're already using this branch successfully to generate bindings for our
project and my understanding is that a couple of other projects have used
it as well. It should be pretty close to be merged with SWIG master branch.

Feel free to try it out and contribute.

Best regards,
Joel
Post by Ian Bell
https://github.com/jaeandersson/swig ? They've resolved most of the hard
problems.
On Mon, Aug 8, 2016 at 5:18 AM, Richard Crozier <
Post by Richard Crozier
Dear SWIG team,
Currently swig has a C++ wrapper for Octave, but not one for Matlab. The
Octave wrapper requires knowledge and access to the Octave underlying
C++ implementation, which is why it does not work for Matlab (at least
that's what I think I read somewhere). I have a suggestion for an
alternative method of wrapping C++ objects that would work unchanged for
both Matlab and Octave and be a much more natural way of wrapping C++
objects using the object orientated features now available in both
systems (the 'classdef' syntax).
http://uk.mathworks.com/help/matlab/ref/classdef.html
Classes can derive from the 'handle' class which means they are a
http://uk.mathworks.com/help/matlab/ref/handle-class.html
If you copy an object's handle variable, MATLAB® copies only the handle.
Both the original and copy refer to the same object. For example, if a
function modifies a handle object passed as an input argument, the
modification affects the original input object.
This opens up the possibility of creating a classdef wrapper around a
C++ class by holding a pointer to the C++ object in memory in the handle
class properties and ensuring it is destroyed when the Matlab class is
destroyed.
This has already been implemented and posted on the Matlab file exchange
http://uk.mathworks.com/matlabcentral/fileexchange/38964-
example-matlab-class-wrapper-for-a-c++-class
I have taken this a bit further and created macros for and a fuller
https://github.com/crobarcro/mcode-cpp-utilities
This also has examples of having the underlying classes interact as they
can have access to each other's pointers.
https://github.com/kyamagu/mexplus
As I said, this also has the benefit of creating a much more 'natural'
way of interacting with C++ classes, so I wondered if there would be any
interest is developing a SWIG wrapper based on this?
Unfortunately I don't know much about SWIG, so wouldn't be an awful lot
of help here, but if there was any interest, could at least help with
testing.
Best regards,
Richard
------------------------------------------------------------
------------------
_______________________________________________
Swig-devel mailing list
https://lists.sourceforge.net/lists/listinfo/swig-devel
------------------------------------------------------------
------------------
_______________________________________________
Swig-devel mailing list
https://lists.sourceforge.net/lists/listinfo/swig-devel
Richard Crozier
2016-08-08 15:03:30 UTC
Permalink
I wasn't aware of this, so great! I can hopefully get around to testing
it some time soon. Thanks!

Richard
Post by Joel Andersson
Dear Richard,
Yeah, the branch Ian linked uses an approach based on classdef, handle
classes and mex. It works for MATLAB and since recently also Octave.
We're already using this branch successfully to generate bindings for
our project and my understanding is that a couple of other projects have
used it as well. It should be pretty close to be merged with SWIG master
branch.
Feel free to try it out and contribute.
Best regards,
Joel
Are you aware of the work that is happening on the MATLAB swig
branch: https://github.com/jaeandersson/swig
<https://github.com/jaeandersson/swig> ? They've resolved most of
the hard problems.
On Mon, Aug 8, 2016 at 5:18 AM, Richard Crozier
Dear SWIG team,
Currently swig has a C++ wrapper for Octave, but not one for Matlab. The
Octave wrapper requires knowledge and access to the Octave underlying
C++ implementation, which is why it does not work for Matlab (at least
that's what I think I read somewhere). I have a suggestion for an
alternative method of wrapping C++ objects that would work unchanged for
both Matlab and Octave and be a much more natural way of wrapping C++
objects using the object orientated features now available in both
systems (the 'classdef' syntax).
http://uk.mathworks.com/help/matlab/ref/classdef.html
<http://uk.mathworks.com/help/matlab/ref/classdef.html>
Classes can derive from the 'handle' class which means they are a
http://uk.mathworks.com/help/matlab/ref/handle-class.html
<http://uk.mathworks.com/help/matlab/ref/handle-class.html>
If you copy an object's handle variable, MATLAB® copies only the handle.
Both the original and copy refer to the same object. For example, if a
function modifies a handle object passed as an input argument, the
modification affects the original input object.
This opens up the possibility of creating a classdef wrapper around a
C++ class by holding a pointer to the C++ object in memory in the handle
class properties and ensuring it is destroyed when the Matlab class is
destroyed.
This has already been implemented and posted on the Matlab file exchange
http://uk.mathworks.com/matlabcentral/fileexchange/38964-example-matlab-class-wrapper-for-a-c++-class
<http://uk.mathworks.com/matlabcentral/fileexchange/38964-example-matlab-class-wrapper-for-a-c++-class>
I have taken this a bit further and created macros for and a fuller
https://github.com/crobarcro/mcode-cpp-utilities
<https://github.com/crobarcro/mcode-cpp-utilities>
This also has examples of having the underlying classes interact as they
can have access to each other's pointers.
https://github.com/kyamagu/mexplus
<https://github.com/kyamagu/mexplus>
As I said, this also has the benefit of creating a much more 'natural'
way of interacting with C++ classes, so I wondered if there would be any
interest is developing a SWIG wrapper based on this?
Unfortunately I don't know much about SWIG, so wouldn't be an awful lot
of help here, but if there was any interest, could at least help with
testing.
Best regards,
Richard
------------------------------------------------------------------------------
_______________________________________________
Swig-devel mailing list
https://lists.sourceforge.net/lists/listinfo/swig-devel
<https://lists.sourceforge.net/lists/listinfo/swig-devel>
------------------------------------------------------------------------------
_______________________________________________
Swig-devel mailing list
https://lists.sourceforge.net/lists/listinfo/swig-devel
<https://lists.sourceforge.net/lists/listinfo/swig-devel>
------------------------------------------------------------------------------
Loading...