From dca587c6043ba96c7fe86a7b303c1ae7025af019 Mon Sep 17 00:00:00 2001 From: Jon Siwek Date: Wed, 13 May 2020 18:42:08 -0700 Subject: [PATCH] Change EventRegistry/EventHandler methods to use std::string{_view} --- src/EventHandler.cc | 7 +++---- src/EventHandler.h | 6 +++--- src/EventRegistry.cc | 10 +++++----- src/EventRegistry.h | 9 +++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/EventHandler.cc b/src/EventHandler.cc index b4ccbd11fc..f9c3e72d47 100644 --- a/src/EventHandler.cc +++ b/src/EventHandler.cc @@ -10,9 +10,9 @@ #include "broker/Manager.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; local = nullptr; type = nullptr; @@ -24,7 +24,6 @@ EventHandler::EventHandler(const char* arg_name) EventHandler::~EventHandler() { Unref(local); - delete [] name; } EventHandler::operator bool() const @@ -39,7 +38,7 @@ FuncType* EventHandler::FType(bool check_export) if ( 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); if ( ! id ) diff --git a/src/EventHandler.h b/src/EventHandler.h index 59773560f6..6844662b22 100644 --- a/src/EventHandler.h +++ b/src/EventHandler.h @@ -13,10 +13,10 @@ class FuncType; class EventHandler { public: - explicit EventHandler(const char* name); + explicit EventHandler(std::string name); ~EventHandler(); - const char* Name() { return name; } + const char* Name() { return name.data(); } Func* LocalHandler() { return local; } FuncType* FType(bool check_export = true); @@ -55,7 +55,7 @@ public: private: void NewEvent(const zeek::Args& vl); // Raise new_event() meta event. - const char* name; + std::string name; Func* local; FuncType* type; bool used; // this handler is indeed used somewhere diff --git a/src/EventRegistry.cc b/src/EventRegistry.cc index 5bd2654924..ea6f3a0ca2 100644 --- a/src/EventRegistry.cc +++ b/src/EventRegistry.cc @@ -6,7 +6,7 @@ EventRegistry::EventRegistry() = 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 // local handler on the script layer. @@ -18,7 +18,7 @@ EventHandlerPtr EventRegistry::Register(const char* name) return h; } - h = new EventHandler(name); + h = new EventHandler(std::string(name)); event_registry->Register(h); h->SetUsed(); @@ -31,7 +31,7 @@ void EventRegistry::Register(EventHandlerPtr handler) handlers[std::string(handler->Name())] = std::unique_ptr(handler.Ptr()); } -EventHandler* EventRegistry::Lookup(const std::string& name) +EventHandler* EventRegistry::Lookup(std::string_view name) { auto it = handlers.find(name); 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); @@ -117,6 +117,6 @@ void EventRegistry::SetErrorHandler(const std::string& name) } reporter->InternalWarning("unknown event handler '%s' in SetErrorHandler()", - name.c_str()); + std::string(name).c_str()); } diff --git a/src/EventRegistry.h b/src/EventRegistry.h index 7b8055ec51..13f3fcc5c4 100644 --- a/src/EventRegistry.h +++ b/src/EventRegistry.h @@ -5,6 +5,7 @@ #include #include #include +#include #include class EventHandler; @@ -23,12 +24,12 @@ public: * @param name The name of the event handler to lookup/register. * @return The event handler. */ - EventHandlerPtr Register(const char* name); + EventHandlerPtr Register(std::string_view name); void Register(EventHandlerPtr handler); // 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. // Passes ownership of list. @@ -38,7 +39,7 @@ public: // 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 std::string& name); + void SetErrorHandler(std::string_view name); string_list UnusedHandlers(); string_list UsedHandlers(); @@ -47,7 +48,7 @@ public: void PrintDebug(); private: - std::map> handlers; + std::map, std::less<>> handlers; }; extern EventRegistry* event_registry;