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