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/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 )

View file

@ -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

View file

@ -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<EventHandler>(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());
}

View file

@ -5,6 +5,7 @@
#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
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<std::string, std::unique_ptr<EventHandler>> handlers;
std::map<std::string, std::unique_ptr<EventHandler>, std::less<>> handlers;
};
extern EventRegistry* event_registry;