Change Scope::Find() and Scope::Remove() to use std::string_view

This commit is contained in:
Jon Siwek 2020-05-12 23:01:09 -07:00
parent 0af7f8141b
commit 86cbab3b7f
2 changed files with 31 additions and 32 deletions

View file

@ -14,7 +14,7 @@ typedef PList<Scope> scope_list;
static scope_list scopes; static scope_list scopes;
static Scope* top_scope; static Scope* top_scope;
static IntrusivePtr<ID> nil_id;
Scope::Scope(IntrusivePtr<ID> id, attr_list* al) Scope::Scope(IntrusivePtr<ID> id, attr_list* al)
: scope_id(std::move(id)) : 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) ID* Scope::GenerateTemporary(const char* name)
{ {
return new ID(name, SCOPE_FUNCTION, false); 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); return global_scope()->Find(globalname);
} }
static IntrusivePtr<ID> nil; return nil_id;
return nil;
} }
IntrusivePtr<ID> install_ID(const char* name, const char* module_name, IntrusivePtr<ID> install_ID(const char* name, const char* module_name,

View file

@ -4,6 +4,7 @@
#include <utility> #include <utility>
#include <string> #include <string>
#include <string_view>
#include <map> #include <map>
#include "Obj.h" #include "Obj.h"
@ -21,42 +22,17 @@ public:
explicit Scope(IntrusivePtr<ID> id, attr_list* al); explicit Scope(IntrusivePtr<ID> id, attr_list* al);
~Scope() override; ~Scope() override;
template<typename N> const IntrusivePtr<ID>& Find(std::string_view name) const;
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;
}
template<typename N> template<typename N>
[[deprecated("Remove in v4.1. Use Find().")]] [[deprecated("Remove in v4.1. Use Find().")]]
ID* Lookup(N&& name) const ID* Lookup(N&& name) const
{ { return Find(name).get(); }
return Find(name).get();
}
template<typename N, typename I> template<typename N, typename I>
void Insert(N&& name, I&& id) { local[std::forward<N>(name)] = std::forward<I>(id); } void Insert(N&& name, I&& id) { local[std::forward<N>(name)] = std::forward<I>(id); }
template<typename N> IntrusivePtr<ID> Remove(std::string_view name);
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;
}
ID* ScopeID() const { return scope_id.get(); } ID* ScopeID() const { return scope_id.get(); }
attr_list* Attrs() const { return attrs; } attr_list* Attrs() const { return attrs; }
@ -82,7 +58,7 @@ protected:
IntrusivePtr<ID> scope_id; IntrusivePtr<ID> scope_id;
attr_list* attrs; attr_list* attrs;
IntrusivePtr<BroType> return_type; IntrusivePtr<BroType> return_type;
std::map<std::string, IntrusivePtr<ID>> local; std::map<std::string, IntrusivePtr<ID>, std::less<>> local;
id_list* inits; id_list* inits;
}; };