Tiny Change We Can Believe In

It has irked me for a long time that although the President of the United States wields great power over the rest of the world, only US citizens get to vote. To be brutally honest it irked me a lot more when Bush II was in power, but I approve of the way our USAian cousins have voted this time. Obama seems exactly what the US and the rest of the world needs – someone thoughtful, honest and willing surround himself with actual experts instead of cronies. That fact that he can string two words together without stumbling is an added bonus.

And its not just me. The action figure industry thinks Obama is pretty cool. This Obama figure is way more bad-ass than the old George W Bush doll.

The C++ Boost Libraries Part 3 – string algorithms

One of the many, many legitimate criticisms that could be leveled at C++ is that string handling is abysmal. Sure std::string can hold some chars for you, but there is a distinct lack of utility functions to actually do anything with those characters. Enter the Boost String Algorithm library, or string_algo to its many friends.

String_algo contains all those fiddly little functions that you write yourself to process text, as well as some clever extensions. The simple stuff is very simple:

string mixedCase = “Hello there”;
to_upper(mixedCase);

string allLower = to_lower_copy(mixedCase);
// mixedCase == “HELLO THERE”
// allLower == “hello there”

string weirdFormat = ” .;text;;;;.. “;
trim(weirdFormat);

// remove punctuation with a predicate
trim_if(weirdFormat, is_punct());
// weirdFormat == “text”

The is_punct() function returns a predicate – string_algo defines a bunch of useful predicates so you will rarely have to roll your own. Not shown in the above is the ability to pass a std::locale to handle strings in other languages.

There is the full compliment of find, replace, and erase substring options:

string statement = “I like cheese, cheese, and cheese”;
replace_nth( statement, “cheese”, -2, “bacon” );
replace_last( statement, “cheese”, “eggs” );
erase_first( statement, “cheese” );
erase_all( statement, “,” );

(the -2 parameter for replace_nth means “replace the 2nd from last occurance” – very useful)

And check this out:

vector words;
string anthem = “God of nations! at Thy feet\n”
                ”In the bonds of love we meet,”;
split( words, anthem, is_any_of(” \n!,”) );

I haven’t even mentioned the ability to do fancy things with regular expression and the concept of find iterators that advance over the substrings that match. Also, the library is generic enough to work with any container that looks vaguely like a sequence of characters, not just std::string.

String_algo is replacing vast numbers of little hacked-together functions in my code. If nothing else it will reduce the temptation to use the God-forsaken CString classes.

The C++ Boost Libraries (Part 2 – boost::assign)

We are still only in the low lands of boost territory but already we are coming across useful discoveries. Today’s stop is boost::assign, one of those clever little pieces of code that makes life easier for everyone. Often you just want to load up a container with some small amount of data. The STL containers do not make such a task particularly easy:

vector<string> faceCards;
faceCards.push_back(“jack”);
faceCards.push_back(“queen”)
faceCards.push_back(“king”);
faceCards.push_back(“ace”);

What a pain in the neck! With boost::assign it becomes:

vector<string> faceCards;
faceCards += “jack”,”queen”, “king”, “ace”;

Neat.

It gets better. A lot of the time you want to fill a container with some data for testing but you don’t really care what that data is:

list<int> data;
// data will contain 1,2,5,5,5,5,5,5,5,5,7
data += 1, 2, repeat(8, 5), 7;

Or even:

list<int> randomData;
// data will contain 1,2,(8 random numbers),7
randomData += 1, 2, repeat_fun(8, &rand),7;

Of course, other containers are supported. This example uses the more flexible insert function (there are other functions for inserting at specific points for containers that support such things):

map<string, string> maoriColors;
insert(maoriColors)(“ma”, “white”)
                   (“whero”, “red”)
                   (“kakariki”, “green”)
                   (“kowhai”, “yellow” );

As with all the boost libraries, a lot of thought has gone into to making the interface safe and flexible. The library can even be extended to non-standard containers if the need should arise.

All the best magic tricks are really just smoke and mirrors, boost::assign is really just functions that secretly return functor objects and operator,() abuse. Unlike most magic, knowing how it works makes it even more delightful; the library is very well documented.

Although boost::assign is useful anywhere, it really shines in simple throwaway programs and test harnesses, where small, simple and easily modifiable code is the goal. I keep finding more and more places to use it.

Game Review : Battlestar Galactica

BSG Box ArtIf you are getting a bunch of guys together to play a board game, you may as well make it a nerdy one. Battlestar Gallactica (BSG) is the board game version of the recent TV show of the same name, and is the best game tie-ins that I have ever seen in terms of capturing the flavor of the original work. This has a downside; of the 5 players we had, one was unfamiliar with the show and was initially quite lost as to what was going on.

On the show, humanity has been all but destroyed by surprise attacks on the 12 colony planets by killer robots (the cylons), some of which look human. Luckily a small fraction of the population happened to be aboard various space craft at the time of the attack. Unluckily, it was the whiniest and most depressing fraction but on the plus side they managed to get away with the one remaining military vessel and a bunch of smaller craft. Now they travel the galaxy in this ragtag fleet looking for a shining planet known as Earth and arguing with their fathers. The cylon fleet (which is much cooler) is hot on their trail and if that wasn’t bad enough the humans have been infiltrated by human-looking cylons that cannot be detected. Some of the traitors don’t even know they aren’t human until they are activated!
Continue reading

The C++ Boost Libraries (Part 1 – Introduction)

Although I like to think of myself of a jack-of-all-programming-trades, I must admit that I am mainly a C++ man. I have dabbled in the seductive dark side of Java and C#, but still prefer the mad poetry that C++ code can generate. A few years ago, C++ was showing signs of its advanced years but this has changed with the Boost libraries – a set of weird and wonderful additions to the C++ standard libraries that really bring C++ into the current century.
Continue reading

Book Review : A Wanderer’s Tale

wanderertaleThe summer holidays are the one time of year when it is acceptable to stuff your face with cheap food and drink, and stuff your brain with cheap paperback novels. As with the food, you tend to have a good time during the consumption of the books but regret it later on. So it is with A Wanderer’s Tale by David Bilsborough, the book I foolishly bought to read during a recent trip.
Continue reading

Replacing a MacBook Hard Drive

My first-generation MacBook laptop had only one problem – its first generation 60 gig hard disk. Actually it used to have two problems, but I installed more memory in it ages ago. 60 gigs doesn’t go far in today’s world of movies and huge software packages, so I felt it was time to upgrade. One trip to the store and a $167 EFTPOS transaction later and I am the owner of a new 320 gig hard disk, which seems to be the current sweet spot for price vs. capacity for 2.5 inch drives.

Thankfully Apple have made the job of replacing a MacBook’s hard disk extremely easy, and they provide step by step instructions. All you need is a 10c coin, a Philips #1 screw driver, a piece of stiff card, and a secret tool not mentioned anywhere in the instructions!

torxscrew

The surprise turns out to be a manner of fastening of which I was hitherto unaware – a TORX screw. TORX screws are used to hold the metal shield surrounding the drive in place, with the screw heads slotting into the bay runners – these must be removed from the old drive to fit the shield to the new drive.

Luckily I happened to have a #1 TORX bit in my tool kit, but finding an unfamiliar screw while a $3000 computer lies in bits on the dinner table is not an experience I wish to repeat.

driveinfo

After replacing the drive, I booted from the Leopard install disk, ran the Disk Utility to format the drive and then restored my data from my Time Machine drive. The end result is that my computer is set up exactly the same way as it was before, except I now have 250 gigs of free space to play with. Much better.