Monday, September 27, 2010

Abusing shared_ptr custom deleters to store data.

So you can associate a custom deleter with a boost::shared_ptr object. That deleter will get called when the last reference to the underlying object is destroyed and is responsible for cleaning up the object. You might use this to use a boost::shared_ptr to hold a mysql handle.
boost::shared_ptr mysql( mysq_init(), mysql_close );

(or something like that .. can't remember the exact function names)
But you can also "hide" objects with the shared_ptr that will get destroyed with it... I'm not sure quite why you might want to do it, but someone was asking how to do somethign similar in StackOverflow.

Here's how you do it.

Friday, September 24, 2010

Graphing include dependencies

Sometimes it's nice to get a higher level view of what files are including which other files.
This becomes tricky as your projects get larger .. So I wrote a little python script to help me visualise it...
You can find the script here

It takes a list of files to analyse on stdin and produces a dot graph file that you can render using the graphviz tools (I find dot and neato to be the most useful for this task)

Heres an example of what I get when I run it over a filtered subset of one of my projects:
(Original is a PDF so its easy to zoom in to see filenames, but you only get a PNG to protect the innocent)

Thursday, September 9, 2010

Fixing whitespace issues with git rebase

We're using a rebase and merge style approach for getting topic branches into our master branch.

Typical usage looks like this:
# Get the topic branch
git checkout topicbranch
# Sit it ontop of master
git rebase master
# Clean up all the commits so its nice and tidy
git rebase -i master
git checkout master
git merge topicbranch
Now if you add a --whitespace=fix to the first rebase stage, git will clean up all the odd whitespace at end of line stuff that git complains about and that editors can leave in there accidentally.

However this only works if your branch is not already branched off master. If your branch already contains the master head you'll just get the message:
Current branch topicbranch is up to date.

It turns out its easy to make the rebase happen:
git rebase -f master --whitespace=fix