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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
|
/*
* ComponentManager.h
* ------------------
* Purpose: Manages loading of optional components.
* Notes : (currently none)
* Authors: Joern Heusipp
* OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#pragma once
#include "openmpt/all/BuildSettings.hpp"
#include "mpt/mutex/mutex.hpp"
#include <map>
#include <vector>
#include "../common/misc_util.h"
#if defined(MODPLUG_TRACKER)
#include "../misc/mptLibrary.h"
#endif
OPENMPT_NAMESPACE_BEGIN
enum ComponentType
{
ComponentTypeUnknown = 0,
ComponentTypeBuiltin, // PortAudio
ComponentTypeSystem, // mf.dll
ComponentTypeSystemInstallable, // acm mp3 codec
ComponentTypeBundled, // libsoundtouch
ComponentTypeForeign, // libmp3lame
};
class ComponentFactoryBase;
class IComponent
{
friend class ComponentFactoryBase;
protected:
IComponent() = default;
public:
virtual ~IComponent() = default;
public:
virtual ComponentType GetType() const = 0;
virtual bool IsInitialized() const = 0; // Initialize() has been called
virtual bool IsAvailable() const = 0; // Initialize() has been successfull
virtual mpt::ustring GetVersion() const = 0;
virtual void Initialize() = 0; // try to load the component
};
class ComponentBase
: public IComponent
{
private:
ComponentType m_Type;
bool m_Initialized;
bool m_Available;
protected:
ComponentBase(ComponentType type);
public:
~ComponentBase() override;
protected:
void SetInitialized();
void SetAvailable();
public:
ComponentType GetType() const override;
bool IsInitialized() const override;
bool IsAvailable() const override;
mpt::ustring GetVersion() const override;
public:
void Initialize() override;
protected:
virtual bool DoInitialize() = 0;
};
class ComponentBuiltin : public ComponentBase
{
public:
ComponentBuiltin()
: ComponentBase(ComponentTypeBuiltin)
{
return;
}
bool DoInitialize() override
{
return true;
}
};
#define MPT_GLOBAL_BIND(lib, name) name = &::name;
#if defined(MODPLUG_TRACKER)
class ComponentLibrary
: public ComponentBase
{
private:
using TLibraryMap = std::map<std::string, mpt::Library>;
TLibraryMap m_Libraries;
bool m_BindFailed;
protected:
ComponentLibrary(ComponentType type);
public:
virtual ~ComponentLibrary();
protected:
bool AddLibrary(const std::string &libName, const mpt::LibraryPath &libPath);
void ClearLibraries();
void SetBindFailed();
void ClearBindFailed();
bool HasBindFailed() const;
public:
virtual mpt::Library GetLibrary(const std::string &libName) const;
template <typename Tfunc>
bool Bind(Tfunc * & f, const std::string &libName, const std::string &symbol) const
{
return GetLibrary(libName).Bind(f, symbol);
}
protected:
bool DoInitialize() override = 0;
};
#define MPT_COMPONENT_BIND(libName, func) do { if(!Bind( func , libName , #func )) { SetBindFailed(); } } while(0)
#define MPT_COMPONENT_BIND_OPTIONAL(libName, func) Bind( func , libName , #func )
#define MPT_COMPONENT_BIND_SYMBOL(libName, symbol, func) do { if(!Bind( func , libName , symbol )) { SetBindFailed(); } } while(0)
#define MPT_COMPONENT_BIND_SYMBOL_OPTIONAL(libName, symbol, func) Bind( func , libName , symbol )
#if MPT_OS_WINDOWS
#ifdef UNICODE
#define MPT_COMPONENT_BINDWIN_SUFFIX "W"
#else
#define MPT_COMPONENT_BINDWIN_SUFFIX "A"
#endif
#define MPT_COMPONENT_BINDWIN(libName, func) do { if(!Bind( func , libName , #func MPT_COMPONENT_BINDWIN_SUFFIX )) { SetBindFailed(); } } while(0)
#define MPT_COMPONENT_BINDWIN_OPTIONAL(libName, func) Bind( func , libName , #func MPT_COMPONENT_BINDWIN_SUFFIX )
#define MPT_COMPONENT_BINDWIN_SYMBOL(libName, symbol, func) do { if(!Bind( func , libName , symbol MPT_COMPONENT_BINDWIN_SUFFIX )) { SetBindFailed(); } } while(0)
#define MPT_COMPONENT_BINDWIN_SYMBOL_OPTIONAL(libName, symbol, func) Bind( func , libName , symbol MPT_COMPONENT_BINDWIN_SUFFIX )
#endif
class ComponentSystemDLL : public ComponentLibrary
{
private:
mpt::PathString m_BaseName;
public:
ComponentSystemDLL(const mpt::PathString &baseName)
: ComponentLibrary(ComponentTypeSystem)
, m_BaseName(baseName)
{
return;
}
bool DoInitialize() override
{
AddLibrary(m_BaseName.ToUTF8(), mpt::LibraryPath::System(m_BaseName));
return GetLibrary(m_BaseName.ToUTF8()).IsValid();
}
};
class ComponentBundledDLL : public ComponentLibrary
{
private:
mpt::PathString m_FullName;
public:
ComponentBundledDLL(const mpt::PathString &fullName)
: ComponentLibrary(ComponentTypeBundled)
, m_FullName(fullName)
{
return;
}
bool DoInitialize() override
{
AddLibrary(m_FullName.ToUTF8(), mpt::LibraryPath::AppFullName(m_FullName));
return GetLibrary(m_FullName.ToUTF8()).IsValid();
}
};
#endif // MODPLUG_TRACKER
#if MPT_COMPONENT_MANAGER
class ComponentManager;
using ComponentFactoryMethod = std::shared_ptr<IComponent> (*)(ComponentManager &componentManager);
class IComponentFactory
{
protected:
IComponentFactory() = default;
public:
virtual ~IComponentFactory() = default;
public:
virtual std::string GetID() const = 0;
virtual std::string GetSettingsKey() const = 0;
virtual std::shared_ptr<IComponent> Construct(ComponentManager &componentManager) const = 0;
virtual ComponentFactoryMethod GetStaticConstructor() const = 0;
};
class ComponentFactoryBase
: public IComponentFactory
{
private:
std::string m_ID;
std::string m_SettingsKey;
protected:
ComponentFactoryBase(const std::string &id, const std::string &settingsKey);
void PreConstruct() const;
void Initialize(ComponentManager &componentManager, std::shared_ptr<IComponent> component) const;
public:
virtual ~ComponentFactoryBase();
std::string GetID() const override;
std::string GetSettingsKey() const override;
std::shared_ptr<IComponent> Construct(ComponentManager &componentManager) const override = 0;
ComponentFactoryMethod GetStaticConstructor() const override = 0;
};
template <typename T>
class ComponentFactory
: public ComponentFactoryBase
{
public:
ComponentFactory()
: ComponentFactoryBase(T::g_ID, T::g_SettingsKey)
{
return;
}
public:
std::shared_ptr<IComponent> Construct(ComponentManager &componentManager) const override
{
PreConstruct();
std::shared_ptr<IComponent> component = std::make_shared<T>();
Initialize(componentManager, component);
return component;
}
static std::shared_ptr<IComponent> StaticConstruct(ComponentManager &componentManager)
{
return ComponentFactory().Construct(componentManager);
}
virtual ComponentFactoryMethod GetStaticConstructor() const override
{
return &StaticConstruct;
}
};
class IComponentManagerSettings
{
public:
virtual bool LoadOnStartup() const = 0;
virtual bool KeepLoaded() const = 0;
virtual bool IsBlocked(const std::string &key) const = 0;
protected:
virtual ~IComponentManagerSettings() = default;
};
class ComponentManagerSettingsDefault
: public IComponentManagerSettings
{
public:
bool LoadOnStartup() const override { return false; }
bool KeepLoaded() const override { return true; }
bool IsBlocked(const std::string & /*key*/ ) const override { return false; }
};
enum ComponentState
{
ComponentStateUnregistered,
ComponentStateBlocked,
ComponentStateUnintialized,
ComponentStateUnavailable,
ComponentStateAvailable,
};
struct ComponentInfo
{
std::string name;
ComponentState state;
std::string settingsKey;
ComponentType type;
};
class ComponentManager
{
friend class ComponentFactoryBase;
public:
static void Init(const IComponentManagerSettings &settings);
static void Release();
static std::shared_ptr<ComponentManager> Instance();
private:
ComponentManager(const IComponentManagerSettings &settings);
private:
struct RegisteredComponent
{
std::string settingsKey;
ComponentFactoryMethod factoryMethod;
std::shared_ptr<IComponent> instance;
std::weak_ptr<IComponent> weakInstance;
};
using TComponentMap = std::map<std::string, RegisteredComponent>;
const IComponentManagerSettings &m_Settings;
TComponentMap m_Components;
private:
bool IsComponentBlocked(const std::string &settingsKey) const;
void InitializeComponent(std::shared_ptr<IComponent> component) const;
public:
void Register(const IComponentFactory &componentFactory);
void Startup();
std::shared_ptr<const IComponent> GetComponent(const IComponentFactory &componentFactory);
std::shared_ptr<const IComponent> ReloadComponent(const IComponentFactory &componentFactory);
std::vector<std::string> GetRegisteredComponents() const;
ComponentInfo GetComponentInfo(std::string name) const;
};
struct ComponentListEntry
{
ComponentListEntry *next;
void (*reg)(ComponentManager &componentManager);
};
bool ComponentListPush(ComponentListEntry *entry);
template <typename TComponent>
struct ComponentRegisterer
{
static inline void RegisterComponent(ComponentManager &componentManager)
{
componentManager.Register(ComponentFactory<TComponent>());
}
static inline ComponentListEntry &GetComponentListEntry()
{
static ComponentListEntry s_ComponentListEntry = {nullptr, &RegisterComponent};
return s_ComponentListEntry;
}
static inline bool g_ComponentRegistered = ComponentListPush(&GetComponentListEntry());
};
#define MPT_DECLARE_COMPONENT_MEMBERS(name, settingsKey) \
public: \
static constexpr const char *g_ID = #name ; \
static constexpr const char *g_SettingsKey = settingsKey ; \
static inline ComponentRegisterer< name > s_ComponentRegisterer; \
/**/
template <typename type>
std::shared_ptr<const type> GetComponent()
{
return std::dynamic_pointer_cast<const type>(ComponentManager::Instance()->GetComponent(ComponentFactory<type>()));
}
template <typename type>
std::shared_ptr<const type> ReloadComponent()
{
return std::dynamic_pointer_cast<const type>(ComponentManager::Instance()->ReloadComponent(ComponentFactory<type>()));
}
#else // !MPT_COMPONENT_MANAGER
#define MPT_DECLARE_COMPONENT_MEMBERS(name, settingsKey)
template <typename type>
std::shared_ptr<const type> GetComponent()
{
#if MPT_COMPILER_CLANG
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wexit-time-destructors"
#endif // MPT_COMPILER_CLANG
static std::weak_ptr<type> cache;
static mpt::mutex m;
#if MPT_COMPILER_CLANG
#pragma clang diagnostic pop
#endif // MPT_COMPILER_CLANG mpt::lock_guard<mpt::mutex> l(m);
mpt::lock_guard<mpt::mutex> l(m);
std::shared_ptr<type> component = cache.lock();
if(!component)
{
component = std::make_shared<type>();
component->Initialize();
cache = component;
}
return component;
}
#endif // MPT_COMPONENT_MANAGER
// Simple wrapper around std::shared_ptr<ComponentType> which automatically
// gets a reference to the component (or constructs it) on initialization.
template <typename T>
class ComponentHandle
{
private:
std::shared_ptr<const T> component;
public:
ComponentHandle()
: component(GetComponent<T>())
{
return;
}
~ComponentHandle()
{
return;
}
bool IsAvailable() const
{
return component && component->IsAvailable();
}
const T *get() const
{
return component.get();
}
const T &operator*() const
{
return *component;
}
const T *operator->() const
{
return &*component;
}
#if MPT_COMPONENT_MANAGER
void Reload()
{
component = nullptr;
component = ReloadComponent<T>();
}
#endif
};
template <typename T>
bool IsComponentAvailable(const ComponentHandle<T> &handle)
{
return handle.IsAvailable();
}
OPENMPT_NAMESPACE_END
|