mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
Change Scope::Find() and Scope::Remove() to use std::string_view
This commit is contained in:
parent
0af7f8141b
commit
86cbab3b7f
2 changed files with 31 additions and 32 deletions
29
src/Scope.cc
29
src/Scope.cc
|
@ -14,7 +14,7 @@ typedef PList<Scope> scope_list;
|
|||
|
||||
static scope_list scopes;
|
||||
static Scope* top_scope;
|
||||
|
||||
static IntrusivePtr<ID> nil_id;
|
||||
|
||||
Scope::Scope(IntrusivePtr<ID> id, attr_list* al)
|
||||
: scope_id(std::move(id))
|
||||
|
@ -57,6 +57,30 @@ Scope::~Scope()
|
|||
}
|
||||
}
|
||||
|
||||
const IntrusivePtr<ID>& Scope::Find(std::string_view name) const
|
||||
{
|
||||
auto entry = local.find(name);
|
||||
|
||||
if ( entry != local.end() )
|
||||
return entry->second;
|
||||
|
||||
return nil_id;
|
||||
}
|
||||
|
||||
IntrusivePtr<ID> Scope::Remove(std::string_view name)
|
||||
{
|
||||
auto entry = local.find(name);
|
||||
|
||||
if ( entry != local.end() )
|
||||
{
|
||||
auto id = std::move(entry->second);
|
||||
local.erase(entry);
|
||||
return id;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ID* Scope::GenerateTemporary(const char* name)
|
||||
{
|
||||
return new ID(name, SCOPE_FUNCTION, false);
|
||||
|
@ -148,8 +172,7 @@ const IntrusivePtr<ID>& lookup_ID(const char* name, const char* curr_module,
|
|||
return global_scope()->Find(globalname);
|
||||
}
|
||||
|
||||
static IntrusivePtr<ID> nil;
|
||||
return nil;
|
||||
return nil_id;
|
||||
}
|
||||
|
||||
IntrusivePtr<ID> install_ID(const char* name, const char* module_name,
|
||||
|
|
34
src/Scope.h
34
src/Scope.h
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <map>
|
||||
|
||||
#include "Obj.h"
|
||||
|
@ -21,42 +22,17 @@ public:
|
|||
explicit Scope(IntrusivePtr<ID> id, attr_list* al);
|
||||
~Scope() override;
|
||||
|
||||
template<typename N>
|
||||
const IntrusivePtr<ID>& Find(N&& name) const
|
||||
{
|
||||
static IntrusivePtr<ID> nil;
|
||||
const auto& entry = local.find(std::forward<N>(name));
|
||||
|
||||
if ( entry != local.end() )
|
||||
return entry->second;
|
||||
|
||||
return nil;
|
||||
}
|
||||
const IntrusivePtr<ID>& Find(std::string_view name) const;
|
||||
|
||||
template<typename N>
|
||||
[[deprecated("Remove in v4.1. Use Find().")]]
|
||||
ID* Lookup(N&& name) const
|
||||
{
|
||||
return Find(name).get();
|
||||
}
|
||||
{ return Find(name).get(); }
|
||||
|
||||
template<typename N, typename I>
|
||||
void Insert(N&& name, I&& id) { local[std::forward<N>(name)] = std::forward<I>(id); }
|
||||
|
||||
template<typename N>
|
||||
IntrusivePtr<ID> Remove(N&& name)
|
||||
{
|
||||
const auto& entry = local.find(std::forward<N>(name));
|
||||
|
||||
if ( entry != local.end() )
|
||||
{
|
||||
auto id = std::move(entry->second);
|
||||
local.erase(entry);
|
||||
return id;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
IntrusivePtr<ID> Remove(std::string_view name);
|
||||
|
||||
ID* ScopeID() const { return scope_id.get(); }
|
||||
attr_list* Attrs() const { return attrs; }
|
||||
|
@ -82,7 +58,7 @@ protected:
|
|||
IntrusivePtr<ID> scope_id;
|
||||
attr_list* attrs;
|
||||
IntrusivePtr<BroType> return_type;
|
||||
std::map<std::string, IntrusivePtr<ID>> local;
|
||||
std::map<std::string, IntrusivePtr<ID>, std::less<>> local;
|
||||
id_list* inits;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue