Friday, February 16, 2007

Beyond STL

The Standard Template Library has made its way into the standards, being shipped as an indispensable object oriented core low-level library. Known to most C++ developers fondly as the STL, it provides standard object oriented reusable components that allow developers to focus on the problem in hand rather than think in terms of storing, retrieving and sorting data-structures.

The most common classes that you might have encountered would be the string, the vector and the list. There is more than just these in STL and plenty of books and articles have been devoted to the innards of STL and how to use them effectively.

STL liberates the developers from constructing custom linked lists and hash tables by using templates thereby aiding them in focusing at more higher level goals of the software.

My aim in the article is to focus at the next level of software development. Though not focused at topics like architecture and design patterns, we will be looking at a public domain C++ library, partially built on STL. The library is called Boost++.

Boost++ (http://www.boost.org)

Boost++ is a C++ library that contains within itself, a set of useful components that makes common developer tasks much easier. Boost++ is public domain. It means that it can be used in both commercial and non-commercial projects without need to submit the sources or display copyright information in your application .

Boost complements the STL by providing utility classes to aid the developers to bridge the gap to the next level of software components. These utility classes are organized into various categories like ‘text manipulation’, ‘math’, ‘memory management’ etc.

Though it is not possible to do justice to this library in just a few pages of text, I will focus on just a few useful classes that I’ve used and leave the rest to the inquisitive explorer.

Memory Management: Smart Pointers

With the birth of new object oriented programming languages like C#, Java and Python, the focus has mainly been to free the developers from pointer nuances and custom memory management. C++ still lacks garbage collection and reference counting by giving more control to the developers. However, more control means more responsibility and failing to handle such power may leave you with a few dangling pointers and leaked memory chunks.

Consider the code snippet:


void foo()
{
char *ptr = new int;
if(some_cond)
{
*ptr = 10;
// do something with ptr

return; // Oops! I failed to free ptr
}

delete [] ptr;
}


Not limited to just conditional branches, the same result could be obtained with code that throws exceptions. These kinds of code crops in unnoticed and become difficult to detect. After all, to err is human…

Boost++ solves this problem by providing type-safe classes to do the memory de-allocation for you.

Agreed that there are a few components like Microsoft’s ATL auto_ptr and others. However, most components that I’ve found tightly embrace their parent architecture, which makes it less portable. Boost on the other hand is not build for any specific architecture. Boost is a set of components that can be applied to any kind of architecture. Currently, Boost supports Microsoft’s Visual C++ (6 and 7), GCC, Borland’s C++ tools and more.

Boost++ comes with not just one memory management class, but five to suite the developers needs.

Here is a sample code that uses Boost++


void foo()
{
boost::scoped_ptr ptr(new int);

if(some_cond)
{
*ptr = 10;
// do something with ptr

return;
}
}


Simple eh? Not only does this improves the readability of the code, but also prevents unintentional errors from cropping up.

String and text processing:

Using regular expressions to validate input data

With any GUI application, developers are faced with writing lots of validation logic to validate data typed by the users. Typically, this would consists of a lot of calls to ‘strchr’ and ‘strtok’ to see if the data entered by the user falls into a specific pattern and to extract the data typed by the user excluding the white-spaces.

A easier way to see if the data conforms with a specific pattern is to use regular expressions. Any developer using an advanced editor or search tools will be familiar with regular expressions.


bool validate(const string& data)
{
// pattern for a data (dd/mm/yyyy)
static boost::regex regex("[0-9]{2}/[0-9]{2}/[0-9]{4}");

if(regex_match(data, regex))
return true;

return false;
}


The code snippet above checks to see if the data entered by the user conforms to the pattern of the ‘dd/mm/yyyy’ format. The same can be done for credit-card numbers, phone numbers and more.

Well, that's not over yet. A slew of more features and capabilities provided by Boost++ awaits future reviews...

As the popular saying goes, Boost++ is the secret of (my|our) energy!

Saturday, February 10, 2007

Yahoo! Pipes – News nuggets served piping hot

A news feed aggregator recently released by Yahoo is not just an aggregator. It allows users to configure your various news feeds, consolidate them, filter them by specific keywords, cull out redundant news items and present a single feed. Not much? They provide basic building blocks to perform all these operations and allow users to program by plug in the blocks very much like Lego in a graphical manner! Check out http://pipes.yahoo.com to create your own.

Yahoo Pipes is named after the Unix Pipes where the output of one application is 'piped' to another application for further processing.

A simple consolidator for a few sites that I frequent is here

Tuesday, February 06, 2007

Space sims / shooters and true 3D

Recently, I had a craving for space sims and shooters and a tendency to blast off a few aliens. As the system I own is no gaming rig, I look for older games that are cheaper or requirements and on the purse as well. Nowadays, most publishers are keener in releasing games that fall into the FPS or MMORPG categories.

After rummaging through the bargain bins at Landmark, I acquired Freelancer, a space shooter from Microsoft. You play a freelancer who takes odd jobs, make money so that you can retrofit your ship or weapons. One key differentiator of Freelancer is its open-endedness. Though the game features a central theme and a plot, the player is free to do whatever he / she chooses, like accepting odd jobs that are randomly generated, thus enhancing the playability.

An oddity of Freelancer and most space shooters are, though the action takes place in deep space, all items and objects of interest are located in the same plane.

Compare this to Homeworld, a 3D RTS (real-time strategy) game, and you are in a completely different league altogether. All the three axis’s are put to full use and the freedom disorients you. This one as most RTS goes gives you a fleet to manage over and during the course of an epic battle, you instruct your fleet to perform various maneuvers like attacking, refueling or salvaging in true 3D.

It looks like the market for space sims seems to be down the past few years, with no new major titles being released. The current game on the download queue (hint: BSNL has upped it’s bandwidth to 2MBPS shared) is the grand-daddy of all space shooters, Decent Freespace 2. The game was finally open-sourced giving access to all the code-base for you to tweak and enhance.