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!

No comments: