Add Component::GetCanonicalNameVal to avoid extra StringVal allocations

This commit is contained in:
Tim Wojtulewicz 2022-05-10 13:31:06 -07:00
parent 22ab3bb0b3
commit a680c2faf0
5 changed files with 51 additions and 3 deletions

View file

@ -74,6 +74,22 @@ public:
*/
const std::string& GetComponentName(EnumValPtr val) const;
/**
* Get a component name from its tag.
*
* @param tag A component's tag.
* @return The canonical component name as a StringValPtr.
*/
StringValPtr GetComponentNameVal(zeek::Tag tag) const;
/**
* Get a component name from it's enum value.
*
* @param val A component's enum value.
* @return The canonical component name as a StringValPtr.
*/
StringValPtr GetComponentNameVal(EnumValPtr val) const;
/**
* Get a component tag from its name.
*
@ -216,6 +232,35 @@ template <class C> const std::string& ComponentManager<C>::GetComponentName(Enum
return error;
}
template <class C> StringValPtr ComponentManager<C>::GetComponentNameVal(zeek::Tag tag) const
{
static auto error = make_intrusive<StringVal>("<error>");
if ( ! tag )
return error;
if ( C* c = Lookup(tag) )
return c->CanonicalNameVal();
reporter->InternalWarning("requested name of unknown component tag %s", tag.AsString().c_str());
return error;
}
template <class C> StringValPtr ComponentManager<C>::GetComponentNameVal(EnumValPtr val) const
{
static auto error = make_intrusive<StringVal>("<error>");
if ( ! val )
return error;
if ( C* c = Lookup(val.get()) )
return c->CanonicalNameVal();
reporter->InternalWarning("requested name of unknown component tag %s",
val->AsString()->CheckString());
return error;
}
template <class C> zeek::Tag ComponentManager<C>::GetComponentTag(const std::string& name) const
{
C* c = Lookup(name);