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

Inside a Text Editor Ever since my college days, after dabbling with vi and a few other editors, I always had an yearning to create my own. Now, I am still stuck with XEmacs and jEdit and had a chance to compile / study the sources and documentation of EMACS and a free editor component called Scintilla. Until now, I was under the the belief that text editors used a doubly linked list to represent the text in memory. The advantages of this approach being insertions and deletions are much more easier which is just a matter of just un-linking a node off the list. But the shortcomming is that they tend to fragment memory with each node or line take a bit of memory. The other alternative approach is to have a dynamic array which is a contiguous space of memory and can sometimes be directly written off to a file. The disadvantages are that insertion and deletion are costly and you need to reallocate quite frequently. While goint throug the source and documentation of text editors, I chanced ...
The time machine Managed to get a copy of the King Solomon's Mines by Henry Rider Haggard. The book was originally published at 1855, more than 150 years since I am reading it. I'm sure that it would transport me back in time revealing how people lived and the problems that they faced. Of course, you can get the text in an electronic form from the Project Gutenberg for free, but I prefer the traditional form of reading where you can 'shutdown' any time and 'restore' in a jiffy. The other book that I have in reserve is The Journey to the Center of the Earth by Jules Verne (1864). There are so many classics that you can get access to for free if you do not mind staring at your display or PDA. But don't miss them out.
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...