Skip to main content
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!

Comments

Popular posts from this blog

Battle of Wesnoth Been on the lookout for a free turn based strategy game and chanced upon the Battle of Wesnoth . Despite it being an open source game (meaning, you get the source), it was incredibly polished akin to any of the other turn based strategy game (Alpha Centauri), be it the background score or the graphics or the tutorials. The game itself is set in a period similar to the D&D or nethack era. For the film buffs, if you have read or seen the Lord of the Rings, you would probably be able to relate to the clans that populate the game world. The game play, as with any turn based strategy game requires background information on each of the units that you own, their strengths and weaknesses and a lot of planning (a kin to chess, but with a lot more parameters) where factors like day - night cycles are taken into account (e.g, humans fight well during the day, but the orcs are better during the night). It is encouraged to keep your older units as they gain experience and beco
Helmet and seat-belt mandated at TN Today morning, I was pleasantly surprised to see most bikers (along with pillion riders) traveling with their helmets on as TN geared itself from the 1st of June to make it mandatory to wear helmets for bikers and seat belts (for the front seat passengers) for car commuters. The FM radio RJs had been conducting on-the-road commentaries, catching helmet-less riders seeking justification on why they had not yet acquired one (the helmet) yet. The shamed few who got caught eventually were presented with a helmet. One the way (near Tirumangalam), I couldn't help notice people flocking near put-up helmet shops on the sidewalks, trying to acquire the license to ride a bike! Let's hope the momentum carries on...

WiFi atlast

After numerous hours of tweaking, I resolved the problem with my WG311v3 NetGear PCI card. The problem? The router was too far away! Now that I am online using my Linux box, here are a few screen-shots of my desktop! The next time your supported WiFi card acts up, you know what to do... For those inquisitive lot, I am currently using the XP drivers with ndiswrapper.