From 26ac315192e0d8a04d50e910697ab794eedf0cc1 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 20 Apr 2014 14:35:14 -0400 Subject: New event system --- include/service.h | 128 ++++++++++++++---------------------------------------- 1 file changed, 33 insertions(+), 95 deletions(-) (limited to 'include/service.h') diff --git a/include/service.h b/include/service.h index 32686afb5..d8287fc52 100644 --- a/include/service.h +++ b/include/service.h @@ -23,62 +23,21 @@ */ class CoreExport Service : public virtual Base { - static std::map > Services; - static std::map > Aliases; + static std::map > *Services; + static std::map > *Aliases; - static Service *FindService(const std::map &services, const std::map *aliases, const Anope::string &n) - { - std::map::const_iterator it = services.find(n); - if (it != services.end()) - return it->second; + static void check(); - if (aliases != NULL) - { - std::map::const_iterator it2 = aliases->find(n); - if (it2 != aliases->end()) - return FindService(services, aliases, it2->second); - } - - return NULL; - } + static Service *FindService(const std::map &services, const std::map *aliases, const Anope::string &n); public: - static Service *FindService(const Anope::string &t, const Anope::string &n) - { - std::map >::const_iterator it = Services.find(t); - if (it == Services.end()) - return NULL; - - std::map >::const_iterator it2 = Aliases.find(t); - if (it2 != Aliases.end()) - return FindService(it->second, &it2->second, n); - - return FindService(it->second, NULL, n); - } + static Service *FindService(const Anope::string &t, const Anope::string &n); - static std::vector GetServiceKeys(const Anope::string &t) - { - std::vector keys; - std::map >::iterator it = Services.find(t); - if (it != Services.end()) - for (std::map::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) - keys.push_back(it2->first); - return keys; - } + static std::vector GetServiceKeys(const Anope::string &t); - static void AddAlias(const Anope::string &t, const Anope::string &n, const Anope::string &v) - { - std::map &smap = Aliases[t]; - smap[n] = v; - } + static void AddAlias(const Anope::string &t, const Anope::string &n, const Anope::string &v); - static void DelAlias(const Anope::string &t, const Anope::string &n) - { - std::map &smap = Aliases[t]; - smap.erase(n); - if (smap.empty()) - Aliases.erase(t); - } + static void DelAlias(const Anope::string &t, const Anope::string &n); Module *owner; /* Service type, which should be the class name (eg "Command") */ @@ -86,31 +45,15 @@ class CoreExport Service : public virtual Base /* Service name, commands are usually named service/command */ Anope::string name; - Service(Module *o, const Anope::string &t, const Anope::string &n) : owner(o), type(t), name(n) - { - this->Register(); - } + Service(Module *o, const Anope::string &t, const Anope::string &n); - virtual ~Service() - { - this->Unregister(); - } + Service(const Service &) = delete; - void Register() - { - std::map &smap = Services[this->type]; - if (smap.find(this->name) != smap.end()) - throw ModuleException("Service " + this->type + " with name " + this->name + " already exists"); - smap[this->name] = this; - } + virtual ~Service(); - void Unregister() - { - std::map &smap = Services[this->type]; - smap.erase(this->name); - if (smap.empty()) - Services.erase(this->type); - } + void Register(); + + void Unregister(); }; /** Like Reference, but used to refer to Services. @@ -120,6 +63,7 @@ class ServiceReference : public Reference { Anope::string type; Anope::string name; + bool need_check = true; public: ServiceReference() { } @@ -128,45 +72,39 @@ class ServiceReference : public Reference { } - inline void operator=(const Anope::string &n) + void operator=(const Anope::string &n) { this->name = n; - this->invalid = true; } - operator bool() override + void Check() override { - if (this->invalid) - { - this->invalid = false; - this->ref = NULL; - } - if (!this->ref) + if (need_check) { - /* This really could be dynamic_cast in every case, except for when a module - * creates its own service type (that other modules must include the header file - * for), as the core is not compiled with it so there is no RTTI for it. - */ - this->ref = static_cast(::Service::FindService(this->type, this->name)); + need_check = false; + + this->ref = static_cast(Service::FindService(this->type, this->name)); if (this->ref) + { this->ref->AddReference(this); + this->OnAcquire(); + } } - return this->ref; } + + void Reset() override + { + need_check = true; + } + + virtual void OnAcquire() { } }; class ServiceAlias { Anope::string t, f; public: - ServiceAlias(const Anope::string &type, const Anope::string &from, const Anope::string &to) : t(type), f(from) - { - Service::AddAlias(type, from, to); - } - - ~ServiceAlias() - { - Service::DelAlias(t, f); - } + ServiceAlias(const Anope::string &type, const Anope::string &from, const Anope::string &to); + ~ServiceAlias(); }; -- cgit