Change zeek:🆔:lookup functions to use std::string_view

This commit is contained in:
Jon Siwek 2020-05-12 23:24:46 -07:00
parent 86cbab3b7f
commit 3bcf55ce41
2 changed files with 23 additions and 17 deletions

View file

@ -31,45 +31,49 @@ IntrusivePtr<TableType> zeek::id::count_set;
IntrusivePtr<VectorType> zeek::id::string_vec;
IntrusivePtr<VectorType> zeek::id::index_vec;
const IntrusivePtr<ID>& zeek::id::lookup(const char* name)
const IntrusivePtr<ID>& zeek::id::lookup(std::string_view name)
{
return global_scope()->Find(name);
}
const IntrusivePtr<BroType>& zeek::id::lookup_type(const char* name)
const IntrusivePtr<BroType>& zeek::id::lookup_type(std::string_view name)
{
auto id = global_scope()->Find(name);
if ( ! id )
reporter->InternalError("Failed to find type named: %s", name);
reporter->InternalError("Failed to find type named: %s",
std::string(name).data());
return id->GetType();
}
const IntrusivePtr<Val>& zeek::id::lookup_val(const char* name)
const IntrusivePtr<Val>& zeek::id::lookup_val(std::string_view name)
{
auto id = global_scope()->Find(name);
if ( ! id )
reporter->InternalError("Failed to find variable named: %s", name);
reporter->InternalError("Failed to find variable named: %s",
std::string(name).data());
return id->GetVal();
}
const IntrusivePtr<Val>& zeek::id::lookup_const(const char* name)
const IntrusivePtr<Val>& zeek::id::lookup_const(std::string_view name)
{
auto id = global_scope()->Find(name);
if ( ! id )
reporter->InternalError("Failed to find variable named: %s", name);
reporter->InternalError("Failed to find variable named: %s",
std::string(name).data());
if ( ! id->IsConst() )
reporter->InternalError("Variable is not 'const', but expected to be: %s", name);
reporter->InternalError("Variable is not 'const', but expected to be: %s",
std::string(name).data());
return id->GetVal();
}
IntrusivePtr<Func> zeek::id::lookup_func(const char* name)
IntrusivePtr<Func> zeek::id::lookup_func(std::string_view name)
{
const auto& v = zeek::id::lookup_val(name);
@ -77,7 +81,8 @@ IntrusivePtr<Func> zeek::id::lookup_func(const char* name)
return nullptr;
if ( ! IsFunc(v->GetType()->Tag()) )
reporter->InternalError("Expected variable '%s' to be a function", name);
reporter->InternalError("Expected variable '%s' to be a function",
std::string(name).data());
return {NewRef{}, v->AsFunc()};
}

View file

@ -10,6 +10,7 @@
#include <map>
#include <string>
#include <string_view>
#include <vector>
class Val;
@ -165,7 +166,7 @@ namespace zeek { namespace id {
* @return The identifier, which may reference a nil object if no such
* name exists.
*/
const IntrusivePtr<ID>& lookup(const char* name);
const IntrusivePtr<ID>& lookup(std::string_view name);
/**
* Lookup an ID by its name and return its type. A fatal occurs if the ID
@ -173,7 +174,7 @@ const IntrusivePtr<ID>& lookup(const char* name);
* @param name The identifier name to lookup
* @return The type of the identifier.
*/
const IntrusivePtr<BroType>& lookup_type(const char* name);
const IntrusivePtr<BroType>& lookup_type(std::string_view name);
/**
* Lookup an ID by its name and return its type (as cast to @c T).
@ -182,7 +183,7 @@ const IntrusivePtr<BroType>& lookup_type(const char* name);
* @return The type of the identifier.
*/
template<class T>
IntrusivePtr<T> lookup_type(const char* name)
IntrusivePtr<T> lookup_type(std::string_view name)
{ return cast_intrusive<T>(lookup_type(name)); }
/**
@ -191,7 +192,7 @@ IntrusivePtr<T> lookup_type(const char* name)
* @param name The identifier name to lookup
* @return The current value of the identifier
*/
const IntrusivePtr<Val>& lookup_val(const char* name);
const IntrusivePtr<Val>& lookup_val(std::string_view name);
/**
* Lookup an ID by its name and return its value (as cast to @c T).
@ -200,7 +201,7 @@ const IntrusivePtr<Val>& lookup_val(const char* name);
* @return The current value of the identifier.
*/
template<class T>
IntrusivePtr<T> lookup_val(const char* name)
IntrusivePtr<T> lookup_val(std::string_view name)
{ return cast_intrusive<T>(lookup_val(name)); }
/**
@ -209,7 +210,7 @@ IntrusivePtr<T> lookup_val(const char* name)
* @param name The identifier name to lookup
* @return The current value of the identifier
*/
const IntrusivePtr<Val>& lookup_const(const char* name);
const IntrusivePtr<Val>& lookup_const(std::string_view name);
/**
* Lookup an ID by its name and return the function it references.
@ -217,7 +218,7 @@ const IntrusivePtr<Val>& lookup_const(const char* name);
* @param name The identifier name to lookup
* @return The current function value the identifier references.
*/
IntrusivePtr<Func> lookup_func(const char* name);
IntrusivePtr<Func> lookup_func(std::string_view name);
extern IntrusivePtr<RecordType> conn_id;
extern IntrusivePtr<RecordType> endpoint;