Change EventRegistry/EventHandler methods to use std::string{_view}

This commit is contained in:
Jon Siwek 2020-05-13 18:42:08 -07:00
parent 78e3267c44
commit dca587c604
4 changed files with 16 additions and 16 deletions

View file

@ -10,9 +10,9 @@
#include "broker/Manager.h" #include "broker/Manager.h"
#include "broker/Data.h" #include "broker/Data.h"
EventHandler::EventHandler(const char* arg_name) EventHandler::EventHandler(std::string arg_name)
{ {
name = copy_string(arg_name); name = std::move(arg_name);
used = false; used = false;
local = nullptr; local = nullptr;
type = nullptr; type = nullptr;
@ -24,7 +24,6 @@ EventHandler::EventHandler(const char* arg_name)
EventHandler::~EventHandler() EventHandler::~EventHandler()
{ {
Unref(local); Unref(local);
delete [] name;
} }
EventHandler::operator bool() const EventHandler::operator bool() const
@ -39,7 +38,7 @@ FuncType* EventHandler::FType(bool check_export)
if ( type ) if ( type )
return type; return type;
const auto& id = lookup_ID(name, current_module.c_str(), false, false, const auto& id = lookup_ID(name.data(), current_module.c_str(), false, false,
check_export); check_export);
if ( ! id ) if ( ! id )

View file

@ -13,10 +13,10 @@ class FuncType;
class EventHandler { class EventHandler {
public: public:
explicit EventHandler(const char* name); explicit EventHandler(std::string name);
~EventHandler(); ~EventHandler();
const char* Name() { return name; } const char* Name() { return name.data(); }
Func* LocalHandler() { return local; } Func* LocalHandler() { return local; }
FuncType* FType(bool check_export = true); FuncType* FType(bool check_export = true);
@ -55,7 +55,7 @@ public:
private: private:
void NewEvent(const zeek::Args& vl); // Raise new_event() meta event. void NewEvent(const zeek::Args& vl); // Raise new_event() meta event.
const char* name; std::string name;
Func* local; Func* local;
FuncType* type; FuncType* type;
bool used; // this handler is indeed used somewhere bool used; // this handler is indeed used somewhere

View file

@ -6,7 +6,7 @@
EventRegistry::EventRegistry() = default; EventRegistry::EventRegistry() = default;
EventRegistry::~EventRegistry() noexcept = default; EventRegistry::~EventRegistry() noexcept = default;
EventHandlerPtr EventRegistry::Register(const char* name) EventHandlerPtr EventRegistry::Register(std::string_view name)
{ {
// If there already is an entry in the registry, we have a // If there already is an entry in the registry, we have a
// local handler on the script layer. // local handler on the script layer.
@ -18,7 +18,7 @@ EventHandlerPtr EventRegistry::Register(const char* name)
return h; return h;
} }
h = new EventHandler(name); h = new EventHandler(std::string(name));
event_registry->Register(h); event_registry->Register(h);
h->SetUsed(); h->SetUsed();
@ -31,7 +31,7 @@ void EventRegistry::Register(EventHandlerPtr handler)
handlers[std::string(handler->Name())] = std::unique_ptr<EventHandler>(handler.Ptr()); handlers[std::string(handler->Name())] = std::unique_ptr<EventHandler>(handler.Ptr());
} }
EventHandler* EventRegistry::Lookup(const std::string& name) EventHandler* EventRegistry::Lookup(std::string_view name)
{ {
auto it = handlers.find(name); auto it = handlers.find(name);
if ( it != handlers.end() ) if ( it != handlers.end() )
@ -106,7 +106,7 @@ void EventRegistry::PrintDebug()
} }
} }
void EventRegistry::SetErrorHandler(const std::string& name) void EventRegistry::SetErrorHandler(std::string_view name)
{ {
EventHandler* eh = Lookup(name); EventHandler* eh = Lookup(name);
@ -117,6 +117,6 @@ void EventRegistry::SetErrorHandler(const std::string& name)
} }
reporter->InternalWarning("unknown event handler '%s' in SetErrorHandler()", reporter->InternalWarning("unknown event handler '%s' in SetErrorHandler()",
name.c_str()); std::string(name).c_str());
} }

View file

@ -5,6 +5,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
class EventHandler; class EventHandler;
@ -23,12 +24,12 @@ public:
* @param name The name of the event handler to lookup/register. * @param name The name of the event handler to lookup/register.
* @return The event handler. * @return The event handler.
*/ */
EventHandlerPtr Register(const char* name); EventHandlerPtr Register(std::string_view name);
void Register(EventHandlerPtr handler); void Register(EventHandlerPtr handler);
// Return nil if unknown. // Return nil if unknown.
EventHandler* Lookup(const std::string& name); EventHandler* Lookup(std::string_view 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.
@ -38,7 +39,7 @@ public:
// 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 std::string& name); void SetErrorHandler(std::string_view name);
string_list UnusedHandlers(); string_list UnusedHandlers();
string_list UsedHandlers(); string_list UsedHandlers();
@ -47,7 +48,7 @@ public:
void PrintDebug(); void PrintDebug();
private: private:
std::map<std::string, std::unique_ptr<EventHandler>> handlers; std::map<std::string, std::unique_ptr<EventHandler>, std::less<>> handlers;
}; };
extern EventRegistry* event_registry; extern EventRegistry* event_registry;