pluginloader.h
1 //------------------------------------------------------------------------------
2 // pluginloader.h
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2011 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 
24 #ifndef __PLUGINLOADER_H__
25 #define __PLUGINLOADER_H__
26 
27 #include <QList>
28 #include <QMap>
29 #include <QString>
30 
31 #include "dptr.h"
32 #include "global.h"
33 
34 #define gPlugins (PluginLoader::instance())
35 
36 class EnginePlugin;
37 
39 {
40 public:
45  class Plugin
46  {
47  public:
57  Plugin(unsigned int type, QString file);
58  virtual ~Plugin();
59 
63  void *function(const char *func) const;
64  void initConfig();
65  bool isValid() const;
69  EnginePlugin *info() const;
70 
71  private:
72  DPtr<Plugin> d;
73 
74  void unload();
75  };
76 
80  static void deinit();
97  static void init(const QStringList &directories,
98  const QMap<QString, bool> &pluginsEnabled);
106  static PluginLoader *instance();
107 
108  virtual ~PluginLoader();
109 
113  void clearPlugins();
114 
122  EnginePlugin *info(int pluginIndex) const;
123 
133  EnginePlugin *info(const QString &name) const
134  {
135  auto idx = pluginIndexFromName(name);
136  return idx >= 0 ? info(idx) : nullptr;
137  }
138 
142  void initConfig();
143 
149  unsigned int numPlugins() const;
150 
167  int pluginIndexFromName(const QString &name) const;
168 
169  const QList<Plugin *> &plugins() const;
173  const Plugin *plugin(unsigned int index) const;
174 
179  void resetPluginsDirectory(const QString &pluginsDirectory);
180 
184  const Plugin *operator[] (unsigned int index) const;
185 
186 private:
188 
189  static PluginLoader *staticInstance;
190 
191  PluginLoader(unsigned int type, const QStringList &directories,
192  const QMap<QString, bool> &pluginsEnabled);
193 
194  bool filesInDir();
195 };
196 
197 #endif /* __PLUGINLOADER_HPP__ */