Syndicate content

Programming / Technology

Type-safe publish/subscribe in C++ (part 1)

In the next few posts, I'll be describing a type-safe implementation of the publish/subscribe pattern in C++ that has proven quite useful to me. I've used this code for everything from handling input events to implementing message passing across a network. It's main advantages are:

  • No modifications to a subscriber's interface are necessary (ie: doesn't need to define a specific method or implement any specific interface)
  • Abstracts the manner in which messages are published to subscribers behind a common interface (ie: publish messages immediately, queue them up for delivery from a single thread, etc.)
  • Easily allows subscribing for specific messages or entire groups of messages (ie: subscribing for the escape key versus subscribing for all keyboard messages)
  • Type safety (subscribers will only receive messages of the C++ type they specified at subscription time)
  • Doesn't rely on singletons or any other type of global state (more concurrency-friendly and allows localized subscriptions for better performance)

This first post will concentrate on the system's primary interfaces and won't delve into implementation very much. Subsequent posts will then go under the hood as well as describe some of the non-essential, but useful pieces of code. This post assumes some familiarity with templates as well as common Boost components such as Bind, Function, and Shared Pointer.

Arcade project is (really) finished!

So it only took me three years to get around to having the custom control panel overlay printed up for my arcade machine. I've had the image file sitting on my computer the whole time, but I just recently sent it to Scott at Mame Marquees for printing. It came out great. Here's a before and after:


Boost.Spirit: take two

In a previous post I shared my first experience using Boost.Spirit using the example of parsing a greatly simplified material definition. Since then, I've learned a few new things about Spirit, and would like to share them by rewriting the previous example.

Boost.Spirit is cool

I recently needed to parse simple material definition scripts for a work-in-progress rendering system, so I decided it was high time I learn how to use Spirit. For those unfamiliar with the library, Spirit facilitates the creation of small, fast parsers by letting you define EBNF grammars directly in C++ code. Obviously, that's a very "nutshell" description of the library, but you can check out the Spirit homepage if you want to know more. Anyways, lets demonstrate the library's wizadry using my simple material script use-case:

The "Microsoft namespace" strikes again

I hate it when I write trivial code like this:

namespace rs
{

    enum TranslucencyType
    {
        OPAQUE,
        NORMAL,
        ADDITIVE,
        ...
    };

} // end namespace rs

...

switch( translucencyType )
{
    case OPAQUE:
        ...
        break;
    case NORMAL:
        ...
        break;
    case ADDITIVE:
        ...
        break;
    ...
}

and get a confusing error.

Some things any junior C++ dev should know

First of all, I consider myself to be an intermediate C++ programmer at best. Perhaps this is because I've noticed that a programmer's opinion of their abilities is usually inversely proportional to their actual skill level. Like most other development shops, my coworkers and I have a standard list of interview questions we draw from when interviewing programming applicants. Many of them are code fragments with various issues, defects, design issues, etc. While many of these questions try not to focus on language-level details, there are a few things in particular that most "senior" applicants don't know that really amaze me. This is by no means a complete list of things a junior C++ programmer should know, but the regularity with which people are unfamiliar with these specific items blows my mind.

First programming post

I've been trying to decide what my first post in this section should be, and I guess a brief synopsis of my history with programming makes the most sense. I've always found computers and technology interesting, but I didn't start to seriously consider computer science and programming until high school. Unfortunately, my high school didn't offer a computer science class until the second half of my senior year, but I did my best to learn at home too. I ended up getting a BS in computer science from the University of Texas at Dallas.

Down-time = new server

So obviously I haven't posted since July. It's really due to a combination of laziness and business (a strange combination, I know), but I've decided to blame it on my web server dying instead. I can't really be upset about it since at the time of death, it had a contiguous up-time of over a year. So, I used the opportunity to upgrade my main gaming PC, with the plan being to pass it's guts down to the web server. Then, my coworker randomly decided to give me some brand new parts he had purchased for a server he never built. So now the server is sitting pretty in a massive, modded full-tower I used to use, which now contains a sweet Soyo server-grade motherboard and dual Opterons:

Posting from an iPhone 3G line...

I'm sitting here in the dark outside of an AT&T store at the front of an iPhone 3G line. Yes, I know I'm a dumb ass. Yes, I know I could probably get one without doing this. Don't try to rationalize it. I'm here because I want one before I go to San Diego for Comic Con in about a week, and I'm paranoid that some ridiculous sequence of events would prevent me from getting one otherwise. My girlfriend and I got here at 6pm and were the first ones to show up. About 30 minutes later, more people started to trickle in. It's now 10:30pm and there's about 30 people in line behind us.

Arcade project is finished!

Actually, it's been finished for about a month, but I've been too busy playing with it and doing other stuff to update my site. The only thing left to do is to put the control panel overlay artwork on once my coworker finishes making the image for me, but that's not a big deal. It sure has been nice to have my living room back...here's a few pics: