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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2019 Sadie Powell <sadie@witchery.services>
*
* This file is part of InspIRCd. InspIRCd is free software: you can
* redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "inspircd.h"
bool ExtensionManager::Register(ExtensionItem* item)
{
return types.emplace(item->name, item).second;
}
void ExtensionManager::BeginUnregister(Module* module, std::vector<reference<ExtensionItem>>& items)
{
for (ExtMap::iterator type = types.begin(); type != types.end(); )
{
ExtMap::iterator thistype = type++;
ExtensionItem* item = thistype->second;
if (item->creator == module)
{
items.push_back(item);
types.erase(thistype);
}
}
}
ExtensionItem* ExtensionManager::GetItem(const std::string& name)
{
ExtMap::iterator iter = types.find(name);
if (iter == types.end())
return nullptr;
return iter->second;
}
Extensible::Extensible()
: culled(false)
{
}
Extensible::~Extensible()
{
if ((!extensions.empty() || !culled) && ServerInstance)
ServerInstance->Logs.Log("CULLLIST", LOG_DEBUG, "Extensible destructor called without cull @%p", (void*)this);
}
Cullable::Result Extensible::Cull()
{
FreeAllExtItems();
culled = true;
return Cullable::Cull();
}
void Extensible::FreeAllExtItems()
{
for (const auto& [extension, item] : extensions)
extension->Delete(this, item);
extensions.clear();
}
void Extensible::UnhookExtensions(const std::vector<reference<ExtensionItem>>& items)
{
for (const auto& item : items)
{
ExtensibleStore::iterator iter = extensions.find(item);
if (iter != extensions.end())
{
item->Delete(this, iter->second);
extensions.erase(iter);
}
}
}
ExtensionItem::ExtensionItem(Module* mod, const std::string& Key, ExtensibleType exttype)
: ServiceProvider(mod, Key, SERVICE_METADATA)
, type(exttype)
{
}
void ExtensionItem::RegisterService()
{
if (!ServerInstance->Extensions.Register(this))
throw ModuleException("Extension already exists: " + name);
}
void* ExtensionItem::GetRaw(const Extensible* container) const
{
auto iter = container->extensions.find(const_cast<ExtensionItem*>(this));
if (iter == container->extensions.end())
return nullptr;
return iter->second;
}
void* ExtensionItem::SetRaw(Extensible* container, void* value)
{
auto result = container->extensions.insert(std::make_pair(this, value));
if (result.second)
return nullptr;
void* old = result.first->second;
result.first->second = value;
return old;
}
void* ExtensionItem::UnsetRaw(Extensible* container)
{
auto iter = container->extensions.find(this);
if (iter == container->extensions.end())
return nullptr;
void* result = iter->second;
container->extensions.erase(iter);
return result;
}
void ExtensionItem::FromInternal(Extensible* container, const std::string& value) noexcept
{
FromNetwork(container, value);
}
void ExtensionItem::FromNetwork(Extensible* container, const std::string& value) noexcept
{
}
std::string ExtensionItem::ToHuman(const Extensible* container, void* item) const noexcept
{
// Try to use the network form by default.
std::string ret = ToNetwork(container, item);
// If there's no network form then fall back to the internal form.
if (ret.empty())
ret = ToInternal(container, item);
return ret;
}
std::string ExtensionItem::ToInternal(const Extensible* container, void* item) const noexcept
{
return ToNetwork(container, item);
}
std::string ExtensionItem::ToNetwork(const Extensible* container, void* item) const noexcept
{
return std::string();
}
IntExtItem::IntExtItem(Module* owner, const std::string& key, ExtensibleType exttype, bool sync)
: ExtensionItem(owner, key, exttype)
, synced(sync)
{
}
void IntExtItem::Delete(Extensible* container, void* item)
{
// Intentionally left blank.
}
void IntExtItem::FromInternal(Extensible* container, const std::string& value) noexcept
{
Set(container, ConvToNum<intptr_t>(value));
}
void IntExtItem::FromNetwork(Extensible* container, const std::string& value) noexcept
{
if (synced)
FromInternal(container, value);
}
intptr_t IntExtItem::Get(const Extensible* container) const
{
return reinterpret_cast<intptr_t>(GetRaw(container));
}
void IntExtItem::Set(Extensible* container, intptr_t value)
{
if (value)
SetRaw(container, reinterpret_cast<void*>(value));
else
UnsetRaw(container);
}
std::string IntExtItem::ToInternal(const Extensible* container, void* item) const noexcept
{
return ConvToStr(reinterpret_cast<intptr_t>(item));
}
std::string IntExtItem::ToNetwork(const Extensible* container, void* item) const noexcept
{
return synced ? ToInternal(container, item) : std::string();
}
void IntExtItem::Unset(Extensible* container)
{
UnsetRaw(container);
}
StringExtItem::StringExtItem(Module* owner, const std::string& key, ExtensibleType exttype, bool sync)
: SimpleExtItem(owner, key, exttype)
, synced(sync)
{
}
void StringExtItem::FromInternal(Extensible* container, const std::string& value) noexcept
{
if (value.empty())
Unset(container);
else
Set(container, value);
}
void StringExtItem::FromNetwork(Extensible* container, const std::string& value) noexcept
{
if (synced)
FromInternal(container, value);
}
std::string StringExtItem::ToInternal(const Extensible* container, void* item) const noexcept
{
return item ? *static_cast<std::string*>(item) : std::string();
}
std::string StringExtItem::ToNetwork(const Extensible* container, void* item) const noexcept
{
return synced ? ToInternal(container, item) : std::string();
}
|