246 5. Engine Support Systems
this because the latt er implementation causes the strings to be unnecessarily
re-interned every time the function f() is called.
static StringId sid_foo = internString(“foo”);
static StringId sid_bar = internString(“bar”);
// ...
void f(StringId id)
{
if (id == sid_foo)
{
// handle case of id == “foo”
}
else if (id == sid_bar)
{
// handle case of id == “bar”
}
}
This approach is much less effi cient.
void f(StringId id)
{
if (id == internString(“foo”))
{
// handle case of id == “foo”
}
else if (id == internString(“bar”))
{
// handle case of id == “bar”
}
}
Here’s one possible implementation of internString().
stringid.h
typedef U32 StringId;
extern StringId internString(const char* str);
stringid.cpp
static HashTable<StringId, const char*> gStringIdTable;