PureMVC++ – A C++ MVC framework (ported from AS3)

I had some time over the past month to port the popular PureMVC application architecture from AS3, to C++. I learned a ton about C++ and it turned out to be a rather smooth process. The code is up on github.

Differences in the C++ version

Notification names and types are ints!
At first I used strings for notification names and types, just like in other ports. I did this until I was writing the first sample application. At that moment I realize in C++ you can’t switch on strings! There’s no support for writing a switch statement on the type std::string. I simply didn’t know that. So code like this just won’t compile:

switch ("somestring")
{
    case "notthisone":
        trace("not gonna happen");
        break;
       
    case "northisone":
        trace("also not gonna happen");
       
    case "somestring":
        trace("this one gets evaluated");
        break;
       
    default:
}

C++ only allows you to switch on ints, so to save us from having to write something like this:

if(note->getName() == "notthisstring")
    cout << "not gonna happen";
else if(note->getName() == "northisstring")
    cout << "also not going to happen";
else if...

I decided to make note names and types ints. This way we can enumerate our notification names and types like so:

class n_name
{
public:
    enum name
    {
        NIL,
        STARTUP,                // triggers the app startup sequence
        SET,                    // sets something
        GET,                    // makes a request to get something
        DISPLAY,                // display something
        QUIT                    // quit the app
    };
}

and then handle the notification with ints:

int name = note->getName();
switch(name)
{
    case n_name::STARTUP:
        cout << "Startup the app!";
        break;
    case n_name::QUIT:
        cout << "Shutdown...";
        break;
    default:
}

IFacade no longer contains a registerCommand method
I tried my best to figure out a way to implement stateless commands in C++. The way the AS3 version accomplishes this is by passing around references to classes. In that port handling references to classes in an abstract way is easy because AS3 has built in type introspection. In order to do the same thing in C++ we’d have to depend on something from the boost library. That’s not an option. Instead I gave Facade a templated method to replace IFacade’s registerCommand (conveniently called registerCommand). So instead of calling registerCommand(noteName, CommandClassName), one makes a call like this: registerCommand<CommandClassName>(noteName). This calls a special template function created by the compiler that adds CommandClassName to a templated Observer, which is added to the View’s observer list.

That covers it for now. In the coming weeks I’ll probably be adding example code to this post, as well as example applications to the repo. If you’re interested in the project, keep up to date with the repo at github and soon we’ll have a repo at PureMVC.org. I’ve added some temporary documentation for you to use as well.

Tags: ,

6 Responses to “PureMVC++ – A C++ MVC framework (ported from AS3)”

  1. Looks really great! I am going to try to implement this into an openFrameworks ( a student ) project over the next week and see how it works. I’ll let you know. Thanks!

  2. Awesome! Yeah – let me know how it goes.

  3. Thank you for good job, Ive just started to think about porting the puremvc to c++, than i found this page.

  4. Great! I hope it works for you.

  5. Please try with LibPureMVC C++ which support thread-safe http://code.google.com/p/libpuremvc/

  6. Awesome – I’ll definitely check this out.

Leave a Reply


Follow me on GitHub
Follow me on Google+
Follow me on Twitter
Thrilling.
EFNX is proudly powered by WordPress
Entries (RSS) and Comments (RSS).