| 1 |
/* |
| 2 |
* ComponentManager.h |
| 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 |
#pragma once |
| 12 |
|
| 13 |
#include "openmpt/all/BuildSettings.hpp" |
| 14 |
|
| 15 |
#include "mpt/mutex/mutex.hpp" |
| 16 |
|
| 17 |
#include <map> |
| 18 |
#include <vector> |
| 19 |
#include "../common/misc_util.h" |
| 20 |
#if defined(MODPLUG_TRACKER) |
| 21 |
#include "../misc/mptLibrary.h" |
| 22 |
#endif |
| 23 |
|
| 24 |
|
| 25 |
OPENMPT_NAMESPACE_BEGIN |
| 26 |
|
| 27 |
|
| 28 |
enum ComponentType |
| 29 |
{ |
| 30 |
ComponentTypeUnknown = 0, |
| 31 |
ComponentTypeBuiltin, // PortAudio |
| 32 |
ComponentTypeSystem, // mf.dll |
| 33 |
ComponentTypeSystemInstallable, // acm mp3 codec |
| 34 |
ComponentTypeBundled, // libsoundtouch |
| 35 |
ComponentTypeForeign, // libmp3lame |
| 36 |
}; |
| 37 |
|
| 38 |
|
| 39 |
class ComponentFactoryBase; |
| 40 |
|
| 41 |
|
| 42 |
class IComponent |
| 43 |
{ |
| 44 |
|
| 45 |
friend class ComponentFactoryBase; |
| 46 |
|
| 47 |
protected: |
| 48 |
|
| 49 |
IComponent() = default; |
| 50 |
|
| 51 |
public: |
| 52 |
|
| 53 |
virtual ~IComponent() = default; |
| 54 |
|
| 55 |
public: |
| 56 |
|
| 57 |
virtual ComponentType GetType() const = 0; |
| 58 |
|
| 59 |
virtual bool IsInitialized() const = 0; // Initialize() has been called |
| 60 |
virtual bool IsAvailable() const = 0; // Initialize() has been successfull |
| 61 |
virtual mpt::ustring GetVersion() const = 0; |
| 62 |
|
| 63 |
virtual void Initialize() = 0; // try to load the component |
| 64 |
|
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
class ComponentBase |
| 69 |
: public IComponent |
| 70 |
{ |
| 71 |
|
| 72 |
private: |
| 73 |
|
| 74 |
ComponentType m_Type; |
| 75 |
|
| 76 |
bool m_Initialized; |
| 77 |
bool m_Available; |
| 78 |
|
| 79 |
protected: |
| 80 |
|
| 81 |
ComponentBase(ComponentType type); |
| 82 |
|
| 83 |
public: |
| 84 |
|
| 85 |
~ComponentBase() override; |
| 86 |
|
| 87 |
protected: |
| 88 |
|
| 89 |
void SetInitialized(); |
| 90 |
void SetAvailable(); |
| 91 |
|
| 92 |
public: |
| 93 |
|
| 94 |
ComponentType GetType() const override; |
| 95 |
bool IsInitialized() const override; |
| 96 |
bool IsAvailable() const override; |
| 97 |
|
| 98 |
mpt::ustring GetVersion() const override; |
| 99 |
|
| 100 |
public: |
| 101 |
|
| 102 |
void Initialize() override; |
| 103 |
|
| 104 |
protected: |
| 105 |
|
| 106 |
virtual bool DoInitialize() = 0; |
| 107 |
|
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
class ComponentBuiltin : public ComponentBase |
| 112 |
{ |
| 113 |
public: |
| 114 |
ComponentBuiltin() |
| 115 |
: ComponentBase(ComponentTypeBuiltin) |
| 116 |
{ |
| 117 |
return; |
| 118 |
} |
| 119 |
bool DoInitialize() override |
| 120 |
{ |
| 121 |
return true; |
| 122 |
} |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
#define MPT_GLOBAL_BIND(lib, name) name = &::name; |
| 127 |
|
| 128 |
|
| 129 |
#if defined(MODPLUG_TRACKER) |
| 130 |
|
| 131 |
|
| 132 |
class ComponentLibrary |
| 133 |
: public ComponentBase |
| 134 |
{ |
| 135 |
|
| 136 |
private: |
| 137 |
|
| 138 |
using TLibraryMap = std::map<std::string, mpt::Library>; |
| 139 |
TLibraryMap m_Libraries; |
| 140 |
|
| 141 |
bool m_BindFailed; |
| 142 |
|
| 143 |
protected: |
| 144 |
|
| 145 |
ComponentLibrary(ComponentType type); |
| 146 |
|
| 147 |
public: |
| 148 |
|
| 149 |
virtual ~ComponentLibrary(); |
| 150 |
|
| 151 |
protected: |
| 152 |
|
| 153 |
bool AddLibrary(const std::string &libName, const mpt::LibraryPath &libPath); |
| 154 |
void ClearLibraries(); |
| 155 |
void SetBindFailed(); |
| 156 |
void ClearBindFailed(); |
| 157 |
bool HasBindFailed() const; |
| 158 |
|
| 159 |
public: |
| 160 |
|
| 161 |
virtual mpt::Library GetLibrary(const std::string &libName) const; |
| 162 |
|
| 163 |
template <typename Tfunc> |
| 164 |
bool Bind(Tfunc * & f, const std::string &libName, const std::string &symbol) const |
| 165 |
{ |
| 166 |
return GetLibrary(libName).Bind(f, symbol); |
| 167 |
} |
| 168 |
|
| 169 |
protected: |
| 170 |
|
| 171 |
bool DoInitialize() override = 0; |
| 172 |
|
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
#define MPT_COMPONENT_BIND(libName, func) do { if(!Bind( func , libName , #func )) { SetBindFailed(); } } while(0) |
| 177 |
#define MPT_COMPONENT_BIND_OPTIONAL(libName, func) Bind( func , libName , #func ) |
| 178 |
#define MPT_COMPONENT_BIND_SYMBOL(libName, symbol, func) do { if(!Bind( func , libName , symbol )) { SetBindFailed(); } } while(0) |
| 179 |
#define MPT_COMPONENT_BIND_SYMBOL_OPTIONAL(libName, symbol, func) Bind( func , libName , symbol ) |
| 180 |
|
| 181 |
#if MPT_OS_WINDOWS |
| 182 |
#ifdef UNICODE |
| 183 |
#define MPT_COMPONENT_BINDWIN_SUFFIX "W" |
| 184 |
#else |
| 185 |
#define MPT_COMPONENT_BINDWIN_SUFFIX "A" |
| 186 |
#endif |
| 187 |
#define MPT_COMPONENT_BINDWIN(libName, func) do { if(!Bind( func , libName , #func MPT_COMPONENT_BINDWIN_SUFFIX )) { SetBindFailed(); } } while(0) |
| 188 |
#define MPT_COMPONENT_BINDWIN_OPTIONAL(libName, func) Bind( func , libName , #func MPT_COMPONENT_BINDWIN_SUFFIX ) |
| 189 |
#define MPT_COMPONENT_BINDWIN_SYMBOL(libName, symbol, func) do { if(!Bind( func , libName , symbol MPT_COMPONENT_BINDWIN_SUFFIX )) { SetBindFailed(); } } while(0) |
| 190 |
#define MPT_COMPONENT_BINDWIN_SYMBOL_OPTIONAL(libName, symbol, func) Bind( func , libName , symbol MPT_COMPONENT_BINDWIN_SUFFIX ) |
| 191 |
#endif |
| 192 |
|
| 193 |
|
| 194 |
class ComponentSystemDLL : public ComponentLibrary |
| 195 |
{ |
| 196 |
private: |
| 197 |
mpt::PathString m_BaseName; |
| 198 |
public: |
| 199 |
ComponentSystemDLL(const mpt::PathString &baseName) |
| 200 |
: ComponentLibrary(ComponentTypeSystem) |
| 201 |
, m_BaseName(baseName) |
| 202 |
{ |
| 203 |
return; |
| 204 |
} |
| 205 |
bool DoInitialize() override |
| 206 |
{ |
| 207 |
AddLibrary(m_BaseName.ToUTF8(), mpt::LibraryPath::System(m_BaseName)); |
| 208 |
return GetLibrary(m_BaseName.ToUTF8()).IsValid(); |
| 209 |
} |
| 210 |
}; |
| 211 |
|
| 212 |
|
| 213 |
class ComponentBundledDLL : public ComponentLibrary |
| 214 |
{ |
| 215 |
private: |
| 216 |
mpt::PathString m_FullName; |
| 217 |
public: |
| 218 |
ComponentBundledDLL(const mpt::PathString &fullName) |
| 219 |
: ComponentLibrary(ComponentTypeBundled) |
| 220 |
, m_FullName(fullName) |
| 221 |
{ |
| 222 |
return; |
| 223 |
} |
| 224 |
bool DoInitialize() override |
| 225 |
{ |
| 226 |
AddLibrary(m_FullName.ToUTF8(), mpt::LibraryPath::AppFullName(m_FullName)); |
| 227 |
return GetLibrary(m_FullName.ToUTF8()).IsValid(); |
| 228 |
} |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
#endif // MODPLUG_TRACKER |
| 233 |
|
| 234 |
|
| 235 |
#if MPT_COMPONENT_MANAGER |
| 236 |
|
| 237 |
|
| 238 |
class ComponentManager; |
| 239 |
|
| 240 |
using ComponentFactoryMethod = std::shared_ptr<IComponent> (*)(ComponentManager &componentManager); |
| 241 |
|
| 242 |
|
| 243 |
class IComponentFactory |
| 244 |
{ |
| 245 |
protected: |
| 246 |
IComponentFactory() = default; |
| 247 |
public: |
| 248 |
virtual ~IComponentFactory() = default; |
| 249 |
public: |
| 250 |
virtual std::string GetID() const = 0; |
| 251 |
virtual std::string GetSettingsKey() const = 0; |
| 252 |
virtual std::shared_ptr<IComponent> Construct(ComponentManager &componentManager) const = 0; |
| 253 |
virtual ComponentFactoryMethod GetStaticConstructor() const = 0; |
| 254 |
}; |
| 255 |
|
| 256 |
|
| 257 |
class ComponentFactoryBase |
| 258 |
: public IComponentFactory |
| 259 |
{ |
| 260 |
private: |
| 261 |
std::string m_ID; |
| 262 |
std::string m_SettingsKey; |
| 263 |
protected: |
| 264 |
ComponentFactoryBase(const std::string &id, const std::string &settingsKey); |
| 265 |
void PreConstruct() const; |
| 266 |
void Initialize(ComponentManager &componentManager, std::shared_ptr<IComponent> component) const; |
| 267 |
public: |
| 268 |
virtual ~ComponentFactoryBase(); |
| 269 |
std::string GetID() const override; |
| 270 |
std::string GetSettingsKey() const override; |
| 271 |
std::shared_ptr<IComponent> Construct(ComponentManager &componentManager) const override = 0; |
| 272 |
ComponentFactoryMethod GetStaticConstructor() const override = 0; |
| 273 |
}; |
| 274 |
|
| 275 |
|
| 276 |
template <typename T> |
| 277 |
class ComponentFactory |
| 278 |
: public ComponentFactoryBase |
| 279 |
{ |
| 280 |
public: |
| 281 |
ComponentFactory() |
| 282 |
: ComponentFactoryBase(T::g_ID, T::g_SettingsKey) |
| 283 |
{ |
| 284 |
return; |
| 285 |
} |
| 286 |
public: |
| 287 |
std::shared_ptr<IComponent> Construct(ComponentManager &componentManager) const override |
| 288 |
{ |
| 289 |
PreConstruct(); |
| 290 |
std::shared_ptr<IComponent> component = std::make_shared<T>(); |
| 291 |
Initialize(componentManager, component); |
| 292 |
return component; |
| 293 |
} |
| 294 |
static std::shared_ptr<IComponent> StaticConstruct(ComponentManager &componentManager) |
| 295 |
{ |
| 296 |
return ComponentFactory().Construct(componentManager); |
| 297 |
} |
| 298 |
virtual ComponentFactoryMethod GetStaticConstructor() const override |
| 299 |
{ |
| 300 |
return &StaticConstruct; |
| 301 |
} |
| 302 |
}; |
| 303 |
|
| 304 |
|
| 305 |
class IComponentManagerSettings |
| 306 |
{ |
| 307 |
public: |
| 308 |
virtual bool LoadOnStartup() const = 0; |
| 309 |
virtual bool KeepLoaded() const = 0; |
| 310 |
virtual bool IsBlocked(const std::string &key) const = 0; |
| 311 |
protected: |
| 312 |
virtual ~IComponentManagerSettings() = default; |
| 313 |
}; |
| 314 |
|
| 315 |
|
| 316 |
class ComponentManagerSettingsDefault |
| 317 |
: public IComponentManagerSettings |
| 318 |
{ |
| 319 |
public: |
| 320 |
bool LoadOnStartup() const override { return false; } |
| 321 |
bool KeepLoaded() const override { return true; } |
| 322 |
bool IsBlocked(const std::string & /*key*/ ) const override { return false; } |
| 323 |
}; |
| 324 |
|
| 325 |
|
| 326 |
enum ComponentState |
| 327 |
{ |
| 328 |
ComponentStateUnregistered, |
| 329 |
ComponentStateBlocked, |
| 330 |
ComponentStateUnintialized, |
| 331 |
ComponentStateUnavailable, |
| 332 |
ComponentStateAvailable, |
| 333 |
}; |
| 334 |
|
| 335 |
|
| 336 |
struct ComponentInfo |
| 337 |
{ |
| 338 |
std::string name; |
| 339 |
ComponentState state; |
| 340 |
std::string settingsKey; |
| 341 |
ComponentType type; |
| 342 |
}; |
| 343 |
|
| 344 |
|
| 345 |
class ComponentManager |
| 346 |
{ |
| 347 |
friend class ComponentFactoryBase; |
| 348 |
public: |
| 349 |
static void Init(const IComponentManagerSettings &settings); |
| 350 |
static void Release(); |
| 351 |
static std::shared_ptr<ComponentManager> Instance(); |
| 352 |
private: |
| 353 |
ComponentManager(const IComponentManagerSettings &settings); |
| 354 |
private: |
| 355 |
struct RegisteredComponent |
| 356 |
{ |
| 357 |
std::string settingsKey; |
| 358 |
ComponentFactoryMethod factoryMethod; |
| 359 |
std::shared_ptr<IComponent> instance; |
| 360 |
std::weak_ptr<IComponent> weakInstance; |
| 361 |
}; |
| 362 |
using TComponentMap = std::map<std::string, RegisteredComponent>; |
| 363 |
const IComponentManagerSettings &m_Settings; |
| 364 |
TComponentMap m_Components; |
| 365 |
private: |
| 366 |
bool IsComponentBlocked(const std::string &settingsKey) const; |
| 367 |
void InitializeComponent(std::shared_ptr<IComponent> component) const; |
| 368 |
public: |
| 369 |
void Register(const IComponentFactory &componentFactory); |
| 370 |
void Startup(); |
| 371 |
std::shared_ptr<const IComponent> GetComponent(const IComponentFactory &componentFactory); |
| 372 |
std::shared_ptr<const IComponent> ReloadComponent(const IComponentFactory &componentFactory); |
| 373 |
std::vector<std::string> GetRegisteredComponents() const; |
| 374 |
ComponentInfo GetComponentInfo(std::string name) const; |
| 375 |
}; |
| 376 |
|
| 377 |
|
| 378 |
struct ComponentListEntry |
| 379 |
{ |
| 380 |
ComponentListEntry *next; |
| 381 |
void (*reg)(ComponentManager &componentManager); |
| 382 |
}; |
| 383 |
|
| 384 |
bool ComponentListPush(ComponentListEntry *entry); |
| 385 |
|
| 386 |
template <typename TComponent> |
| 387 |
struct ComponentRegisterer |
| 388 |
{ |
| 389 |
static inline void RegisterComponent(ComponentManager &componentManager) |
| 390 |
{ |
| 391 |
componentManager.Register(ComponentFactory<TComponent>()); |
| 392 |
} |
| 393 |
static inline ComponentListEntry &GetComponentListEntry() |
| 394 |
{ |
| 395 |
static ComponentListEntry s_ComponentListEntry = {nullptr, &RegisterComponent}; |
| 396 |
return s_ComponentListEntry; |
| 397 |
} |
| 398 |
static inline bool g_ComponentRegistered = ComponentListPush(&GetComponentListEntry()); |
| 399 |
}; |
| 400 |
|
| 401 |
#define MPT_DECLARE_COMPONENT_MEMBERS(name, settingsKey) \ |
| 402 |
public: \ |
| 403 |
static constexpr const char *g_ID = #name ; \ |
| 404 |
static constexpr const char *g_SettingsKey = settingsKey ; \ |
| 405 |
static inline ComponentRegisterer< name > s_ComponentRegisterer; \ |
| 406 |
/**/ |
| 407 |
|
| 408 |
|
| 409 |
template <typename type> |
| 410 |
std::shared_ptr<const type> GetComponent() |
| 411 |
{ |
| 412 |
return std::dynamic_pointer_cast<const type>(ComponentManager::Instance()->GetComponent(ComponentFactory<type>())); |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
template <typename type> |
| 417 |
std::shared_ptr<const type> ReloadComponent() |
| 418 |
{ |
| 419 |
return std::dynamic_pointer_cast<const type>(ComponentManager::Instance()->ReloadComponent(ComponentFactory<type>())); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
#else // !MPT_COMPONENT_MANAGER |
| 424 |
|
| 425 |
|
| 426 |
#define MPT_DECLARE_COMPONENT_MEMBERS(name, settingsKey) |
| 427 |
|
| 428 |
|
| 429 |
template <typename type> |
| 430 |
std::shared_ptr<const type> GetComponent() |
| 431 |
{ |
| 432 |
#if MPT_COMPILER_CLANG |
| 433 |
#pragma clang diagnostic push |
| 434 |
#pragma clang diagnostic ignored "-Wexit-time-destructors" |
| 435 |
#endif // MPT_COMPILER_CLANG |
| 436 |
static std::weak_ptr<type> cache; |
| 437 |
static mpt::mutex m; |
| 438 |
#if MPT_COMPILER_CLANG |
| 439 |
#pragma clang diagnostic pop |
| 440 |
#endif // MPT_COMPILER_CLANG mpt::lock_guard<mpt::mutex> l(m); |
| 441 |
mpt::lock_guard<mpt::mutex> l(m); |
| 442 |
std::shared_ptr<type> component = cache.lock(); |
| 443 |
if(!component) |
| 444 |
{ |
| 445 |
component = std::make_shared<type>(); |
| 446 |
component->Initialize(); |
| 447 |
cache = component; |
| 448 |
} |
| 449 |
return component; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
#endif // MPT_COMPONENT_MANAGER |
| 454 |
|
| 455 |
|
| 456 |
// Simple wrapper around std::shared_ptr<ComponentType> which automatically |
| 457 |
// gets a reference to the component (or constructs it) on initialization. |
| 458 |
template <typename T> |
| 459 |
class ComponentHandle |
| 460 |
{ |
| 461 |
private: |
| 462 |
std::shared_ptr<const T> component; |
| 463 |
public: |
| 464 |
ComponentHandle() |
| 465 |
: component(GetComponent<T>()) |
| 466 |
{ |
| 467 |
return; |
| 468 |
} |
| 469 |
~ComponentHandle() |
| 470 |
{ |
| 471 |
return; |
| 472 |
} |
| 473 |
bool IsAvailable() const |
| 474 |
{ |
| 475 |
return component && component->IsAvailable(); |
| 476 |
} |
| 477 |
const T *get() const |
| 478 |
{ |
| 479 |
return component.get(); |
| 480 |
} |
| 481 |
const T &operator*() const |
| 482 |
{ |
| 483 |
return *component; |
| 484 |
} |
| 485 |
const T *operator->() const |
| 486 |
{ |
| 487 |
return &*component; |
| 488 |
} |
| 489 |
#if MPT_COMPONENT_MANAGER |
| 490 |
void Reload() |
| 491 |
{ |
| 492 |
component = nullptr; |
| 493 |
component = ReloadComponent<T>(); |
| 494 |
} |
| 495 |
#endif |
| 496 |
}; |
| 497 |
|
| 498 |
|
| 499 |
template <typename T> |
| 500 |
bool IsComponentAvailable(const ComponentHandle<T> &handle) |
| 501 |
{ |
| 502 |
return handle.IsAvailable(); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
OPENMPT_NAMESPACE_END |