66#include < QDir>
77#include < QPluginLoader>
88#include < algorithm>
9+ #include < utility>
910
1011namespace QtNodes
1112{
@@ -49,7 +50,7 @@ registry()
4950{ return _register; };
5051
5152
52- int
53+ void
5354PluginsManager::
5455loadPlugins (const QString &folderPath)
5556{
@@ -68,44 +69,47 @@ loadPlugins(const QString &folderPath)
6869 {
6970 if (fileInfo.isFile ())
7071 {
71- qDebug () << " plugin path: " << fileInfo.absoluteFilePath ();
7272 loadPluginFromPath (fileInfo.absoluteFilePath ());
7373 }
7474 else
7575 {
7676 loadPlugins (fileInfo.absoluteFilePath ());
7777 }
7878 }
79- return 0 ;
8079}
8180
8281
8382void
8483PluginsManager::
8584unloadPlugins ()
8685{
87- for (QString filePath : _loaders.keys ())
88- unloadPluginFromPath (filePath);
86+ for (auto loadMap : _loaders)
87+ {
88+ unloadPluginFromPath (loadMap.second ->fileName ());
89+ }
8990}
9091
9192
92- int
93+ PluginInterface*
9394PluginsManager::
9495loadPluginFromPath (const QString & filePath)
9596{
9697 if (!QLibrary::isLibrary (filePath))
97- return - 1 ;
98+ return nullptr ;
9899
99100 QPluginLoader* loader = new QPluginLoader (filePath);
100101 if (loader->load ())
101102 {
102103 PluginInterface* plugin = qobject_cast<PluginInterface*>(loader->instance ());
103104 if (plugin)
104105 {
105- _plugins.push_back (plugin);
106- _loaders.insert (filePath, loader);
107- qDebug () << " add plugin: " << plugin->name ();
108- return 0 ;
106+ const QString name = plugin->name ();
107+ qDebug () << " add plugin: " << name;
108+
109+ _loaders[name] = loader;
110+ _plugins[filePath] = plugin;
111+
112+ return plugin;
109113 }
110114 else
111115 {
@@ -117,29 +121,54 @@ loadPluginFromPath(const QString & filePath)
117121 {
118122 qCritical () << " loadPlugin:" << filePath << loader->errorString ();
119123 }
120- return - 1 ;
124+ return nullptr ;
121125}
122126
123127
124128int
125129PluginsManager::
126130unloadPluginFromPath (const QString & filePath)
127131{
128- QPluginLoader* loader = _loaders.value (filePath);
129- PluginInterface* plugin = qobject_cast<PluginInterface*>(loader->instance ());
130- if (plugin)
132+ auto pluginIter = _plugins.find (filePath);
133+ if (pluginIter != _plugins.end ())
131134 {
132- // TODO: Verification required
133- _plugins.erase (std::remove (_plugins.begin (), _plugins.end (), plugin), _plugins.end ());
135+ auto loaderIter = _loaders.find (pluginIter->second ->name ());
136+ if (loaderIter != _loaders.end ())
137+ {
138+ // delete loader
139+ loaderIter->second ->unload ();
140+ delete loaderIter->second ;
141+ _loaders.erase (loaderIter->first );
142+ }
143+
144+ // delete plugin
145+ _plugins.erase (pluginIter->first );
146+ return 0 ;
134147 }
148+ return -1 ;
149+ }
135150
136- if (loader->unload ())
151+ int
152+ PluginsManager::
153+ unloadPluginFromName (const QString &pluginName)
154+ {
155+ auto loaderIter = _loaders.find (pluginName);
156+ if (loaderIter != _loaders.end ())
137157 {
138- _loaders.remove (filePath);
139- delete loader;
140- loader = nullptr ;
158+ auto pluginIter = _plugins.find (loaderIter->second ->fileName ());
159+ if (pluginIter != _plugins.end ())
160+ {
161+ // delete plugin
162+ _plugins.erase (pluginIter->first );
163+ }
164+
165+ // delete loaders
166+ loaderIter->second ->unload ();
167+ delete loaderIter->second ;
168+ _loaders.erase (loaderIter->first );
169+ return 0 ;
141170 }
142- return 0 ;
171+ return - 1 ;
143172}
144173
145174} // namespace QtNodes
0 commit comments