-
NOTE: The MATLAB® Interface to C++ is the recommended way to interface with C++ classes in MATLAB.
-
NOTE:
libmexclassis 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.hppandCustomProxyFactory.cpp. CustomProxyFactory.hppandCustomProxyFactory.cppMUST be in the same directory.
example/CustomProxyFactory.hppandexample/CustomProxyFactory.cppprovide 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++
Proxysubclasses don't match exactly, thenlibmexclasswon't be able to connect them.
- If you created a custom
mexclass::Proxysubclass in C++ namedCustomProxyand used the stringCustomProxyin theswitchstatement inside of yourCustomProxyFactoryimplementation, then the name of yourmexclass.Proxysubclass 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.