Boost::Signal et al

· by Steve · Read in about 5 min · (867 Words)

I tackled a few varied tasks today. One was addressing a bug in the rubber-band selection code I’d written, since it became bizarrely clear to me as I was sipping my tea very late last night that under orthographic projection it should never work. Oddly it had seemed to work but I hadn’t had have time to properly stress test it, and I think it must have been a fluke. Funny how these things occur to you when you’re not concentrating on them. It’s like there’s a second night-shift neuron team who spring into action whilst you’re doing other things and all of a sudden, ‘ping!’, like an oven timer going off they announce their results - in most cases showing up the day-shift neurons for the bunch of muppets they really are. Gotta get those guys working together sometime.

Anyway, so I fixed that, and then started to think about making a few enhancements to Ogre for a couple of editor features I wanted to do. I ended up removing and tidying up a fair amount of old redundant code, and adding a generic way to whiz through all the Renderable instances on any particular object via a visitor pattern. It’s similar to the way the render queue gets populated (which also uses a visitor), where the queueing process doesn’t know precisely how the objects get added to the queue, since objects may be made up of several Renderables due to geometry format changes, material changes etc. Applications can now do the same sort of thing, but provide a visitor of their own, which gets called back with all of the potential Renderables that might get added if the object ends up being visible, including all the LODs and potentially the debug renderables depending on the params. This is different from registering a queue listener where you only get called back just as renderables are about to get pushed onto the queue (where you can veto it, switch material techniques etc), this is more to do things like generic up-front tagging at a renderable level, which is useful to me. It’s all committed to Shoggoth anyway so you can play with it if you use CVS, who knows, perhaps you’ll find a use for it too.

The other thing I did today was incorporate Boost::Signals into my application. For those who don’t know, it’s a C++ version of the signals and slots approach to implementing the Observer pattern, typically used for event processing. Now that I have a need for a more complex set of exchanges between my Model and View I needed to start using proper events instead of the hacked-in direct calls I’d managed with so far, and I didn’t want to use wxWidgets event system for that. That’s mainly because the Model shouldn’t know anything about wxWidgets, and also because wxWidget’s event system uses lots of macros which I don’t like much. Boost::Signals uses templates (of course, this is Boost after all) which is a much more modern approach. And it’s Boost, so by nature it rocks 😉 It needed a bit more wrapping - on its own (well, together with Boost::Bind) it just provides you the ‘glue’ required to wire things together, but nothing for keeping a dictionary of event types and such, which I’ve written around it. And of course typdeffing to make those templates easier on the eye.

Boost is an excellent resource and in the future we intend to start using it more heavily in Ogre too. We already use it for threading, but that’s only required if you turn it on. So far we’ve avoided adding it as a primary dependency, but that position won’t last that much longer I’m sure - Boost is just too great to ignore, and the buggy compilers that used to have problems with some of its templates (VC6 and .Net 2002 in particular) have pretty much gone the way of the Dodo now.  Many elements of Boost will be in the next C++ standard library (such as shared pointers) so eventually this will all be moot.

I’m painfully aware that it’s the end of another week already and I’m still building infrastructure, which is really not where I wanted to be (hence me putting my foot down yesterday on time commitments as I saw the writing on the wall). But I really need all these things before I can prototype anything remotely complicated, so I guess I just have to knuckle down and get through it. I’ll be doing a bit over the weekend anyway (Ogre 1.4.1 on Sunday) but it’s now May, which has an unseemly number of bank holidays in it, at least here. There are 2 next week in fact - Monday is May Day, where in principle we should all be cavorting around May poles and worshipping a pagan entity of some sort (Bel I think, if we’re being celtic?), and Wednesday is Liberation Day, which is one we get extra to the rest of the UK on account of our Grandparents having been under the jackboot. I never thought I’d look at the calendar and say ‘Damn, 2 bank holidays!’, but that’s self-employment for you I guess…;)