A global table for the hashing and tracking of strings.
Only one _StringTable is ever instantiated in Torque. It is accessible via the global variable StringTable.
StringTable is used to manage strings in Torque. It performs the following tasks:
- Ensures that only one pointer is ever used for a given string (through insert()).
- Allows the lookup of a string in the table.
StringTableEntry mRoot;
mRoot = StringTable->insert(root);
StringTableEntry stName = StringTable->lookupn(name, len);
if(mRoot == stName)
Con::printf(
"These strings are equal!");
void printf(const char *fmt,...)
Definition console.cc:644
But why is this useful, you ask? Because every string that's run through the StringTable is stored once and only once, every string has one and only one pointer mapped to it. As a pointer is an integer value (usually an unsigned int), so we can do several neat things:
- StringTableEntrys can be compared directly for equality, instead of using the time-consuming dStrcmp() or dStricmp() function.
- For things like object names, we can avoid storing multiple copies of the string containing the name. The StringTable ensures that we only ever store one copy.
- When we're doing lookups by name (for instances, of resources), we can determine if the object is even registered in the system by looking up its name in the StringTable. Then, we can use the pointer as a hash key.
The scripting engine and the resource manager are the primary users of the StringTable.
- Note
- Be aware that the StringTable NEVER DEALLOCATES memory, so be careful when you add strings to it. If you carelessly add many strings, you will end up wasting space.