New Music Player

I had been thinking of putting up my entire track list on this web page so that people don’t always have to listen to whatever track I had hard-coded in the play list.  This is the same player which I had on my previous website, but this time I think I am going to settle with one of the provided skins.  I don’t think it matches the theme of my web page.  If someday I find some free time, I’d change the colors on the player to sit better with the rest of the website.

I actually have the registration key for this player, just have to get one for this domain.

I haven’t listed all my earlier tracks because they sound horrible :).

Smart Pointers :: counted_ptr

After working in C# or ObjC for a while you realize how painful C++ memory management can get at times. Being responsible for releasing pointers explicitly seriously hinders my program design sometimes. This is particularly a problem when doing new just can’t be avoided (unmanaged objects within managed classes), or when you’re managing a list of objects where you need a new to allow some degree of runtime polymorphism.

Then, I stumbled across this simple smart pointer class which takes care of reference counting. Its called counted_ptr and can be found here. Using this class, I allocate my objects once and let the counted_ptr class object carry it. Once I no longer need the allocated object the counted_ptr object deletes it for me. This is how I use it within my code:

void DrawLine (const dhVector<float>& start, const dhVector<float>& end, const dhColor<float>& color) {
	_lastQueuedOperations.push_back (dhOperation (dhOperationType_Line,
		counted_ptr<OperationParameters>(new dhLineDrawOperationParameters<float, float> (start, end, color))));
}

Here is a routine that queues a draw line operation which will be executed in a post process pass of the rendering pipeline of our 3D engine. I allocate an object of type dhLineDrawOperationParameters which I pass in to a counted_ptr object as a OperationParameters object, which is then passed into the dhOperation constructor and is pushed back into the list of queued operations. Once this operation is executed, I don't need to explicitly delete the allocated pointer. counted_ptr takes care of releasing the object once the reference count falls to zero.

HTH

Smarter Dictionaries in C#.Net

Alright, here we go. After a long long long time I have decided to yet again try and maintain a blog.

The other day at work I was trying to use .NET’s Dictionary class to create a hierarchical structure which allows me to do something like this:

torqueInfo[eAvatarGender.male][eJointName.elbow][eJointDirection.extension]
	= new TorqueCurves(eAvatarGender.male, eJointName.elbow, eJointDirection.extension);

torqueInfo has been defined as:

Dictionary<eAvatarGender, Dictionary<eJointName, Dictionary<eJointDirection, TorqueCurves>>> torqueInfo;

Using the dictionary object this way would help me reduce my coding clutter to just 3 look-ups instead of coding a 3 level conditional switch (one of the straightforward ways of doing it).

The problem with this is that unlike C++ std::map objects, if a .NET dictionary doesn’t contain the specified key it throws an exception.

Dictionary<int, string> keyVal = new Dictionary<int, string> ();
keyVal [5] = "Hello";		// exception
keyVal.Add (5, "Hello");	// OK

Filling up such a construct with an Add method could have easily turned into a nightmare. To tackle this problem, I created my own little dictionary class which could handle unknown keys and allow the programmer to easily populate a complex construct like I mentioned above. Here’s what the class looks like:

public class SmarterDictionary<TKey, TValue> where TValue : new ()
{
    Dictionary<TKey, TValue> keyVal = new Dictionary<TKey,TValue> ();

    // access operator is all we need!
    public TValue this[TKey key] {
        get {
            if (!keyVal.ContainsKey(key))
                keyVal.Add(key, new TValue ());

            return keyVal[key];
        }
        set {
            keyVal[key] = value;
        }
    }
}

HTH