mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
EventRegistry: remove uses of PDict
This commit is contained in:
parent
6fa0f4ac49
commit
acff8d5a2b
3 changed files with 45 additions and 68 deletions
|
@ -4,85 +4,67 @@
|
|||
|
||||
void EventRegistry::Register(EventHandlerPtr handler)
|
||||
{
|
||||
HashKey key(handler->Name());
|
||||
handlers.Insert(&key, handler.Ptr());
|
||||
handlers[string(handler->Name())] = handler.Ptr();
|
||||
}
|
||||
|
||||
EventHandler* EventRegistry::Lookup(const char* name)
|
||||
EventHandler* EventRegistry::Lookup(const string& name)
|
||||
{
|
||||
HashKey key(name);
|
||||
return handlers.Lookup(&key);
|
||||
auto it = handlers.find(name);
|
||||
if ( it != handlers.end() )
|
||||
return it->second;
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
EventRegistry::string_list* EventRegistry::Match(RE_Matcher* pattern)
|
||||
EventRegistry::string_list EventRegistry::Match(RE_Matcher* pattern)
|
||||
{
|
||||
string_list* names = new string_list;
|
||||
string_list names;
|
||||
|
||||
IterCookie* c = handlers.InitForIteration();
|
||||
|
||||
HashKey* k;
|
||||
EventHandler* v;
|
||||
while ( (v = handlers.NextEntry(k, c)) )
|
||||
for ( const auto& entry : handlers )
|
||||
{
|
||||
EventHandler* v = entry.second;
|
||||
if ( v->LocalHandler() && pattern->MatchExactly(v->Name()) )
|
||||
names->push_back(v->Name());
|
||||
|
||||
delete k;
|
||||
names.push_back(entry.first);
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
EventRegistry::string_list* EventRegistry::UnusedHandlers()
|
||||
EventRegistry::string_list EventRegistry::UnusedHandlers()
|
||||
{
|
||||
string_list* names = new string_list;
|
||||
string_list names;
|
||||
|
||||
IterCookie* c = handlers.InitForIteration();
|
||||
|
||||
HashKey* k;
|
||||
EventHandler* v;
|
||||
while ( (v = handlers.NextEntry(k, c)) )
|
||||
for ( const auto& entry : handlers )
|
||||
{
|
||||
EventHandler* v = entry.second;
|
||||
if ( v->LocalHandler() && ! v->Used() )
|
||||
names->push_back(v->Name());
|
||||
|
||||
delete k;
|
||||
names.push_back(entry.first);
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
EventRegistry::string_list* EventRegistry::UsedHandlers()
|
||||
EventRegistry::string_list EventRegistry::UsedHandlers()
|
||||
{
|
||||
string_list* names = new string_list;
|
||||
string_list names;
|
||||
|
||||
IterCookie* c = handlers.InitForIteration();
|
||||
|
||||
HashKey* k;
|
||||
EventHandler* v;
|
||||
while ( (v = handlers.NextEntry(k, c)) )
|
||||
for ( const auto& entry : handlers )
|
||||
{
|
||||
EventHandler* v = entry.second;
|
||||
if ( v->LocalHandler() && v->Used() )
|
||||
names->push_back(v->Name());
|
||||
|
||||
delete k;
|
||||
names.push_back(entry.first);
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
EventRegistry::string_list* EventRegistry::AllHandlers()
|
||||
EventRegistry::string_list EventRegistry::AllHandlers()
|
||||
{
|
||||
string_list* names = new string_list(handlers.Length());
|
||||
string_list names;
|
||||
|
||||
IterCookie* c = handlers.InitForIteration();
|
||||
|
||||
HashKey* k;
|
||||
EventHandler* v;
|
||||
while ( (v = handlers.NextEntry(k, c)) )
|
||||
for ( const auto& entry : handlers )
|
||||
{
|
||||
names->push_back(v->Name());
|
||||
delete k;
|
||||
names.push_back(entry.first);
|
||||
}
|
||||
|
||||
return names;
|
||||
|
@ -90,13 +72,9 @@ EventRegistry::string_list* EventRegistry::AllHandlers()
|
|||
|
||||
void EventRegistry::PrintDebug()
|
||||
{
|
||||
IterCookie* c = handlers.InitForIteration();
|
||||
|
||||
HashKey* k;
|
||||
EventHandler* v;
|
||||
while ( (v = handlers.NextEntry(k, c)) )
|
||||
for ( const auto& entry : handlers )
|
||||
{
|
||||
delete k;
|
||||
EventHandler* v = entry.second;
|
||||
fprintf(stderr, "Registered event %s (%s handler / %s)\n", v->Name(),
|
||||
v->LocalHandler()? "local" : "no",
|
||||
*v ? "active" : "not active"
|
||||
|
@ -104,7 +82,7 @@ void EventRegistry::PrintDebug()
|
|||
}
|
||||
}
|
||||
|
||||
void EventRegistry::SetErrorHandler(const char* name)
|
||||
void EventRegistry::SetErrorHandler(const string& name)
|
||||
{
|
||||
EventHandler* eh = Lookup(name);
|
||||
|
||||
|
@ -115,6 +93,6 @@ void EventRegistry::SetErrorHandler(const char* name)
|
|||
}
|
||||
|
||||
reporter->InternalWarning(
|
||||
"unknown event handler '%s' in SetErrorHandler()", name);
|
||||
"unknown event handler '%s' in SetErrorHandler()", name.c_str());
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
#ifndef EVENT_REGISTRY
|
||||
#define EVENT_REGISTRY
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "Func.h"
|
||||
#include "List.h"
|
||||
#include "Dict.h"
|
||||
|
@ -17,28 +20,26 @@ public:
|
|||
void Register(EventHandlerPtr handler);
|
||||
|
||||
// Return nil if unknown.
|
||||
EventHandler* Lookup(const char* name);
|
||||
EventHandler* Lookup(const string& name);
|
||||
|
||||
// Returns a list of all local handlers that match the given pattern.
|
||||
// Passes ownership of list.
|
||||
typedef const char constchar; // PList doesn't like "const char"
|
||||
typedef PList<constchar> string_list;
|
||||
string_list* Match(RE_Matcher* pattern);
|
||||
typedef vector<string> string_list;
|
||||
string_list Match(RE_Matcher* pattern);
|
||||
|
||||
// Marks a handler as handling errors. Error handler will not be called
|
||||
// recursively to avoid infinite loops in case they trigger an error
|
||||
// themselves.
|
||||
void SetErrorHandler(const char* name);
|
||||
void SetErrorHandler(const string& name);
|
||||
|
||||
string_list* UnusedHandlers();
|
||||
string_list* UsedHandlers();
|
||||
string_list* AllHandlers();
|
||||
string_list UnusedHandlers();
|
||||
string_list UsedHandlers();
|
||||
string_list AllHandlers();
|
||||
|
||||
void PrintDebug();
|
||||
|
||||
private:
|
||||
typedef PDict<EventHandler> handler_map;
|
||||
handler_map handlers;
|
||||
std::map<std::string, EventHandler*> handlers;
|
||||
};
|
||||
|
||||
extern EventRegistry* event_registry;
|
||||
|
|
10
src/main.cc
10
src/main.cc
|
@ -982,17 +982,15 @@ int main(int argc, char** argv)
|
|||
if ( zeek_init ) //### this should be a function
|
||||
mgr.QueueEventFast(zeek_init, val_list{});
|
||||
|
||||
EventRegistry::string_list* dead_handlers =
|
||||
EventRegistry::string_list dead_handlers =
|
||||
event_registry->UnusedHandlers();
|
||||
|
||||
if ( dead_handlers->length() > 0 && check_for_unused_event_handlers )
|
||||
if ( ! dead_handlers.empty() && check_for_unused_event_handlers )
|
||||
{
|
||||
for ( int i = 0; i < dead_handlers->length(); ++i )
|
||||
reporter->Warning("event handler never invoked: %s", (*dead_handlers)[i]);
|
||||
for ( const string& handler : dead_handlers )
|
||||
reporter->Warning("event handler never invoked: %s", handler.c_str());
|
||||
}
|
||||
|
||||
delete dead_handlers;
|
||||
|
||||
if ( stmts )
|
||||
{
|
||||
stmt_flow_type flow;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue