max++ library for writing externals in C++
I’m making public (under MIT license) a C++ template library & cmake scripts that I use to write max externals in C++:
This is not a release but "developer preview", there isn’t much of documentation save for a dummy "hello_maxxx" object which demonstrates what it takes to build an external with max++. The library depends on several boost headers and boost::regex library. The code is available in the git repository at gitorious, you can browse the sources online at:
or download the archive with the latest sources (or any older version) going to the link on the right hand side of that page.
The library is pretty minimalist, its main purpose is to automatically generate all the class / methods registration code – i.e. if you have a C++ class Foo with a method bar(long x), you just tell something like: …(("bar",bar))… and the clever macro/template thing knows to call
class_addmethod(Wrapper(Foo), wrapper(Foo::bar), "bar", A_LONG, 0); where Wrapper(Foo) and wrapper(Foo::bar) are generated behind the scenes and
A_LONG is figured out from the wrapped method prototype. If the method prototype looks like the it expects A_GIMME, then that’s what it’ll get.
Basically, I wanted to avoid boiler plate programming, so I hid all the boiler plates and logic to generate them under the hood of the maxxx.hpp header. All I need to do now is declare the class as
MXX_CLASS(Foo) instead of
class Foo and register it in
main() with something like:
MXX_REGISTER_CLASS(Foo, "foo", (("message-name", method_name)) (("another-message",another_method)) ..., 1); // 1 outlet
At the moment the library doesn’t use proxies, so objects made with it can accept only numbers in non-leftmost inlets.
For a better illustration see:
Any comments, questions and suggestions are appreciated.
I forgot to mention that at the moment it only understands (knows to wrap) a limited subset of C/C++ primitive types – long, float. double and [const] char *. I just didn’t use anything else in my own code. But it is possible (and in many cases very simple) to add support for more types: e.g. int and std::string. I think I’ll do that so the diff can be used as a demo how to add types :-)
And here you go, the diff:
demonstrates int and std::string automated parameter conversions. maxxx.hpp portion shows the implementation of the conversion and hello_maxxx.cpp shows how they are used (or rather that the conversions are transparent).
The conversion can’t deal with the normal way to pass objects in C++ – as constant references, which is a bit wasteful (conversion creates a temporary string object and right away passing it to the method copies it). Dealing with const references will require more template voodoo as soon as I have more time.
The problem has proven to be easier then I anticipated, the following diff:
automates handling both const and const reference typed arguments. Notice how I don’t have to give duplicate rule for
char * and
const char * anymore and how
std::string rule allows to pass
Forums > Dev