summaryrefslogtreecommitdiff
path: root/include/extensible.h
blob: 4faff818b269ef04acef78618ecc3b5318a06354 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 *
 * (C) 2003-2014 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 *
 */

#pragma once

#include "anope.h"
#include "service.h"
#include "logger.h"

class Extensible;

class CoreExport ExtensibleBase : public Service
{
 protected:
	std::map<Extensible *, void *> items;

	ExtensibleBase(Module *m, const Anope::string &n);
	ExtensibleBase(Module *m, const Anope::string &t, const Anope::string &n);
	~ExtensibleBase();

 public:
	virtual void Unset(Extensible *obj) anope_abstract;
};

class CoreExport Extensible
{
 public:
	std::vector<ExtensibleBase *> extension_items;

	virtual ~Extensible();

	template<typename T> T* GetExt(const Anope::string &name);
	bool HasExtOK(const Anope::string &name);

	template<typename T> T* Extend(const Anope::string &name, const T &what);

	template<typename T> void ShrinkOK(const Anope::string &name);
};

template<typename T>
class ExtensibleItem : public ExtensibleBase
{
 public:
	ExtensibleItem(Module *m, const Anope::string &n) : ExtensibleBase(m, n) { }
	ExtensibleItem(Module *m, const Anope::string &t, const Anope::string &n) : ExtensibleBase(m, t, n) { }

	~ExtensibleItem()
	{
		while (!items.empty())
		{
			std::map<Extensible *, void *>::iterator it = items.begin();
			Extensible *obj = it->first;
			T *value = static_cast<T *>(it->second);

			auto it2 = std::find(obj->extension_items.begin(), obj->extension_items.end(), this);
			if (it2 != obj->extension_items.end())
				obj->extension_items.erase(it2);
			items.erase(it);

			delete value;
		}
	}

	T* Set(Extensible *obj, const T &value)
	{
		T* t = new T(value);
		Unset(obj);

		items[obj] = t;
		obj->extension_items.push_back(this);

		return t;
	}

	void Unset(Extensible *obj) override
	{
		T *value = Get(obj);

		items.erase(obj);
		auto it = std::find(obj->extension_items.begin(), obj->extension_items.end(), this);
		if (it != obj->extension_items.end())
			obj->extension_items.erase(it);

		delete value;
	}

	T* Get(Extensible *obj)
	{
		std::map<Extensible *, void *>::const_iterator it = items.find(obj);
		if (it != items.end())
			return static_cast<T *>(it->second);
		return nullptr;
	}

	bool HasExt(Extensible *obj)
	{
		return items.find(obj) != items.end();
	}

	T* Require(Extensible *obj)
	{
		T* t = Get(obj);
		if (t)
			return t;

		return Set(obj, T());
	}
};

template<typename T>
struct ExtensibleRef : ServiceReference<ExtensibleItem<T>>
{
	ExtensibleRef(const Anope::string &n) : ServiceReference<ExtensibleItem<T>>("Extensible", n) { }
	ExtensibleRef(const Anope::string &t, const Anope::string &n) : ServiceReference<ExtensibleItem<T>>(t, n) { }
};

template<typename T>
T* Extensible::GetExt(const Anope::string &name)
{
	ExtensibleRef<T> ref(name);
	if (ref)
		return ref->Get(this);

	Log(LOG_DEBUG) << "GetExt for nonexistent type " << name << " on " << static_cast<const void *>(this);
	return NULL;
}

template<typename T>
T* Extensible::Extend(const Anope::string &name, const T &what)
{
	ExtensibleRef<T> ref(name);
	if (ref)
	{
		ref->Set(this, what);
		return ref->Get(this);
	}

	Log(LOG_DEBUG) << "Extend for nonexistent type " << name << " on " << static_cast<void *>(this);
	return NULL;
}

template<typename T>
//XXX
void Extensible::ShrinkOK(const Anope::string &name)
{
	ExtensibleRef<T> ref(name);
	if (ref)
		ref->Unset(this);
	else
		Log(LOG_DEBUG) << "Shrink for nonexistent type " << name << " on " << static_cast<void *>(this);
}