Qt 4.8
Public Functions | Static Public Functions | Public Variables | Private Types | Private Functions | Properties | Friends | List of all members
QLibraryPrivate Class Reference

#include <qlibrary_p.h>

Public Functions

bool isPlugin (QSettings *settings=0)
 
bool load ()
 
bool loadPlugin ()
 
void release ()
 
void * resolve (const char *)
 
bool unload ()
 

Static Public Functions

static QLibraryPrivatefindOrCreate (const QString &fileName, const QString &version=QString())
 

Public Variables

QString errorString
 
QString fileName
 
QString fullVersion
 
QWeakPointer< QObjectinst
 
QtPluginInstanceFunction instance
 
QString lastModified
 
QLibrary::LoadHints loadHints
 
HINSTANCE pHnd
 
uint qt_version
 
QString qualifiedFileName
 

Private Types

enum  { IsAPlugin, IsNotAPlugin, MightBeAPlugin }
 

Private Functions

bool load_sys ()
 
 QLibraryPrivate (const QString &canonicalFileName, const QString &version)
 
void * resolve_sys (const char *)
 
bool unload_sys ()
 
 ~QLibraryPrivate ()
 

Properties

QAtomicInt libraryRefCount
 
QAtomicInt libraryUnloadCount
 
enum QLibraryPrivate:: { ... }  pluginState
 

Friends

class QLibraryPrivateHasFriends
 

Detailed Description

Definition at line 72 of file qlibrary_p.h.

Enumerations

◆ anonymous enum

anonymous enum
private
Enumerator
IsAPlugin 
IsNotAPlugin 
MightBeAPlugin 

Definition at line 116 of file qlibrary_p.h.

Constructors and Destructors

◆ QLibraryPrivate()

QLibraryPrivate::QLibraryPrivate ( const QString canonicalFileName,
const QString version 
)
explicitprivate

Definition at line 433 of file qlibrary.cpp.

Referenced by findOrCreate().

434  :pHnd(0), fileName(canonicalFileName), fullVersion(version), instance(0), qt_version(0),
436 { libraryMap()->insert(canonicalFileName, this); }
QtPluginInstanceFunction instance
Definition: qlibrary_p.h:95
QAtomicInt libraryRefCount
Definition: qlibrary_p.h:113
enum QLibraryPrivate::@42 pluginState
QString fileName
Definition: qlibrary_p.h:83
static LibraryMap * libraryMap()
Definition: qlibrary.cpp:427
HINSTANCE pHnd
Definition: qlibrary_p.h:81
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
QString fullVersion
Definition: qlibrary_p.h:84
QAtomicInt libraryUnloadCount
Definition: qlibrary_p.h:114

◆ ~QLibraryPrivate()

QLibraryPrivate::~QLibraryPrivate ( )
private

Definition at line 449 of file qlibrary.cpp.

450 {
451  LibraryMap * const map = libraryMap();
452  if (map) {
453  QLibraryPrivate *that = map->take(fileName);
454  Q_ASSERT(this == that);
455  Q_UNUSED(that);
456  }
457 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QString fileName
Definition: qlibrary_p.h:83
static LibraryMap * libraryMap()
Definition: qlibrary.cpp:427
T take(const Key &key)
Removes the item with the key key from the map and returns the value associated with it...
Definition: qmap.h:692
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67

Functions

◆ findOrCreate()

QLibraryPrivate * QLibraryPrivate::findOrCreate ( const QString fileName,
const QString version = QString() 
)
static

Definition at line 438 of file qlibrary.cpp.

Referenced by QPluginLoader::setFileName(), QLibrary::setFileName(), QLibrary::setFileNameAndVersion(), QLibrary::setLoadHints(), and QFactoryLoader::updateDir().

439 {
440  QMutexLocker locker(qt_library_mutex());
441  if (QLibraryPrivate *lib = libraryMap()->value(fileName)) {
442  lib->libraryRefCount.ref();
443  return lib;
444  }
445 
446  return new QLibraryPrivate(fileName, version);
447 }
QLibraryPrivate(const QString &canonicalFileName, const QString &version)
Definition: qlibrary.cpp:433
static LibraryMap * libraryMap()
Definition: qlibrary.cpp:427
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101

◆ isPlugin()

bool QLibraryPrivate::isPlugin ( QSettings settings = 0)

Definition at line 669 of file qlibrary.cpp.

Referenced by QPluginLoader::load(), and QFactoryLoader::updateDir().

670 {
671  errorString.clear();
673  return pluginState == IsAPlugin;
674 
675 #ifndef QT_NO_PLUGIN_CHECK
676  bool debug = !QLIBRARY_AS_DEBUG;
677  QByteArray key;
678  bool success = false;
679 
680 #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
681  if (fileName.endsWith(QLatin1String(".debug"))) {
682  // refuse to load a file that ends in .debug
683  // these are the debug symbols from the libraries
684  // the problem is that they are valid shared library files
685  // and dlopen is known to crash while opening them
686 
687  // pretend we didn't see the file
688  errorString = QLibrary::tr("The shared library was not found.");
690  return false;
691  }
692 #endif
693 
694  QFileInfo fileinfo(fileName);
695 
696 #ifndef QT_NO_DATESTRING
697  lastModified = fileinfo.lastModified().toString(Qt::ISODate);
698 #endif
699  QString regkey = QString::fromLatin1("Qt Plugin Cache %1.%2.%3/%4")
700  .arg((QT_VERSION & 0xff0000) >> 16)
701  .arg((QT_VERSION & 0xff00) >> 8)
702  .arg(QLIBRARY_AS_DEBUG ? QLatin1String("debug") : QLatin1String("false"))
703  .arg(fileName);
704 #ifdef Q_WS_MAC
705  // On Mac, add the application arch to the reg key in order to
706  // cache plugin information separately for each arch. This prevents
707  // Qt from wrongly caching plugin load failures when the archs
708  // don't match.
709 #if defined(__x86_64__)
710  regkey += QLatin1String("-x86_64");
711 #elif defined(__i386__)
712  regkey += QLatin1String("-i386");
713 #elif defined(__ppc64__)
714  regkey += QLatin1String("-ppc64");
715 #elif defined(__ppc__)
716  regkey += QLatin1String("-ppc");
717 #endif
718 #endif // Q_WS_MAC
719 
720  QStringList reg;
721 #ifndef QT_NO_SETTINGS
722  if (!settings) {
724  }
725  reg = settings->value(regkey).toStringList();
726 #endif
727  if (reg.count() == 4 && lastModified == reg.at(3)) {
728  qt_version = reg.at(0).toUInt(0, 16);
729  debug = bool(reg.at(1).toInt());
730  key = reg.at(2).toLatin1();
731  success = qt_version != 0;
732  } else {
733 #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) && !defined(Q_OS_SYMBIAN)
734  if (!pHnd) {
735  // use unix shortcut to avoid loading the library
736  success = qt_unix_query(fileName, &qt_version, &debug, &key, this);
737  } else
738 #endif
739  {
740  bool retryLoadLibrary = false; // Only used on Windows with MS compiler.(false in other cases)
741  do {
742  bool temporary_load = false;
743 #ifdef Q_OS_WIN
744  HMODULE hTempModule = 0;
745 #endif
746  if (!pHnd) {
747 #ifdef Q_OS_WIN
748  DWORD dwFlags = (retryLoadLibrary) ? 0: DONT_RESOLVE_DLL_REFERENCES;
749  //avoid 'Bad Image' message box
751  hTempModule = ::LoadLibraryEx((wchar_t*)QDir::toNativeSeparators(fileName).utf16(), 0, dwFlags);
752  SetErrorMode(oldmode);
753 #else
754 # if defined(Q_OS_SYMBIAN)
755  //Guard against accidentally trying to load non-plugin libraries by making sure the stub exists
756  if (fileinfo.exists())
757 # endif
758  temporary_load = load_sys();
759 #endif
760  }
761 #ifdef Q_OS_WIN
762  QtPluginQueryVerificationDataFunction qtPluginQueryVerificationDataFunction = hTempModule ? (QtPluginQueryVerificationDataFunction)
763 #ifdef Q_OS_WINCE
764  ::GetProcAddress(hTempModule, L"qt_plugin_query_verification_data")
765 #else
766  ::GetProcAddress(hTempModule, "qt_plugin_query_verification_data")
767 #endif
768  : (QtPluginQueryVerificationDataFunction) resolve("qt_plugin_query_verification_data");
769 #else
770  QtPluginQueryVerificationDataFunction qtPluginQueryVerificationDataFunction = NULL;
771 # if defined(Q_OS_SYMBIAN)
772  if (temporary_load) {
773  qtPluginQueryVerificationDataFunction = (QtPluginQueryVerificationDataFunction) resolve("qt_plugin_query_verification_data");
774  // If resolving with function name failed (i.e. not STDDLL), try resolving using known ordinal
775  if (!qtPluginQueryVerificationDataFunction)
776  qtPluginQueryVerificationDataFunction = (QtPluginQueryVerificationDataFunction) resolve("1");
777  }
778 # else
779  qtPluginQueryVerificationDataFunction = (QtPluginQueryVerificationDataFunction) resolve("qt_plugin_query_verification_data");
780 # endif
781 #endif
782  bool exceptionThrown = false;
783  bool ret = qt_get_verificationdata(qtPluginQueryVerificationDataFunction,
784  &qt_version, &debug, &key, &exceptionThrown);
785  if (!exceptionThrown) {
786  if (!ret) {
787  qt_version = 0;
788  key = "unknown";
789  if (temporary_load)
790  unload_sys();
791  } else {
792  success = true;
793  }
794  retryLoadLibrary = false;
795  }
796 #ifdef QT_USE_MS_STD_EXCEPTION
797  else {
798  // An exception was thrown when calling qt_plugin_query_verification_data().
799  // This usually happens when plugin is compiled with the /clr compiler flag,
800  // & will only work if the dependencies are loaded & DLLMain() is called.
801  // LoadLibrary() will do this, try once with this & if it fails don't load.
802  retryLoadLibrary = !retryLoadLibrary;
803  }
804 #endif
805 #ifdef Q_OS_WIN
806  if (hTempModule) {
807  BOOL ok = ::FreeLibrary(hTempModule);
808  if (ok) {
809  hTempModule = 0;
810  }
811 
812  }
813 #endif
814  } while(retryLoadLibrary); // Will be 'false' in all cases other than when an
815  // exception is thrown(will happen only when using a MS compiler)
816  }
817 
818  // Qt 4.5 compatibility: stl doesn't affect binary compatibility
819  key.replace(" no-stl", "");
820 
821 #ifndef QT_NO_SETTINGS
822  QStringList queried;
823  queried << QString::number(qt_version,16)
824  << QString::number((int)debug)
825  << QLatin1String(key)
826  << lastModified;
827  settings->setValue(regkey, queried);
828 #endif
829  }
830 
831  if (!success) {
832  if (errorString.isEmpty()){
833  if (fileName.isEmpty())
834  errorString = QLibrary::tr("The shared library was not found.");
835  else
836  errorString = QLibrary::tr("The file '%1' is not a valid Qt plugin.").arg(fileName);
837  }
838  return false;
839  }
840 
841  pluginState = IsNotAPlugin; // be pessimistic
842 
843  if ((qt_version & 0x00ff00) > (QT_VERSION & 0x00ff00) || (qt_version & 0xff0000) != (QT_VERSION & 0xff0000)) {
844  if (qt_debug_component()) {
845  qWarning("In %s:\n"
846  " Plugin uses incompatible Qt library (%d.%d.%d) [%s]",
847  (const char*) QFile::encodeName(fileName),
848  (qt_version&0xff0000) >> 16, (qt_version&0xff00) >> 8, qt_version&0xff,
849  debug ? "debug" : "release");
850  }
851  errorString = QLibrary::tr("The plugin '%1' uses incompatible Qt library. (%2.%3.%4) [%5]")
852  .arg(fileName)
853  .arg((qt_version&0xff0000) >> 16)
854  .arg((qt_version&0xff00) >> 8)
855  .arg(qt_version&0xff)
856  .arg(debug ? QLatin1String("debug") : QLatin1String("release"));
857  } else if (key != QT_BUILD_KEY
858  // we may have some compatibility keys, try them too:
859 #ifdef QT_BUILD_KEY_COMPAT
860  && key != QT_BUILD_KEY_COMPAT
861 #endif
862 #ifdef QT_BUILD_KEY_COMPAT2
863  && key != QT_BUILD_KEY_COMPAT2
864 #endif
865 #ifdef QT_BUILD_KEY_COMPAT3
866  && key != QT_BUILD_KEY_COMPAT3
867 #endif
868  ) {
869  if (qt_debug_component()) {
870  qWarning("In %s:\n"
871  " Plugin uses incompatible Qt library\n"
872  " expected build key \"%s\", got \"%s\"",
873  (const char*) QFile::encodeName(fileName),
874  QT_BUILD_KEY,
875  key.isEmpty() ? "<null>" : (const char *) key);
876  }
877  errorString = QLibrary::tr("The plugin '%1' uses incompatible Qt library."
878  " Expected build key \"%2\", got \"%3\"")
879  .arg(fileName)
881  .arg(key.isEmpty() ? QLatin1String("<null>") : QLatin1String((const char *) key));
882 #ifndef QT_NO_DEBUG_PLUGIN_CHECK
883  } else if(debug != QLIBRARY_AS_DEBUG) {
884  //don't issue a qWarning since we will hopefully find a non-debug? --Sam
885  errorString = QLibrary::tr("The plugin '%1' uses incompatible Qt library."
886  " (Cannot mix debug and release libraries.)").arg(fileName);
887 #endif
888  } else {
890  }
891 
892  return pluginState == IsAPlugin;
893 #else
894  Q_UNUSED(settings);
895  return pluginState == MightBeAPlugin;
896 #endif
897 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
void setValue(const QString &key, const QVariant &value)
Sets the value of setting key to value.
Definition: qsettings.cpp:3328
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Returns the value for setting key.
Definition: qsettings.cpp:3460
#define QLIBRARY_AS_DEBUG
Definition: qlibrary.cpp:72
int toInt(bool *ok=0, int base=10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 ...
Definition: qstring.cpp:6090
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QString lastModified
Definition: qlibrary_p.h:97
#define SEM_FAILCRITICALERRORS
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
void * resolve(const char *)
Definition: qlibrary.cpp:459
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool qt_get_verificationdata(QtPluginQueryVerificationDataFunction pfn, uint *qt_version, bool *debug, QByteArray *key, bool *exceptionThrown)
Definition: qlibrary.cpp:648
#define QT_BUILD_KEY
Definition: qglobal.h:1254
QStringList toStringList() const
Returns the variant as a QStringList if the variant has type() StringList, String ...
Definition: qvariant.cpp:2259
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
enum QLibraryPrivate::@42 pluginState
QString fileName
Definition: qlibrary_p.h:83
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
Q_CORE_EXPORT void qWarning(const char *,...)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
HINSTANCE pHnd
Definition: qlibrary_p.h:81
static QSettings * trolltechConf()
#define SetErrorMode(a)
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
QString errorString
Definition: qlibrary_p.h:99
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
int key
uint toUInt(bool *ok=0, int base=10) const
Returns the string converted to an unsigned int using base base, which is 10 by default and must be b...
Definition: qstring.cpp:6120
#define SEM_NOOPENFILEERRORBOX
static QByteArray encodeName(const QString &fileName)
By default, this function converts fileName to the local 8-bit encoding determined by the user&#39;s loca...
Definition: qfile.cpp:528
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
static QString toNativeSeparators(const QString &pathName)
Returns pathName with the &#39;/&#39; separators converted to separators that are appropriate for the underly...
Definition: qdir.cpp:812
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
bool qt_debug_component()
Definition: qlibrary.cpp:1319
const char *(* QtPluginQueryVerificationDataFunction)()
Definition: qlibrary.cpp:645
#define QT_VERSION
This macro expands a numeric value of the form 0xMMNNPP (MM = major, NN = minor, PP = patch) that spe...
Definition: qglobal.h:51

◆ load()

bool QLibraryPrivate::load ( )

Definition at line 467 of file qlibrary.cpp.

Referenced by loadPlugin().

468 {
470  if (pHnd)
471  return true;
472  if (fileName.isEmpty())
473  return false;
474 
475  bool ret = load_sys();
476  if (ret) {
477  //when loading a library we add a reference to it so that the QLibraryPrivate won't get deleted
478  //this allows to unload the library at a later time
479  if (LibraryData *lib = libraryData()) {
480  lib->loadedLibs += this;
482  }
483  }
484 
485  return ret;
486 }
bool ref()
Atomically increments the value of this QAtomicInt.
QAtomicInt libraryRefCount
Definition: qlibrary_p.h:113
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QString fileName
Definition: qlibrary_p.h:83
HINSTANCE pHnd
Definition: qlibrary_p.h:81
QAtomicInt libraryUnloadCount
Definition: qlibrary_p.h:114

◆ load_sys()

bool QLibraryPrivate::load_sys ( )
private

Definition at line 83 of file qlibrary_unix.cpp.

Referenced by isPlugin(), and load().

84 {
85  QString attempt;
86 #if !defined(QT_NO_DYNAMIC_LIBRARY)
87  QFileSystemEntry fsEntry(fileName);
88 
89 #if defined(Q_OS_SYMBIAN)
90  QString path; // In Symbian, always resolve with just the filename
91  QString name;
92 
93  // Replace possible ".qtplugin" suffix with ".dll"
94  if (fsEntry.suffix() == QLatin1String("qtplugin"))
95  name = fsEntry.completeBaseName() + QLatin1String(".dll");
96  else
97  name = fsEntry.fileName();
98 #else
99  QString path = fsEntry.path();
100  QString name = fsEntry.fileName();
101  if (path == QLatin1String(".") && !fileName.startsWith(path))
102  path.clear();
103  else
104  path += QLatin1Char('/');
105 #endif
106 
107  QStringList suffixes;
108  QStringList prefixes;
109  if (pluginState != IsAPlugin) {
110 #if !defined(Q_OS_SYMBIAN)
111  prefixes << QLatin1String("lib");
112 #endif
113 #if defined(Q_OS_HPUX)
114  // according to
115  // http://docs.hp.com/en/B2355-90968/linkerdifferencesiapa.htm
116 
117  // In PA-RISC (PA-32 and PA-64) shared libraries are suffixed
118  // with .sl. In IPF (32-bit and 64-bit), the shared libraries
119  // are suffixed with .so. For compatibility, the IPF linker
120  // also supports the .sl suffix.
121 
122  // But since we don't know if we are built on HPUX or HPUXi,
123  // we support both .sl (and .<version>) and .so suffixes but
124  // .so is preferred.
125 # if defined(__ia64)
126  if (!fullVersion.isEmpty()) {
127  suffixes << QString::fromLatin1(".so.%1").arg(fullVersion);
128  } else {
129  suffixes << QLatin1String(".so");
130  }
131 # endif
132  if (!fullVersion.isEmpty()) {
133  suffixes << QString::fromLatin1(".sl.%1").arg(fullVersion);
134  suffixes << QString::fromLatin1(".%1").arg(fullVersion);
135  } else {
136  suffixes << QLatin1String(".sl");
137  }
138 #elif defined(Q_OS_SYMBIAN)
139  suffixes << QLatin1String(".dll");
140 #else
141 #ifdef Q_OS_AIX
142  suffixes << ".a";
143 #endif // Q_OS_AIX
144  if (!fullVersion.isEmpty()) {
145  suffixes << QString::fromLatin1(".so.%1").arg(fullVersion);
146  } else {
147  suffixes << QLatin1String(".so");
148  }
149 #endif
150 # ifdef Q_OS_MAC
151  if (!fullVersion.isEmpty()) {
152  suffixes << QString::fromLatin1(".%1.bundle").arg(fullVersion);
153  suffixes << QString::fromLatin1(".%1.dylib").arg(fullVersion);
154  } else {
155  suffixes << QLatin1String(".bundle") << QLatin1String(".dylib");
156  }
157 #endif
158  }
159  int dlFlags = 0;
160 #if defined(QT_HPUX_LD)
161  dlFlags = DYNAMIC_PATH | BIND_NONFATAL;
163  dlFlags |= BIND_IMMEDIATE;
164  } else {
165  dlFlags |= BIND_DEFERRED;
166  }
167 #else
168  if (loadHints & QLibrary::ResolveAllSymbolsHint) {
169  dlFlags |= RTLD_NOW;
170  } else {
171  dlFlags |= RTLD_LAZY;
172  }
174  dlFlags |= RTLD_GLOBAL;
175  }
176 #if !defined(Q_OS_CYGWIN)
177  else {
178 #if defined(Q_OS_MAC)
180 #endif
181  dlFlags |= RTLD_LOCAL;
182  }
183 #endif
184 #if defined(Q_OS_AIX) // Not sure if any other platform actually support this thing.
186  dlFlags |= RTLD_MEMBER;
187  }
188 #endif
189 #endif // QT_HPUX_LD
190  // If using the new search heuristics we do:
191  //
192  // If the filename is an absolute path then we want to try that first as it is most likely
193  // what the callee wants. If we have been given a non-absolute path then lets try the
194  // native library name first to avoid unnecessary calls to dlopen().
195  //
196  // otherwise:
197  //
198  // We use the old behaviour which is to always try the specified filename first
199  if ((loadHints & QLibrary::ImprovedSearchHeuristics) && !fsEntry.isAbsolute()) {
200  suffixes.append(QLatin1String(""));
201  prefixes.append(QLatin1String(""));
202  } else {
203  suffixes.prepend(QLatin1String(""));
204  prefixes.prepend(QLatin1String(""));
205  }
206 
207  bool retry = true;
208  for(int prefix = 0; retry && !pHnd && prefix < prefixes.size(); prefix++) {
209  for(int suffix = 0; retry && !pHnd && suffix < suffixes.size(); suffix++) {
210  if (!prefixes.at(prefix).isEmpty() && name.startsWith(prefixes.at(prefix)))
211  continue;
212  if (!suffixes.at(suffix).isEmpty() && name.endsWith(suffixes.at(suffix)))
213  continue;
214  if (loadHints & QLibrary::LoadArchiveMemberHint) {
215  attempt = name;
216  int lparen = attempt.indexOf(QLatin1Char('('));
217  if (lparen == -1)
218  lparen = attempt.count();
219  attempt = path + prefixes.at(prefix) + attempt.insert(lparen, suffixes.at(suffix));
220  } else {
221  attempt = path + prefixes.at(prefix) + name + suffixes.at(suffix);
222  }
223 #if defined(QT_HPUX_LD)
224  pHnd = (void*)shl_load(QFile::encodeName(attempt), dlFlags, 0);
225 #else
226  pHnd = dlopen(QFile::encodeName(attempt), dlFlags);
227 #endif
228 
229 #if defined(Q_OS_SYMBIAN)
230  // Never try again in symbian, dlopen already handles the library search logic,
231  // and there is only one possible suffix.
232  retry = false;
233 #else
234  if (!pHnd && fileName.startsWith(QLatin1Char('/')) && QFile::exists(attempt)) {
235  // We only want to continue if dlopen failed due to that the shared library did not exist.
236  // However, we are only able to apply this check for absolute filenames (since they are
237  // not influenced by the content of LD_LIBRARY_PATH, /etc/ld.so.cache, DT_RPATH etc...)
238  // This is all because dlerror is flawed and cannot tell us the reason why it failed.
239  retry = false;
240  }
241 #endif
242  }
243  }
244 
245 #ifdef Q_OS_MAC
246  if (!pHnd) {
247  QByteArray utf8Bundle = fileName.toUtf8();
248  QCFType<CFURLRef> bundleUrl = CFURLCreateFromFileSystemRepresentation(NULL, reinterpret_cast<const UInt8*>(utf8Bundle.data()), utf8Bundle.length(), true);
249  QCFType<CFBundleRef> bundle = CFBundleCreate(NULL, bundleUrl);
250  if(bundle) {
251  QCFType<CFURLRef> url = CFBundleCopyExecutableURL(bundle);
252  char executableFile[FILENAME_MAX];
253  CFURLGetFileSystemRepresentation(url, true, reinterpret_cast<UInt8*>(executableFile), FILENAME_MAX);
254  attempt = QString::fromUtf8(executableFile);
255  pHnd = dlopen(QFile::encodeName(attempt), dlFlags);
256  }
257  }
258 #endif
259 #endif // QT_NO_DYNAMIC_LIBRARY
260  if (!pHnd) {
261  errorString = QLibrary::tr("Cannot load library %1: %2").arg(fileName).arg(qdlerror());
262  }
263  if (pHnd) {
264  qualifiedFileName = attempt;
265  errorString.clear();
266  }
267  return (pHnd != 0);
268 }
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool exists() const
Returns true if the file specified by fileName() exists; otherwise returns false. ...
Definition: qfile.cpp:626
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
enum QLibraryPrivate::@42 pluginState
QString fileName
Definition: qlibrary_p.h:83
const char * name
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
int count() const
Definition: qstring.h:103
HINSTANCE pHnd
Definition: qlibrary_p.h:81
int length() const
Same as size().
Definition: qbytearray.h:356
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
QString errorString
Definition: qlibrary_p.h:99
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
QByteArray suffix
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
static QString qdlerror()
static QByteArray encodeName(const QString &fileName)
By default, this function converts fileName to the local 8-bit encoding determined by the user&#39;s loca...
Definition: qfile.cpp:528
static const MacVersion MacintoshVersion
the version of the Macintosh operating system on which the application is run (Mac only)...
Definition: qglobal.h:1646
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
QLibrary::LoadHints loadHints
Definition: qlibrary_p.h:100
QString qualifiedFileName
Definition: qlibrary_p.h:83
QString & insert(int i, QChar c)
Definition: qstring.cpp:1671
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QString fullVersion
Definition: qlibrary_p.h:84

◆ loadPlugin()

bool QLibraryPrivate::loadPlugin ( )

Definition at line 517 of file qlibrary.cpp.

Referenced by QFactoryLoader::instance(), QPluginLoader::load(), and QFactoryLoader::updateDir().

518 {
519  if (instance) {
521  return true;
522  }
523  if (pluginState == IsNotAPlugin)
524  return false;
525  if (load()) {
526  instance = (QtPluginInstanceFunction)resolve("qt_plugin_instance");
527 #if defined(Q_OS_SYMBIAN)
528  if (!instance) {
529  // If resolving with function name failed (i.e. not STDDLL),
530  // try resolving using known ordinal, which for
531  // qt_plugin_instance function is always "2".
533  }
534 #endif
535  return instance;
536  }
537  if (qt_debug_component())
538  qWarning() << "QLibraryPrivate::loadPlugin failed on" << fileName << ":" << errorString;
540  return false;
541 }
QtPluginInstanceFunction instance
Definition: qlibrary_p.h:95
bool ref()
Atomically increments the value of this QAtomicInt.
void * resolve(const char *)
Definition: qlibrary.cpp:459
QObject *(* QtPluginInstanceFunction)()
Definition: qplugin.h:62
enum QLibraryPrivate::@42 pluginState
QString fileName
Definition: qlibrary_p.h:83
Q_CORE_EXPORT void qWarning(const char *,...)
QString errorString
Definition: qlibrary_p.h:99
bool qt_debug_component()
Definition: qlibrary.cpp:1319
QAtomicInt libraryUnloadCount
Definition: qlibrary_p.h:114

◆ release()

void QLibraryPrivate::release ( )

Definition at line 510 of file qlibrary.cpp.

Referenced by QPluginLoader::setFileName(), QLibrary::setFileName(), QLibrary::setFileNameAndVersion(), QFactoryLoader::updateDir(), QLibrary::~QLibrary(), and QPluginLoader::~QPluginLoader().

511 {
512  QMutexLocker locker(qt_library_mutex());
513  if (!libraryRefCount.deref())
514  delete this;
515 }
QAtomicInt libraryRefCount
Definition: qlibrary_p.h:113
bool deref()
Atomically decrements the value of this QAtomicInt.
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101

◆ resolve()

void * QLibraryPrivate::resolve ( const char *  symbol)

Definition at line 459 of file qlibrary.cpp.

Referenced by isPlugin(), loadPlugin(), and QLibrary::resolve().

460 {
461  if (!pHnd)
462  return 0;
463  return resolve_sys(symbol);
464 }
void * resolve_sys(const char *)
HINSTANCE pHnd
Definition: qlibrary_p.h:81

◆ resolve_sys()

void * QLibraryPrivate::resolve_sys ( const char *  symbol)
private

Definition at line 293 of file qlibrary_unix.cpp.

Referenced by resolve().

294 {
295 #if defined(QT_AOUT_UNDERSCORE)
296  // older a.out systems add an underscore in front of symbols
297  char* undrscr_symbol = new char[strlen(symbol)+2];
298  undrscr_symbol[0] = '_';
299  strcpy(undrscr_symbol+1, symbol);
300  void* address = dlsym(pHnd, undrscr_symbol);
301  delete [] undrscr_symbol;
302 #elif defined(QT_HPUX_LD)
303  void* address = 0;
304  if (shl_findsym((shl_t*)&pHnd, symbol, TYPE_UNDEFINED, &address) < 0)
305  address = 0;
306 #elif defined (QT_NO_DYNAMIC_LIBRARY)
307  void *address = 0;
308 #else
309  void* address = dlsym(pHnd, symbol);
310 #endif
311  if (!address) {
312  errorString = QLibrary::tr("Cannot resolve symbol \"%1\" in %2: %3").arg(
314  } else {
315  errorString.clear();
316  }
317  return address;
318 }
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QString fileName
Definition: qlibrary_p.h:83
HINSTANCE pHnd
Definition: qlibrary_p.h:81
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
QString errorString
Definition: qlibrary_p.h:99
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
static QString qdlerror()

◆ unload()

bool QLibraryPrivate::unload ( )

Definition at line 488 of file qlibrary.cpp.

Referenced by QPluginLoader::unload(), and QFactoryLoader::updateDir().

489 {
490  if (!pHnd)
491  return false;
492  if (!libraryUnloadCount.deref()) { // only unload if ALL QLibrary instance wanted to
493  delete inst.data();
494  if (unload_sys()) {
495  if (qt_debug_component())
496  qWarning() << "QLibraryPrivate::unload succeeded on" << fileName;
497  //when the library is unloaded, we release the reference on it so that 'this'
498  //can get deleted
499  if (LibraryData *lib = libraryData()) {
500  if (lib->loadedLibs.remove(this))
502  }
503  pHnd = 0;
504  }
505  }
506 
507  return (pHnd == 0);
508 }
QWeakPointer< QObject > inst
Definition: qlibrary_p.h:94
QAtomicInt libraryRefCount
Definition: qlibrary_p.h:113
T * data() const
QString fileName
Definition: qlibrary_p.h:83
bool deref()
Atomically decrements the value of this QAtomicInt.
Q_CORE_EXPORT void qWarning(const char *,...)
HINSTANCE pHnd
Definition: qlibrary_p.h:81
bool qt_debug_component()
Definition: qlibrary.cpp:1319
QAtomicInt libraryUnloadCount
Definition: qlibrary_p.h:114

◆ unload_sys()

bool QLibraryPrivate::unload_sys ( )
private

Definition at line 270 of file qlibrary_unix.cpp.

Referenced by isPlugin(), and unload().

271 {
272 #if !defined(QT_NO_DYNAMIC_LIBRARY)
273 # if defined(QT_HPUX_LD)
274  if (shl_unload((shl_t)pHnd)) {
275 # else
276  if (dlclose(pHnd)) {
277 # endif
278  errorString = QLibrary::tr("Cannot unload library %1: %2").arg(fileName).arg(qdlerror());
279  return false;
280  }
281 #endif
282  errorString.clear();
283  return true;
284 }
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QString fileName
Definition: qlibrary_p.h:83
HINSTANCE pHnd
Definition: qlibrary_p.h:81
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
QString errorString
Definition: qlibrary_p.h:99
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
static QString qdlerror()

Friends and Related Functions

◆ QLibraryPrivateHasFriends

friend class QLibraryPrivateHasFriends
friend

Definition at line 117 of file qlibrary_p.h.

Properties

◆ errorString

QString QLibraryPrivate::errorString

◆ fileName

QString QLibraryPrivate::fileName

◆ fullVersion

QString QLibraryPrivate::fullVersion

Definition at line 84 of file qlibrary_p.h.

Referenced by load_sys().

◆ inst

QWeakPointer<QObject> QLibraryPrivate::inst

Definition at line 94 of file qlibrary_p.h.

Referenced by QPluginLoader::instance(), and unload().

◆ instance

QtPluginInstanceFunction QLibraryPrivate::instance

◆ lastModified

QString QLibraryPrivate::lastModified

Definition at line 97 of file qlibrary_p.h.

Referenced by isPlugin(), and QFactoryLoader::updateDir().

◆ libraryRefCount

QAtomicInt QLibraryPrivate::libraryRefCount
private

Definition at line 113 of file qlibrary_p.h.

Referenced by load(), release(), and unload().

◆ libraryUnloadCount

QAtomicInt QLibraryPrivate::libraryUnloadCount
private

Definition at line 114 of file qlibrary_p.h.

Referenced by load(), loadPlugin(), and unload().

◆ loadHints

QLibrary::LoadHints QLibraryPrivate::loadHints

◆ pHnd

HINSTANCE QLibraryPrivate::pHnd

◆ pluginState

enum { ... } QLibraryPrivate::pluginState

Referenced by isPlugin(), load_sys(), and loadPlugin().

◆ qt_version

uint QLibraryPrivate::qt_version

Definition at line 96 of file qlibrary_p.h.

Referenced by isPlugin(), and QFactoryLoader::updateDir().

◆ qualifiedFileName

QString QLibraryPrivate::qualifiedFileName

Definition at line 83 of file qlibrary_p.h.

Referenced by load_sys(), and QLibrary::setFileName().


The documentation for this class was generated from the following files: