diff options
Diffstat (limited to 'include/service.h')
-rw-r--r-- | include/service.h | 49 |
1 files changed, 30 insertions, 19 deletions
diff --git a/include/service.h b/include/service.h index ee5168b85..ad1d12f92 100644 --- a/include/service.h +++ b/include/service.h @@ -9,8 +9,7 @@ * Based on the original code of Services by Andy Church. */ -#ifndef SERVICE_H -#define SERVICE_H +#pragma once #include "services.h" #include "anope.h" @@ -21,7 +20,8 @@ * such as commands, use this. This is also used for modules * that publish a service (m_ssl_openssl, etc). */ -class CoreExport Service : public virtual Base +class CoreExport Service + : public virtual Base { static std::map<Anope::string, std::map<Anope::string, Service *> > Services; static std::map<Anope::string, std::map<Anope::string, Anope::string> > Aliases; @@ -42,7 +42,7 @@ class CoreExport Service : public virtual Base return NULL; } - public: +public: static Service *FindService(const Anope::string &t, const Anope::string &n) { std::map<Anope::string, std::map<Anope::string, Service *> >::const_iterator it = Services.find(t); @@ -59,13 +59,27 @@ class CoreExport Service : public virtual Base static std::vector<Anope::string> GetServiceKeys(const Anope::string &t) { std::vector<Anope::string> keys; - std::map<Anope::string, std::map<Anope::string, Service *> >::iterator it = Services.find(t); + const auto it = Services.find(t); if (it != Services.end()) - for (std::map<Anope::string, Service *>::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) - keys.push_back(it2->first); + { + for (const auto &[key, _] : it->second) + keys.push_back(key); + } return keys; } + static std::vector<Service *> GetServices(const Anope::string &t) + { + std::vector<Service *> values; + const auto it = Services.find(t); + if (it != Services.end()) + { + for (const auto &[_, value] : it->second) + values.push_back(value); + } + return values; + } + static void AddAlias(const Anope::string &t, const Anope::string &n, const Anope::string &v) { std::map<Anope::string, Anope::string> &smap = Aliases[t]; @@ -98,10 +112,8 @@ class CoreExport Service : public virtual Base void Register() { - std::map<Anope::string, Service *> &smap = Services[this->type]; - if (smap.find(this->name) != smap.end()) + if (!Services[this->type].emplace(this->name, this).second) throw ModuleException("Service " + this->type + " with name " + this->name + " already exists"); - smap[this->name] = this; } void Unregister() @@ -113,16 +125,17 @@ class CoreExport Service : public virtual Base } }; -/** Like Reference, but used to refer to Services. +/** Like Reference, but used to refer to a Service. */ template<typename T> -class ServiceReference : public Reference<T> +class ServiceReference + : public Reference<T> { Anope::string type; Anope::string name; - public: - ServiceReference() { } +public: + ServiceReference() = default; ServiceReference(const Anope::string &t, const Anope::string &n) : type(t), name(n) { @@ -138,7 +151,7 @@ class ServiceReference : public Reference<T> this->invalid = true; } - operator bool() anope_override + operator bool() override { if (this->invalid) { @@ -159,10 +172,10 @@ class ServiceReference : public Reference<T> } }; -class ServiceAlias +class ServiceAlias final { Anope::string t, f; - public: +public: ServiceAlias(const Anope::string &type, const Anope::string &from, const Anope::string &to) : t(type), f(from) { Service::AddAlias(type, from, to); @@ -173,5 +186,3 @@ class ServiceAlias Service::DelAlias(t, f); } }; - -#endif // SERVICE_H |