For a Delphi project (built with RAD Studio XE7), I want to create a dictionary of brushes. Each dictionary item contains a TMyBrush object as key, that describes the brush to retrieve, and a GDI+ brush as value.
The TMyBrush class contains 3 fields
In my dictionary, I want to retrieve a brush based on his characteristics, and not on his instance. For example, I want to get a black solid brush from my dictionary by creating a local TMyBrush instance, configuring it to black solid, and getting the matching GDI+ value using the TryGetValue() function. For that, I created a TMyBrushComparer.
Writing the Equals() function isn't a problem for me. However I don't know what is the best practice to write the GetHashCode() function. I would tend to write a function like this:
function TMyBrushComparer.GetHashCode(const pValue: TMyBrush): Integer;
Result := BobJenkinsHash(pValue, SizeOf(TMyBrush), 0);
however I feel that is not a very good practice, it is correct? So, what is the best practice to write a good GetHashCode() function for my TMyBrushComparer?
The code in the question hashes the address of the object rather than its value and so is not consistent with your definition of equality.
Your definition of equality is that three of the fields are equal. Your hash function should match that definition. Hash each of the three fields, and combine the values, for instance using the approach outlined here: http://stackoverflow.com/a/263416/505088
Two of your fields are value types. They are easy to hash to match value identity. The brush info field appears to be a reference type. So again you need to decide what form of identity you want (reference identity, value identity, or perhaps something else) and then implement matching equality test and hash.