30
30
#include < windows.h>
31
31
#endif
32
32
33
- #ifndef SIMULATOR_INTERFACE_LOADER_METHOD
34
- #define SIMULATOR_INTERFACE_LOADER_DYNAMIC 1 // How to load simulator libraries: 1=dynamic load and unload; 0=load once (old way)
35
- #endif
36
-
37
- QMap<QString, QLibrary *> SimulatorLoader::registeredSimulators;
33
+ QMap<QString, QPair<QString, QLibrary *>> SimulatorLoader::registeredSimulators;
38
34
39
35
QStringList SimulatorLoader::getAvailableSimulators ()
40
36
{
@@ -45,42 +41,26 @@ int SimulatorLoader::registerSimulators(const QDir & dir)
45
41
{
46
42
QStringList filters;
47
43
#if defined(__APPLE__)
48
- filters << " *-simulator.dylib" ;
44
+ filters << " libedgetx- *-simulator.dylib" ;
49
45
#elif defined(WIN32) || defined(__CYGWIN__)
50
- filters << " *-simulator.dll" ;
46
+ filters << " libedgetx- *-simulator.dll" ;
51
47
#else
52
- filters << " *-simulator.so" ;
48
+ filters << " libedgetx- *-simulator.so" ;
53
49
#endif
54
50
registeredSimulators.clear ();
55
51
56
52
qCDebug (simulatorInterfaceLoader) << " Searching for simulators in" << dir.path () << " matching pattern" << filters;
57
53
58
54
foreach (QString filename, dir.entryList (filters, QDir::Files)) {
59
- QLibrary * lib = new QLibrary ( dir.path () + " /" + filename);
60
-
61
- qCDebug (simulatorInterfaceLoader) << " Trying to register simulator in " << filename;
55
+ QString simuName (filename.mid (3 , filename.lastIndexOf (' -' ) - 3 ));
56
+ QString libPath (dir.path () + " /" + filename);
62
57
63
- SimulatorFactory * factory;
64
- RegisterSimulator registerFunc = (RegisterSimulator)lib->resolve (" registerSimu" );
65
-
66
- if (registerFunc && (factory = registerFunc ())) {
67
- if (getAvailableSimulators ().contains (factory->name ()))
68
- continue ;
69
-
70
- lib->setProperty (" instances_used" , 0 );
71
- registeredSimulators.insert (factory->name (), lib);
72
- delete factory;
73
- #if SIMULATOR_INTERFACE_LOADER_DYNAMIC
74
- lib->unload ();
75
- #endif
76
- qCDebug (simulatorInterfaceLoader) << " Registered" << registeredSimulators.lastKey () << " simulator in " << lib->fileName () << " and unloaded:" << !lib->isLoaded ();
77
- }
78
- else {
79
- qWarning () << " Library error" << lib->fileName () << lib->errorString ();
80
- delete lib;
81
- }
58
+ if (getAvailableSimulators ().contains (simuName))
59
+ continue ;
82
60
61
+ registeredSimulators.insert (simuName, {libPath, nullptr });
83
62
}
63
+
84
64
qCDebug (simulatorInterfaceLoader) << " Found libraries:" << (registeredSimulators.size () ? registeredSimulators.keys () : QStringList () << " none" );
85
65
return registeredSimulators.size ();
86
66
}
@@ -106,8 +86,10 @@ void SimulatorLoader::registerSimulators()
106
86
107
87
void SimulatorLoader::unregisterSimulators ()
108
88
{
109
- foreach (QLibrary * lib, registeredSimulators)
110
- delete lib;
89
+ for (QPair<QString, QLibrary *> lib : registeredSimulators) {
90
+ if (lib.second )
91
+ delete lib.second ;
92
+ }
111
93
}
112
94
113
95
QString SimulatorLoader::findSimulatorByName (const QString & name)
@@ -133,22 +115,29 @@ QString SimulatorLoader::findSimulatorByName(const QString & name)
133
115
134
116
SimulatorInterface * SimulatorLoader::loadSimulator (const QString & name)
135
117
{
136
- SimulatorInterface * si = NULL ;
137
- QString libname = findSimulatorByName (name);
118
+ SimulatorInterface * si = nullptr ;
119
+ QString simuName = findSimulatorByName (name);
138
120
139
- if (libname .isEmpty ()) {
121
+ if (simuName .isEmpty ()) {
140
122
qWarning () << " Simulator" << name << " not found." ;
141
123
return si;
142
124
}
143
125
144
- QLibrary * lib = registeredSimulators.value (libname, NULL );
126
+ QPair<QString, QLibrary *> libInfo = registeredSimulators.value (simuName, {QString (), nullptr });
127
+ QString libPath = libInfo.first ;
128
+ QLibrary *lib = libInfo.second ;
129
+ qCDebug (simulatorInterfaceLoader) << " Trying to load simulator in " << libPath;
130
+
145
131
if (!lib) {
146
- qWarning () << " Simulator library is NULL" ;
147
- return si;
132
+ lib = new QLibrary (libPath);
133
+ if (lib)
134
+ registeredSimulators.insert (simuName, {libPath, lib});
135
+ else {
136
+ qWarning () << " Unable to load library" ;
137
+ return si;
138
+ }
148
139
}
149
140
150
- qCDebug (simulatorInterfaceLoader) << " Trying to load simulator in " << lib->fileName ();
151
-
152
141
SimulatorFactory * factory;
153
142
RegisterSimulator registerFunc = (RegisterSimulator)lib->resolve (" registerSimu" );
154
143
if (registerFunc && (factory = registerFunc ()) && (si = factory->create ())) {
@@ -158,20 +147,21 @@ SimulatorInterface * SimulatorLoader::loadSimulator(const QString & name)
158
147
delete factory;
159
148
}
160
149
else {
161
- qWarning () << " Library error" << lib-> fileName () << lib->errorString ();
150
+ qWarning () << " Library error" << libPath << lib->errorString ();
162
151
}
152
+
163
153
return si;
164
154
}
165
155
166
156
bool SimulatorLoader::unloadSimulator (const QString & name)
167
157
{
168
158
bool ret = false ;
169
- #if SIMULATOR_INTERFACE_LOADER_DYNAMIC
170
159
QString simuName = findSimulatorByName (name);
160
+
171
161
if (simuName.isEmpty ())
172
162
return ret;
173
163
174
- QLibrary * lib = registeredSimulators.value (simuName, NULL ) ;
164
+ QLibrary * lib = registeredSimulators.value (simuName). second ;
175
165
176
166
if (lib && lib->isLoaded ()) {
177
167
quint8 instance = lib->property (" instances_used" ).toUInt ();
@@ -188,9 +178,6 @@ bool SimulatorLoader::unloadSimulator(const QString & name)
188
178
else {
189
179
qCDebug (simulatorInterfaceLoader) << " Simulator library for " << simuName << " already unloaded." ;
190
180
}
191
- #else
192
- qCDebug (simulatorInterfaceLoader) << " Keeping simulator library" << simuName << " loaded." ;
193
- #endif
194
181
195
182
return ret;
196
183
}
0 commit comments