-
NOTE: The MATLAB® Interface to C++ is the recommended way to interface with C++ classes in MATLAB.
-
NOTE:
libmexclass
is UNDER ACTIVE DEVELOPMENT. It is NOT recommended for production use.
libmexclass
is a MATLAB framework which enables users to implement the functionality of MATLAB classes in terms of equivalent C++ classes using MEX.
For MATLAB® projects that are already using MEX, libmexclass
aims to provide an alternative for interfacing with C++ classes.
It is recommended to use libmexclass
with CMake.
libmexclass
enables client code to:
- Implement MATLAB class methods in terms of equivalent C++ class methods using a MEX based proxying mechanism
- Bound the lifetime of C++ objects to equivalent MATLAB objects
libmexclass
is heavily inspired by the Proxy Design Pattern. Using libmexclass
involves connecting mexclass.Proxy
subclasses in MATLAB with corresponding mexclass::Proxy
subclasses in C++.
To use libmexclass
in a MATLAB project, several steps are required, which are enumerated below.
Create a subclass of mexclass::ProxyFactory
in C++ with the name CustomProxyFactory
.
mexclass::ProxyFactory
is a "simple factory". CustomProxyFactory
is responsible for returning subclasses of mexclass::Proxy
which wrap "raw" C++ objects and control access to their state.
- Your subclass MUST be named
CustomProxyFactory
. - Your filenames MUST be named
CustomProxyFactory.hpp
andCustomProxyFactory.cpp
. CustomProxyFactory.hpp
andCustomProxyFactory.cpp
MUST be in the same directory.
example/CustomProxyFactory.hpp
andexample/CustomProxyFactory.cpp
provide an example of how to implementCustomProxyFactory
.
Create a subclass of mexclass.Proxy
in MATLAB whose class name is identical to the string used in the switch
statement inside of your CustomProxyFactory
implementation.
- If the names of your corresponding MATLAB and C++
Proxy
subclasses don't match exactly, thenlibmexclass
won't be able to connect them.
- If you created a custom
mexclass::Proxy
subclass in C++ namedCustomProxy
and used the stringCustomProxy
in theswitch
statement inside of yourCustomProxyFactory
implementation, then the name of yourmexclass.Proxy
subclass in MATLAB MUST also beCustomProxy
.
To use libmexclass
in a MATLAB project, use the ExternalProject_Add()
CMake function.
ExternalProject_Add(libmexclass
GIT_REPOSITORY https://github.com/mathworks/libmexclass
CMAKE_ARGS -D CUSTOM_PROXY_FACTORY_DIR=<custom-proxy-factory-dir>)
In the CMake code snippet above, replace <custom-proxy-factory-dir>
with the location of your implementation of CustomProxyFactory
relative to the CMakeLists.txt
file for your project.
libmexclass
is compatible with MATLAB R2022a or newer.
We welcome external contributions! Feel free to open a pull request!
For more information, see CONTRIBUTING.md.
Copyright © 2022, The MathWorks, Inc.