Archive for December, 2009

DIY Webserver Using PureMVC++

Tuesday, December 22nd, 2009

To test out my newly ported PureMVC++ implementation I decided to write an example webserver. I’ve always wanted to write one, and I didn’t realize just how easy it is (to make a VERY rudimentary version).

The server parses your http request and then spits it back out at you, with some nice html.
Screen shot 2009-12-22 at 2.35.47 PM
That’s it! Source code is here:
http://github.com/schell/PureMVC-Plus-Plus/tree/master/example/httpserver/

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

Friday, December 18th, 2009

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.


Follow me on GitHub
Follow me on Google+
Follow me on Twitter