1
0

ComponentManager.h 11 KB

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