PluginFactory.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * PluginFactory.h
  3. *
  4. * Copyright (c) 2015 Lukas W <lukaswhl/at/gmail.com>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef PLUGINFACTORY_H
  25. #define PLUGINFACTORY_H
  26. #include <memory>
  27. #include <QtCore/QFileInfo>
  28. #include <QtCore/QHash>
  29. #include <QtCore/QList>
  30. #include "export.h"
  31. #include "Plugin.h"
  32. class QLibrary;
  33. class EXPORT PluginFactory
  34. {
  35. public:
  36. struct PluginInfo
  37. {
  38. PluginInfo() : library(nullptr), descriptor(nullptr) {}
  39. const QString name() const;
  40. QFileInfo file;
  41. std::shared_ptr<QLibrary> library;
  42. Plugin::Descriptor* descriptor;
  43. bool isNull() const {return ! library;}
  44. };
  45. typedef QList<PluginInfo> PluginInfoList;
  46. typedef QMultiMap<Plugin::PluginTypes, Plugin::Descriptor*> DescriptorMap;
  47. PluginFactory();
  48. ~PluginFactory();
  49. /// Returns the singleton instance of PluginFactory. You won't need to call
  50. /// this directly, use pluginFactory instead.
  51. static PluginFactory* instance();
  52. /// Returns a list of all found plugins' descriptors.
  53. const Plugin::DescriptorList descriptors() const;
  54. const Plugin::DescriptorList descriptors(Plugin::PluginTypes type) const;
  55. /// Returns a list of all found plugins' PluginFactory::PluginInfo objects.
  56. const PluginInfoList& pluginInfos() const;
  57. /// Returns a plugin that support the given file extension
  58. const PluginInfo pluginSupportingExtension(const QString& ext);
  59. /// Returns the PluginInfo object of the plugin with the given name.
  60. /// If the plugin is not found, an empty PluginInfo is returned (use
  61. /// PluginInfo::isNull() to check this).
  62. const PluginInfo pluginInfo(const char* name) const;
  63. /// When loading a library fails during discovery, the error string is saved.
  64. /// It can be retrieved by calling this function.
  65. QString errorString(QString pluginName) const;
  66. public slots:
  67. void discoverPlugins();
  68. private:
  69. DescriptorMap m_descriptors;
  70. PluginInfoList m_pluginInfos;
  71. QMap<QString, PluginInfo> m_pluginByExt;
  72. QHash<QString, QString> m_errors;
  73. static std::unique_ptr<PluginFactory> s_instance;
  74. };
  75. #define pluginFactory PluginFactory::instance()
  76. #endif // PLUGINFACTORY_H