StringId Class ReferenceLightweight type for storing strings for use as ID's.
Public Member Functions
|StringId (const char *idString)|
|const char *||GetString () const|
|unsigned int||GetHash () const|
|StringId (const StringId &stringId)|
|const StringId &||operator= (const StringId &stringId)|
|bool||operator== (const StringId &stringId) const|
|bool||operator!= (const StringId &stringId) const|
Detailed DescriptionLightweight type for storing strings for use as ID's.
The StringId type is perfect for when you need to identify something by name. When you create a StringId type from a c-style zero-terminated string (const char*), it will calculate a hash-number from all the characters, and use the resulting value as an index into a global array of string id data. This array entry typically holds just the string we're looking for (or is empty in case this is the first time a StringId is requested for that particular string), but sometimes it can hold a list of just a few strings that happen to have the same hash-number. Once the corresonding string have been found (or inserted, if this is its first use) in the table, it is stored in the StringId type.
This initial string matching process involves looping through each character of the string, and (worst case) a handful of string compares), which isn't exactly crazy expensive, but certainly not free, either. All other operations you perform on a StringId is extremely cheap though. Since the string pointer (const char*) stored inside the StringId type is from the shared string id table, we can compare two StringId values by simply comparing two pointers. And we can copy a StringId by simply copying its internal pointer - no extra lookup needed.
So we trade a little bit of overhead at creation (looping through the string once and maybe doing a couple of string compares) for almost free copying and comparison when using it. If you just make sure not to unnecessarily create StringId's, you won't ever find them slowing things down.
There's also a couple of macros, strSwitch and strCase, which you can use to emulate the behaviour of the c-language statements "switch" and "case", to make it more easy to do a bunch of comparisons for a single StringId. The strCase macro is written such that it automatically initializes the specified StringId once on the first time it is run, meaning it will always just do a pointer compare on subsequent runs.
Constructor & Destructor Documentation
|StringId::StringId||(||const char *||idString||)|
Constructor. Looks up the specified string in the shared string table, and calculates the hash value for the string. If it is the empty string (""), the StringId will use a string pointer of 0 instead. This is so that both empty strings and null-strings is treated like the same ID, which is more useful.
idString The string to use for identifier
|StringId::StringId||(||const StringId &||stringId||)|
Member Function Documentation
|const char * StringId::GetString||(||)||const|
|unsigned int StringId::GetHash||(||)||const|
Retrieve the hash-number calculated for the string. This number is primarily for internal use, but can also be useful if you want to store StringId's in your own hash-table, as you could reuse this stored hash-number instead of having to recalculate it yourself. The number is calculated in such a way as to avoid collisions, but collisions WILL happen anyway, so must be dealt with
- The hash-number for the string
|bool StringId::operator==||(||const StringId &||stringId||)||const|
|bool StringId::operator!=||(||const StringId &||stringId||)||const|
Reproduction/republishing of any material on this site without permission is strictly prohibited.