pluginmanager.h
6.95 KB
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
/* ****************************************************************************
* Copyright 2019 Open Systems Development BV *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* ***************************************************************************/
#ifndef OSDEV_COMPONENTS_PLUGINMANAGER_H
#define OSDEV_COMPONENTS_PLUGINMANAGER_H
#include <QObject>
#include <QUuid>
#include <QHash>
#include <QList>
#include <QStringList>
#include "plugin.h"
#include "globallibexport.h"
#include <memory>
namespace osdev {
namespace components {
/**
* \brief This component is responsible for loading plugins.
*
* It also has support for supplying interfaces on the loaded pluins. The plugin
* manager is a singleton component.
*//*
* ________________________________________
* / One of the signs of Napoleon's \
* | greatness is the fact that he once had |
* | a publisher shot. |
* | |
* \ -- Siegfried Unseld /
* ----------------------------------------
* \
* \
* .--.
* |o_o |
* |:_/ |
* // \ \
* (| | )
* /'\_ _/`\
* \___)=(___/
*
*/
class GLOBALLIBINTERFACE PluginManager : public QObject
{
Q_OBJECT
public:
//! Constructs the plugin manager
static PluginManager& Instance();
/// Deleted copy-constructor
PluginManager( const PluginManager& ) = delete;
/// Deleted assignment operator
PluginManager& operator=(const PluginManager&) = delete;
/// Deleted move-constructor
PluginManager( PluginManager&& ) = delete;
/// Deleted move operator
PluginManager& operator=( PluginManager&& ) = delete;
//! Adds a plugin not listed in the configuration.
bool addLibrary( const QString& pluginName );
//! Load the plugin by its name and location
bool loadPlugin(const QString& pluginName);
//! Loads a list of plugins. Names are resolved against the paths added with addLibraryPaths().
bool loadPlugins(const QStringList& pluginNames);
//! Load all plugins set in the staging table.
bool loadPlugins();
//! Unloads a plugin and removes it from the manager
bool unloadPlugin( const QString& pluginName );
//! Specify where the plugin manager will look for plugins
void addLibraryPaths(const QStringList& paths);
//! Specify a single plugin path
void addLibraryPath(const QString& path);
//! Retrieve a loaded plugin with its object ID.
QObject* getPlugin(const QUuid &id) const;
//! Retrieve all objects that have the specified interface.
QList<QObject*> getPlugins(const QString& i_interface) const;
//! Check if a specific plugin was already loaded...
bool isLoaded( const QString& pluginName ) const;
//! Retrieve the uuid of the given plugin by its filename.
QUuid getPluginId( const QString& pluginName ) const;
//! Retrieve the uuid of the given plugin by its Interface_id
QUuid getPluginIdByInterfaceId( const QString &interface_id );
//! Retrieve the name of the given plugin by its uuid
QString getPluginNameByUuid( const QUuid &uuid );
//! Retrieve the plugin_id by its systemname.
QUuid getPluginUuidByName( const QString &name );
/**
* @brief Locate the interface with the specified name
* @tparam T Type of interface needed
* @param i_interface Name of the interface
* @return Interface implementation, or nullptr if no valid interface could
* be found
*/
template<class T>
T* queryInterface(const QString& i_interface);
public slots:
/**
* @brief Slot called when a configuration file changed
* @param fileName Name of the changed file
* @todo Implement me!
*/
void slotConfigChanged( const QString& fileName );
private:
//! Constructs the plugin manager (CTor)
PluginManager();
/**
* @brief Get all the plugins and create their representing objects
*/
void buildLibraryList();
/**
* @brief Find the library associated with a plugin-name
* @param pluginName Plugin name to look for
* @return Library-name, or an empty string if no library was found
*/
QString getLibrary(const QString& pluginName) const;
// ------------------------------------------------------
//! Contains the only instance of a Pluginmanager
static std::unique_ptr<PluginManager> s_instance;
//! Hash table of all plugins. Loaded or not...
QHash<QString, Plugin*> m_pluginHash;
//! List of all possible libraries that may be loaded by the PM....
QStringList m_libraryList;
//! Member holding the classname.
QString m_className;
};
// ------------------------------------------------------
template <class T>
T* PluginManager::queryInterface(const QString& i_interface)
{
// Hash used to store queried interfaces
static QHash<QString, T*> ifaceCache;
T* pInterface = nullptr;
if ( ifaceCache.contains(i_interface) )
{
pInterface = ifaceCache[i_interface];
}
else
{
QList<QObject*> pPluginList = PluginManager::Instance().getPlugins( i_interface );
if ( !pPluginList.isEmpty() )
{
// Return the first plugin with the wanted interface.
pInterface = qobject_cast<T*>(pPluginList[0]);
/// @todo What if we specify the wrong T for this plugin?
Q_ASSERT(pInterface);
ifaceCache.insert(i_interface, pInterface);
}
}
return pInterface;
}
} // End namespace components
} // End namespace osdev
#endif // OSDEV_COMPONENTS_PLUGINMANAGER_H