Some minor c++ modernization in EnumType methods

This commit is contained in:
Tim Wojtulewicz 2025-04-02 12:46:45 -07:00
parent c22e54604f
commit 08101eb372
2 changed files with 8 additions and 14 deletions

View file

@ -1617,12 +1617,10 @@ zeek_int_t EnumType::Lookup(const string& module_name, const char* name) const {
} }
zeek_int_t EnumType::Lookup(const string& full_name) const { zeek_int_t EnumType::Lookup(const string& full_name) const {
auto pos = names.find(full_name.c_str()); if ( auto pos = names.find(full_name.c_str()); pos != names.end() )
if ( pos == names.end() )
return -1;
else
return pos->second; return pos->second;
return -1;
} }
const char* EnumType::Lookup(zeek_int_t value) const { const char* EnumType::Lookup(zeek_int_t value) const {
@ -1641,16 +1639,13 @@ EnumType::enum_name_list EnumType::Names() const {
} }
const EnumValPtr& EnumType::GetEnumVal(zeek_int_t i) { const EnumValPtr& EnumType::GetEnumVal(zeek_int_t i) {
auto it = vals.find(i); if ( auto it = vals.find(i); it != vals.end() )
return it->second;
if ( it == vals.end() ) {
auto ev = make_intrusive<EnumVal>(IntrusivePtr{NewRef{}, this}, i); auto ev = make_intrusive<EnumVal>(IntrusivePtr{NewRef{}, this}, i);
return vals.emplace(i, std::move(ev)).first->second; return vals.emplace(i, std::move(ev)).first->second;
} }
return it->second;
}
void EnumType::DoDescribe(ODesc* d) const { void EnumType::DoDescribe(ODesc* d) const {
auto t = Tag(); auto t = Tag();

View file

@ -869,8 +869,7 @@ protected:
// Whether any of the elements of the enum were added via redef's. // Whether any of the elements of the enum were added via redef's.
bool has_redefs = false; bool has_redefs = false;
using ValMap = std::unordered_map<zeek_int_t, EnumValPtr>; std::unordered_map<zeek_int_t, EnumValPtr> vals;
ValMap vals;
// The counter is initialized to 0 and incremented on every implicit // The counter is initialized to 0 and incremented on every implicit
// auto-increment name that gets added (thus its > 0 if // auto-increment name that gets added (thus its > 0 if