Change Type::type_aliases map to store IntrusivePtr

And deprecate Type::GetAliases() and Type::AddAlias() since they
took raw pointers.  Now replaced with Type::Aliases() and
Type::RegisterAlias().
This commit is contained in:
Jon Siwek 2020-11-06 17:18:20 -08:00
parent bfb7afc600
commit 1dda387ac9
5 changed files with 65 additions and 13 deletions

3
NEWS
View file

@ -134,6 +134,9 @@ Deprecated Functionality
- Marked the Continuation.h and PacketDumper.h files as deprecated. The code - Marked the Continuation.h and PacketDumper.h files as deprecated. The code
contained within them is unused by Zeek. contained within them is unused by Zeek.
- ``Type::GetAliases()`` and ``Type::AddAlias()`` are deprecated, use
``Type::Aliases()`` and ``Type::RegisterAlias()``.
Zeek 3.2.0 Zeek 3.2.0
========== ==========

View file

@ -1348,13 +1348,11 @@ void EnumType::CheckAndAddName(const string& module_name, const char* name,
if ( vals.find(val) == vals.end() ) if ( vals.find(val) == vals.end() )
vals[val] = make_intrusive<EnumVal>(IntrusivePtr{NewRef{}, this}, val); vals[val] = make_intrusive<EnumVal>(IntrusivePtr{NewRef{}, this}, val);
set<Type*> types = Type::GetAliases(GetName()); const auto& types = Type::Aliases(GetName());
set<Type*>::const_iterator it;
for ( it = types.begin(); it != types.end(); ++it ) for ( const auto& t : types )
if ( *it != this ) if ( t.get() != this )
(*it)->AsEnumType()->AddNameInternal(module_name, name, val, t->AsEnumType()->AddNameInternal(module_name, name, val, is_export);
is_export);
} }
void EnumType::AddNameInternal(const string& module_name, const char* name, void EnumType::AddNameInternal(const string& module_name, const char* name,

View file

@ -270,13 +270,64 @@ public:
void SetName(const std::string& arg_name) { name = arg_name; } void SetName(const std::string& arg_name) { name = arg_name; }
const std::string& GetName() const { return name; } const std::string& GetName() const { return name; }
typedef std::map<std::string, std::set<Type*> > TypeAliasMap; struct TypePtrComparer {
bool operator()(const TypePtr& a, const TypePtr& b) const
{ return a.get() < b.get(); }
};
using TypePtrSet = std::set<TypePtr, TypePtrComparer>;
using TypeAliasMap = std::map<std::string, TypePtrSet, std::less<>>;
/**
* Returns a mapping of type-name to all other type names declared as
* an alias to it.
*/
static const TypeAliasMap& GetAliasMap()
{ return type_aliases; }
/**
* Returns true if the given type name has any declared aliases
*/
static bool HasAliases(std::string_view type_name)
{ return Type::type_aliases.find(type_name) != Type::type_aliases.end(); }
/**
* Returns the set of all type names declared as an aliases to the given
* type name. A static empty set is returned if there are no aliases.
*/
static const TypePtrSet& Aliases(std::string_view type_name)
{
static TypePtrSet empty;
auto it = Type::type_aliases.find(type_name);
return it == Type::type_aliases.end() ? empty : it->second;
}
[[deprecated("Remove in v4.1. Use zeek::Type::Aliases() instead.")]]
static std::set<Type*> GetAliases(const std::string& type_name) static std::set<Type*> GetAliases(const std::string& type_name)
{ return Type::type_aliases[type_name]; } {
std::set<Type*> rval;
for ( const auto& t : Type::type_aliases[type_name] )
rval.emplace(t.get());
return rval;
}
/**
* Registers a new type alias.
* @param type_name the name of the type to register a new alias for.
* @param type the associated alias type of *type_name*.
* @return true if the alias is now registered or false if the alias was
* already previously registered.
*/
static bool RegisterAlias(std::string_view type_name, TypePtr type)
{
auto it = Type::type_aliases.find(type_name);
if ( it == Type::type_aliases.end() )
it = Type::type_aliases.emplace(std::string{type_name}, TypePtrSet{}).first;
return it->second.emplace(std::move(type)).second;
}
[[deprecated("Remove in v4.1. Use zeek::Type::RegisterAlias().")]]
static void AddAlias(const std::string &type_name, Type* type) static void AddAlias(const std::string &type_name, Type* type)
{ Type::type_aliases[type_name].insert(type); } { Type::type_aliases[type_name].insert({NewRef{}, type}); }
protected: protected:
Type() = default; Type() = default;

View file

@ -382,10 +382,10 @@ void add_type(ID* id, TypePtr t, std::unique_ptr<std::vector<AttrPtr>> attr)
// Clone the type to preserve type name aliasing. // Clone the type to preserve type name aliasing.
tnew = t->ShallowClone(); tnew = t->ShallowClone();
Type::AddAlias(new_type_name, tnew.get()); Type::RegisterAlias(new_type_name, tnew);
if ( new_type_name != old_type_name && ! old_type_name.empty() ) if ( new_type_name != old_type_name && ! old_type_name.empty() )
Type::AddAlias(old_type_name, tnew.get()); Type::RegisterAlias(old_type_name, tnew);
tnew->SetName(id->Name()); tnew->SetName(id->Name());

View file

@ -171,7 +171,7 @@ static void parser_redef_enum (zeek::detail::ID *id)
static void extend_record(zeek::detail::ID* id, std::unique_ptr<zeek::type_decl_list> fields, static void extend_record(zeek::detail::ID* id, std::unique_ptr<zeek::type_decl_list> fields,
std::unique_ptr<std::vector<zeek::detail::AttrPtr>> attrs) std::unique_ptr<std::vector<zeek::detail::AttrPtr>> attrs)
{ {
std::set<zeek::Type*> types = zeek::Type::GetAliases(id->Name()); const auto& types = zeek::Type::Aliases(id->Name());
if ( types.empty() ) if ( types.empty() )
{ {
@ -189,7 +189,7 @@ static void extend_record(zeek::detail::ID* id, std::unique_ptr<zeek::type_decl_
break; break;
} }
for ( auto t : types ) for ( const auto& t : types )
{ {
auto error = t->AsRecordType()->AddFields(*fields, add_log_attr); auto error = t->AsRecordType()->AddFields(*fields, add_log_attr);