Richard Crozier
2016-08-08 11:18:05 UTC
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
------------------------------------------------------------------------------
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
------------------------------------------------------------------------------