diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2016-09-11 08:42:52 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2016-09-16 17:53:40 +0200 |
commit | 546f9c503d6050251d9a623803b37ecb23f096fe (patch) | |
tree | 40d7daea2107f0e379d44dea529302a4d7c4dfc6 | |
parent | d85dcf1592ab27b12007b7b996d3c1384cf4ba49 (diff) |
api/plugins: simplify qlibrary usage
-rw-r--r-- | api/plugin-support.hpp | 68 |
1 files changed, 36 insertions, 32 deletions
diff --git a/api/plugin-support.hpp b/api/plugin-support.hpp index b1a6fcb0..d8a5ec8a 100644 --- a/api/plugin-support.hpp +++ b/api/plugin-support.hpp @@ -48,10 +48,10 @@ extern "C" typedef void* (*OPENTRACK_CTOR_FUNPTR)(void); extern "C" typedef Metadata* (*OPENTRACK_METADATA_FUNPTR)(void); struct dylib final { - enum Type { Filter, Tracker, Protocol }; + enum Type : unsigned { Filter, Tracker, Protocol, Invalid = 0xcafebabeU }; dylib(const QString& filename, Type t) : - type(t), + type(Invalid), filename(filename), Dialog(nullptr), Constructor(nullptr), @@ -62,46 +62,36 @@ struct dylib final { if (filename.size() == 0) return; - handle = new QLibrary(filename); - //handle->setLoadHints(QLibrary::PreventUnloadHint | handle->loadHints()); + handle.setFileName(filename); + handle.setLoadHints(QLibrary::DeepBindHint | QLibrary::LoadHint::ResolveAllSymbolsHint); - struct _foo { - static bool die(QLibrary*& l, bool failp) - { - if (failp) - { - qDebug() << "failed" << l->errorString(); - delete l; - l = nullptr; - } - return failp; - } - }; - - if (_foo::die(handle, !handle->load())) + if (check(!handle.load())) return; - Dialog = (OPENTRACK_CTOR_FUNPTR) handle->resolve("GetDialog"); - if (_foo::die(handle, !Dialog)) + if (check((Dialog = (OPENTRACK_CTOR_FUNPTR) handle.resolve("GetDialog"), !Dialog))) return; - Constructor = (OPENTRACK_CTOR_FUNPTR) handle->resolve("GetConstructor"); - if (_foo::die(handle, !Constructor)) + if (check((Constructor = (OPENTRACK_CTOR_FUNPTR) handle.resolve("GetConstructor"), !Constructor))) return; - Meta = (OPENTRACK_METADATA_FUNPTR) handle->resolve("GetMetadata"); - if (_foo::die(handle, !Meta)) + if (check((Meta = (OPENTRACK_METADATA_FUNPTR) handle.resolve("GetMetadata"), !Meta))) return; auto m = mem<Metadata>(Meta()); icon = m->icon(); name = m->name(); + + type = t; } ~dylib() { - if (handle) - delete handle; + if (handle.isLoaded()) + { + const bool success = handle.unload(); + if (!success) + qDebug() << "can't unload dylib" << filename << handle.errorString(); + } } static QList<mem<dylib>> enum_libraries(const QString& library_path) @@ -127,14 +117,11 @@ struct dylib final { QIcon icon; QString longName; auto lib = std::make_shared<dylib>(library_path + filename, t); - qDebug() << "Loading" << filename; - std::cout.flush(); if (!get_metadata(lib, longName, icon)) continue; - using d = const mem<dylib>&; if (std::any_of(ret.cbegin(), ret.cend(), - [&](d a) {return a->type == lib->type && a->name == lib->name;})) + [&](mem<dylib> a) {return a->type == lib->type && a->name == lib->name;})) { qDebug() << "Duplicate lib" << lib->filename; continue; @@ -156,7 +143,24 @@ struct dylib final { OPENTRACK_CTOR_FUNPTR Constructor; OPENTRACK_METADATA_FUNPTR Meta; private: - QLibrary* handle; + QLibrary handle; + + bool check(bool fail) + { + if (fail) + { + if (handle.isLoaded()) + (void) handle.unload(); + + Constructor = nullptr; + Dialog = nullptr; + Meta = nullptr; + + type = Invalid; + } + + return fail; + } static bool get_metadata(mem<dylib> lib, QString& name, QIcon& icon) { @@ -170,7 +174,7 @@ private: } }; -struct Modules +struct Modules final { Modules(const QString& library_path) : module_list(dylib::enum_libraries(library_path)), |