summaryrefslogtreecommitdiff
path: root/include/service.h
blob: 748be4dc5051c8d2f1e257ac6a0efc2e2533901e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 *
 * (C) 2003-2014 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 *
 * Based on the original code of Epona by Lara.
 * Based on the original code of Services by Andy Church.
 *
 */

#pragma once

#include "services.h"
#include "anope.h"
#include "base.h"

/** Anything that inherits from this class can be referred to
 * using ServiceReference. Any interfaces provided by modules,
 * 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
{
	static std::map<Anope::string, std::map<Anope::string, Service *> > *Services;
	static std::map<Anope::string, std::map<Anope::string, Anope::string> > *Aliases;

	static void check();

	static Service *FindService(const std::map<Anope::string, Service *> &services, const std::map<Anope::string, Anope::string> *aliases, const Anope::string &n);

 public:
 	static Service *FindService(const Anope::string &t, const Anope::string &n);

	static std::vector<Anope::string> GetServiceKeys(const Anope::string &t);

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

	Module *owner;
	/* Service type, which should be the class name (eg "Command") */
	Anope::string type;
	/* Service name, commands are usually named service/command */
	Anope::string name;

	Service(Module *o, const Anope::string &t, const Anope::string &n);

	Service(const Service &) = delete;

	virtual ~Service();

	void Register();

	void Unregister();
};

/** Like Reference, but used to refer to Services.
 */
template<typename T>
class ServiceReference : public Reference<T>
{
	Anope::string type;
	Anope::string name;
	bool need_check = true;

 public:
 	ServiceReference() { }

	ServiceReference(const Anope::string &t, const Anope::string &n) : type(t), name(n)
	{
	}

	const Anope::string &GetName() const
	{
		return name;
	}

	void operator=(const Anope::string &n)
	{
		this->name = n;
	}

	void Check() override
	{
		if (need_check)
		{
			need_check = false;

			this->ref = static_cast<T *>(Service::FindService(this->type, this->name));
			if (this->ref)
			{
				this->ref->AddReference(this);
				this->OnAcquire();
			}
		}
	}

	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);
	~ServiceAlias();
};