Debian Codegraph @

common/ComponentManager.cpp

raw
1 /*
2 * ComponentManager.cpp
3 * --------------------
4 * Purpose: Manages loading of optional components.
5 * Notes : (currently none)
6 * Authors: Joern Heusipp
7 * OpenMPT Devs
8 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
9 */
10
11 #include "stdafx.h"
12 #include "ComponentManager.h"
13
14 #include "mpt/mutex/mutex.hpp"
15
16 #include "Logging.h"
17
18 OPENMPT_NAMESPACE_BEGIN
19
20
21 ComponentBase::ComponentBase(ComponentType type)
22 : m_Type(type)
23 , m_Initialized(false)
24 , m_Available(false)
25 {
26 return;
27 }
28
29
30 ComponentBase::~ComponentBase()
31 {
32 return;
33 }
34
35
36 void ComponentBase::SetInitialized()
37 {
38 m_Initialized = true;
39 }
40
41
42 void ComponentBase::SetAvailable()
43 {
44 m_Available = true;
45 }
46
47
48 ComponentType ComponentBase::GetType() const
49 {
50 return m_Type;
51 }
52
53
54 bool ComponentBase::IsInitialized() const
55 {
56 return m_Initialized;
57 }
58
59
60 bool ComponentBase::IsAvailable() const
61 {
62 return m_Initialized && m_Available;
63 }
64
65
66 mpt::ustring ComponentBase::GetVersion() const
67 {
68 return mpt::ustring();
69 }
70
71
72 void ComponentBase::Initialize()
73 {
74 if(IsInitialized())
75 {
76 return;
77 }
78 if(DoInitialize())
79 {
80 SetAvailable();
81 }
82 SetInitialized();
83 }
84
85
86 #if defined(MODPLUG_TRACKER)
87
88
89 ComponentLibrary::ComponentLibrary(ComponentType type)
90 : ComponentBase(type)
91 , m_BindFailed(false)
92 {
93 return;
94 }
95
96
97 ComponentLibrary::~ComponentLibrary()
98 {
99 return;
100 }
101
102
103 bool ComponentLibrary::AddLibrary(const std::string &libName, const mpt::LibraryPath &libPath)
104 {
105 if(m_Libraries[libName].IsValid())
106 {
107 // prefer previous
108 return true;
109 }
110 mpt::Library lib(libPath);
111 if(!lib.IsValid())
112 {
113 return false;
114 }
115 m_Libraries[libName] = lib;
116 return true;
117 }
118
119
120 void ComponentLibrary::ClearLibraries()
121 {
122 m_Libraries.clear();
123 }
124
125
126 void ComponentLibrary::SetBindFailed()
127 {
128 m_BindFailed = true;
129 }
130
131
132 void ComponentLibrary::ClearBindFailed()
133 {
134 m_BindFailed = false;
135 }
136
137
138 bool ComponentLibrary::HasBindFailed() const
139 {
140 return m_BindFailed;
141 }
142
143
144 mpt::Library ComponentLibrary::GetLibrary(const std::string &libName) const
145 {
146 const auto it = m_Libraries.find(libName);
147 if(it == m_Libraries.end())
148 {
149 return mpt::Library();
150 }
151 return it->second;
152 }
153
154
155 #endif // MODPLUG_TRACKER
156
157
158 #if MPT_COMPONENT_MANAGER
159
160
161 ComponentFactoryBase::ComponentFactoryBase(const std::string &id, const std::string &settingsKey)
162 : m_ID(id)
163 , m_SettingsKey(settingsKey)
164 {
165 return;
166 }
167
168
169 ComponentFactoryBase::~ComponentFactoryBase()
170 {
171 return;
172 }
173
174
175 std::string ComponentFactoryBase::GetID() const
176 {
177 return m_ID;
178 }
179
180
181 std::string ComponentFactoryBase::GetSettingsKey() const
182 {
183 return m_SettingsKey;
184 }
185
186
187 void ComponentFactoryBase::PreConstruct() const
188 {
189 MPT_LOG_GLOBAL(LogInformation, "Components",
190 MPT_UFORMAT("Constructing Component {}")
191 ( mpt::ToUnicode(mpt::Charset::ASCII, m_ID)
192 )
193 );
194 }
195
196
197 void ComponentFactoryBase::Initialize(ComponentManager &componentManager, std::shared_ptr<IComponent> component) const
198 {
199 if(componentManager.IsComponentBlocked(GetSettingsKey()))
200 {
201 return;
202 }
203 componentManager.InitializeComponent(component);
204 }
205
206
207 // Global list of component register functions.
208 // We do not use a global scope static list head because the corresponding
209 // mutex would be no POD type and would thus not be safe to be usable in
210 // zero-initialized state.
211 // Function scope static initialization is guaranteed to be thread safe
212 // in C++11.
213 // We use this implementation to be future-proof.
214 // MSVC currently does not exploit the possibility of using multiple threads
215 // for global lifetime object's initialization.
216 // An implementation with a simple global list head and no mutex at all would
217 // thus work fine for MSVC (currently).
218
219 static mpt::mutex & ComponentListMutex()
220 {
221 static mpt::mutex g_ComponentListMutex;
222 return g_ComponentListMutex;
223 }
224
225 static ComponentListEntry * & ComponentListHead()
226 {
227 static ComponentListEntry g_ComponentListHeadEmpty = {nullptr, nullptr};
228 static ComponentListEntry *g_ComponentListHead = &g_ComponentListHeadEmpty;
229 return g_ComponentListHead;
230 }
231
232 bool ComponentListPush(ComponentListEntry *entry)
233 {
234 mpt::lock_guard<mpt::mutex> guard(ComponentListMutex());
235 #if MPT_MSVC_BEFORE(2019,0)
236 // Guard against VS2017 compiler bug causing repeated initialization of inline variables.
237 // See <https://developercommunity.visualstudio.com/t/static-inline-variable-gets-destroyed-multiple-tim/297876>.
238 if(entry->next)
239 {
240 return false;
241 }
242 #endif
243 entry->next = ComponentListHead();
244 ComponentListHead() = entry;
245 return true;
246 }
247
248
249 static std::shared_ptr<ComponentManager> g_ComponentManager;
250
251
252 void ComponentManager::Init(const IComponentManagerSettings &settings)
253 {
254 MPT_LOG_GLOBAL(LogInformation, "Components", U_("Init"));
255 // cannot use make_shared because the constructor is private
256 g_ComponentManager = std::shared_ptr<ComponentManager>(new ComponentManager(settings));
257 }
258
259
260 void ComponentManager::Release()
261 {
262 MPT_LOG_GLOBAL(LogInformation, "Components", U_("Release"));
263 g_ComponentManager = nullptr;
264 }
265
266
267 std::shared_ptr<ComponentManager> ComponentManager::Instance()
268 {
269 return g_ComponentManager;
270 }
271
272
273 ComponentManager::ComponentManager(const IComponentManagerSettings &settings)
274 : m_Settings(settings)
275 {
276 mpt::lock_guard<mpt::mutex> guard(ComponentListMutex());
277 for(ComponentListEntry *entry = ComponentListHead(); entry; entry = entry->next)
278 {
279 if(entry->reg)
280 {
281 entry->reg(*this);
282 }
283 }
284 }
285
286
287 void ComponentManager::Register(const IComponentFactory &componentFactory)
288 {
289 if(m_Components.find(componentFactory.GetID()) != m_Components.end())
290 {
291 return;
292 }
293 RegisteredComponent registeredComponent;
294 registeredComponent.settingsKey = componentFactory.GetSettingsKey();
295 registeredComponent.factoryMethod = componentFactory.GetStaticConstructor();
296 registeredComponent.instance = nullptr;
297 registeredComponent.weakInstance = std::weak_ptr<IComponent>();
298 m_Components.insert(std::make_pair(componentFactory.GetID(), registeredComponent));
299 }
300
301
302 void ComponentManager::Startup()
303 {
304 MPT_LOG_GLOBAL(LogDebug, "Components", U_("Startup"));
305 if(m_Settings.LoadOnStartup())
306 {
307 for(auto &it : m_Components)
308 {
309 it.second.instance = it.second.factoryMethod(*this);
310 it.second.weakInstance = it.second.instance;
311 }
312 }
313 if(!m_Settings.KeepLoaded())
314 {
315 for(auto &it : m_Components)
316 {
317 it.second.instance = nullptr;
318 }
319 }
320 }
321
322
323 bool ComponentManager::IsComponentBlocked(const std::string &settingsKey) const
324 {
325 if(settingsKey.empty())
326 {
327 return false;
328 }
329 return m_Settings.IsBlocked(settingsKey);
330 }
331
332
333 void ComponentManager::InitializeComponent(std::shared_ptr<IComponent> component) const
334 {
335 if(!component)
336 {
337 return;
338 }
339 if(component->IsInitialized())
340 {
341 return;
342 }
343 component->Initialize();
344 }
345
346
347 std::shared_ptr<const IComponent> ComponentManager::GetComponent(const IComponentFactory &componentFactory)
348 {
349 std::shared_ptr<IComponent> component = nullptr;
350 auto it = m_Components.find(componentFactory.GetID());
351 if(it != m_Components.end())
352 { // registered component
353 if((*it).second.instance)
354 { // loaded
355 component = (*it).second.instance;
356 } else
357 { // not loaded
358 component = (*it).second.weakInstance.lock();
359 if(!component)
360 {
361 component = (*it).second.factoryMethod(*this);
362 }
363 if(m_Settings.KeepLoaded())
364 { // keep the component loaded
365 (*it).second.instance = component;
366 }
367 (*it).second.weakInstance = component;
368 }
369 } else
370 { // unregistered component
371 component = componentFactory.Construct(*this);
372 }
373 MPT_ASSERT(component);
374 return component;
375 }
376
377
378 std::shared_ptr<const IComponent> ComponentManager::ReloadComponent(const IComponentFactory &componentFactory)
379 {
380 std::shared_ptr<IComponent> component = nullptr;
381 auto it = m_Components.find(componentFactory.GetID());
382 if(it != m_Components.end())
383 { // registered component
384 if((*it).second.instance)
385 { // loaded
386 (*it).second.instance = nullptr;
387 if(!(*it).second.weakInstance.expired())
388 {
389 throw std::runtime_error("Component not completely unloaded. Cannot reload.");
390 }
391 (*it).second.weakInstance = std::weak_ptr<IComponent>();
392 }
393 // not loaded
394 component = (*it).second.factoryMethod(*this);
395 if(m_Settings.KeepLoaded())
396 { // keep the component loaded
397 (*it).second.instance = component;
398 }
399 (*it).second.weakInstance = component;
400 } else
401 { // unregistered component
402 component = componentFactory.Construct(*this);
403 }
404 MPT_ASSERT(component);
405 return component;
406 }
407
408
409 std::vector<std::string> ComponentManager::GetRegisteredComponents() const
410 {
411 std::vector<std::string> result;
412 result.reserve(m_Components.size());
413 for(const auto &it : m_Components)
414 {
415 result.push_back(it.first);
416 }
417 return result;
418 }
419
420
421 ComponentInfo ComponentManager::GetComponentInfo(std::string name) const
422 {
423 ComponentInfo result;
424 result.name = name;
425 result.state = ComponentStateUnregistered;
426 result.settingsKey = "";
427 result.type = ComponentTypeUnknown;
428 const auto it = m_Components.find(name);
429 if(it == m_Components.end())
430 {
431 result.state = ComponentStateUnregistered;
432 return result;
433 }
434 result.settingsKey = it->second.settingsKey;
435 if(IsComponentBlocked(it->second.settingsKey))
436 {
437 result.state = ComponentStateBlocked;
438 return result;
439 }
440 std::shared_ptr<IComponent> component = it->second.instance;
441 if(!component)
442 {
443 component = it->second.weakInstance.lock();
444 }
445 if(!component)
446 {
447 result.state = ComponentStateUnintialized;
448 return result;
449 }
450 result.type = component->GetType();
451 if(!component->IsInitialized())
452 {
453 result.state = ComponentStateUnintialized;
454 return result;
455 }
456 if(!component->IsAvailable())
457 {
458 result.state = ComponentStateUnavailable;
459 return result;
460 }
461 result.state = ComponentStateAvailable;
462 return result;
463 }
464
465
466 #endif // MPT_COMPONENT_MANAGER
467
468
469 OPENMPT_NAMESPACE_END