mirror of
https://github.com/zeek/zeek.git
synced 2025-10-06 08:38:20 +00:00
Add Component::GetCanonicalNameVal to avoid extra StringVal allocations
This commit is contained in:
parent
22ab3bb0b3
commit
a680c2faf0
5 changed files with 51 additions and 3 deletions
|
@ -50,7 +50,7 @@ function __name%(atype: AllAnalyzers::Tag%) : string
|
|||
if ( ! component )
|
||||
return zeek::make_intrusive<zeek::StringVal>("<error>");
|
||||
|
||||
return zeek::make_intrusive<zeek::StringVal>(component->CanonicalName());
|
||||
return component->CanonicalNameVal();
|
||||
%}
|
||||
|
||||
function __tag%(name: string%) : AllAnalyzers::Tag
|
||||
|
|
|
@ -103,8 +103,7 @@ function Files::__stop%(file_id: string%): bool
|
|||
## :zeek:see:`Files::analyzer_name`.
|
||||
function Files::__analyzer_name%(tag: Files::Tag%) : string
|
||||
%{
|
||||
const auto& n = zeek::file_mgr->GetComponentName(zeek::IntrusivePtr{zeek::NewRef{}, tag->AsEnumVal()});
|
||||
return zeek::make_intrusive<zeek::StringVal>(n);
|
||||
return zeek::file_mgr->GetComponentNameVal(zeek::IntrusivePtr{zeek::NewRef{}, tag->AsEnumVal()});
|
||||
%}
|
||||
|
||||
## :zeek:see:`Files::file_exists`.
|
||||
|
|
|
@ -16,6 +16,7 @@ Component::Component(component::Type arg_type, const std::string& arg_name,
|
|||
tag_subtype(tag_subtype)
|
||||
{
|
||||
canon_name = util::canonify_name(name);
|
||||
canon_name_val = make_intrusive<StringVal>(canon_name);
|
||||
}
|
||||
|
||||
void Component::Describe(ODesc* d) const
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include "zeek/Tag.h"
|
||||
#include "zeek/Type.h"
|
||||
#include "zeek/Val.h"
|
||||
|
||||
namespace zeek
|
||||
{
|
||||
|
@ -98,6 +99,7 @@ public:
|
|||
* ID.
|
||||
*/
|
||||
const std::string& CanonicalName() const { return canon_name; }
|
||||
StringValPtr CanonicalNameVal() const { return canon_name_val; }
|
||||
|
||||
/**
|
||||
* Returns a textual representation of the component. This goes into
|
||||
|
@ -135,6 +137,7 @@ private:
|
|||
component::Type type;
|
||||
std::string name;
|
||||
std::string canon_name;
|
||||
StringValPtr canon_name_val;
|
||||
|
||||
/** The automatically assigned component tag */
|
||||
zeek::Tag tag;
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue