max++ library for writing externals in C++

Sep 28, 2010 at 11:08pm

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++:

http://gitorious.org/maxxx

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:

http://gitorious.org/maxxx/maxxx/trees/master

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:

http://gitorious.org/maxxx/maxxx/blobs/master/hello_maxxx.cpp

Any comments, questions and suggestions are appreciated.

#52510
Sep 29, 2010 at 4:28am

nice!

#188847
Sep 29, 2010 at 5:41am

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 :-)

#188848
Sep 29, 2010 at 7:04am

And here you go, the diff:

http://gitorious.org/maxxx/maxxx/commit/fd4077e251fa6e9f1e362a905cd9b1bb10bd39d1

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.

#188849
Sep 29, 2010 at 7:24am

The problem has proven to be easier then I anticipated, the following diff:

http://gitorious.org/maxxx/maxxx/commit/8623eb9ae32fa3b691c9e4e0b0cf3a24e22381ed

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 const std::string&.

#188850

You must be logged in to reply to this topic.