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:
trace("not gonna happen");
trace("also not gonna happen");
trace("this one gets evaluated");
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";
I decided to make note names and types ints. This way we can enumerate our notification names and types like so:
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();
cout << "Startup the app!";
cout << "Shutdown...";
- 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.