diff options
Diffstat (limited to 'api/plugin-support.hpp')
-rw-r--r-- | api/plugin-support.hpp | 103 |
1 files changed, 50 insertions, 53 deletions
diff --git a/api/plugin-support.hpp b/api/plugin-support.hpp index 5062a688..b9400e88 100644 --- a/api/plugin-support.hpp +++ b/api/plugin-support.hpp @@ -8,6 +8,7 @@ #pragma once #include "plugin-api.hpp" +#include "compat/library-path.hpp" #include <memory> #include <algorithm> @@ -21,69 +22,64 @@ #include <QList> #include <QIcon> -#if defined(__APPLE__) -# define OPENTRACK_SOLIB_EXT "dylib" -#elif defined(_WIN32) -# define OPENTRACK_SOLIB_EXT "dll" -#else -# define OPENTRACK_SOLIB_EXT "so" -#endif - -#define OPENTRACK_SOLIB_PREFIX "" - -extern "C" typedef void* (*OPENTRACK_CTOR_FUNPTR)(void); -extern "C" typedef Metadata_* (*OPENTRACK_METADATA_FUNPTR)(void); +extern "C" { + using module_ctor_t = void* (*)(void); + using module_metadata_t = Metadata_* (*)(void); +} struct dylib final { enum Type : unsigned { - Filter = 0xdeadbabeu, - Tracker = 0xcafebeefu, - Protocol = 0xdeadf00du, - Extension = 0xdeadf001u, - Invalid = 0xcafebabeu, + Filter = 0xdeadbabe, + Tracker = 0xcafebeef, + Protocol = 0xdeadf00d, + Extension = 0xcafebabe, + Invalid = (unsigned)-1, }; dylib(const QString& filename_, Type t) : - type(Invalid), full_filename(filename_), - module_name(trim_filename(filename_)), - Dialog(nullptr), - Constructor(nullptr), - Meta(nullptr) + module_name(trim_filename(filename_)) { // otherwise dlopen opens the calling executable - if (filename_.size() == 0 || module_name.size() == 0) + if (filename_.isEmpty() || module_name.isEmpty()) return; handle.setFileName(filename_); handle.setLoadHints(QLibrary::DeepBindHint | QLibrary::PreventUnloadHint | QLibrary::ResolveAllSymbolsHint); +#ifdef __clang__ +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wcomma" +#endif + if (check(!handle.load())) return; - if (check((Dialog = (OPENTRACK_CTOR_FUNPTR) handle.resolve("GetDialog"), !Dialog))) + if (check((Dialog = (module_ctor_t) handle.resolve("GetDialog"), !Dialog))) return; - if (check((Constructor = (OPENTRACK_CTOR_FUNPTR) handle.resolve("GetConstructor"), !Constructor))) + if (check((Constructor = (module_ctor_t) handle.resolve("GetConstructor"), !Constructor))) return; - if (check((Meta = (OPENTRACK_METADATA_FUNPTR) handle.resolve("GetMetadata"), !Meta))) + if (check((Meta = (module_metadata_t) handle.resolve("GetMetadata"), !Meta))) return; - auto m = std::unique_ptr<Metadata_>(Meta()); + std::unique_ptr<Metadata_> m{Meta()}; icon = m->icon(); name = m->name(); type = t; - } - ~dylib() - { - // QLibrary refcounts the .dll's so don't forcefully unload +#ifdef __clang__ +# pragma clang diagnostic pop +#endif } + // QLibrary refcounts the .dll's so don't forcefully unload + ~dylib() = default; + static QList<std::shared_ptr<dylib>> enum_libraries(const QString& library_path) { QDir module_directory(library_path); @@ -91,14 +87,14 @@ struct dylib final using str = QLatin1String; - static const struct filter_ { + const struct filter_ { Type type; QLatin1String glob; } filters[] = { - { Filter, str(OPENTRACK_SOLIB_PREFIX "opentrack-filter-*." OPENTRACK_SOLIB_EXT), }, - { Tracker, str(OPENTRACK_SOLIB_PREFIX "opentrack-tracker-*." OPENTRACK_SOLIB_EXT), }, - { Protocol, str(OPENTRACK_SOLIB_PREFIX "opentrack-proto-*." OPENTRACK_SOLIB_EXT), }, - { Extension, str(OPENTRACK_SOLIB_PREFIX "opentrack-ext-*." OPENTRACK_SOLIB_EXT), }, + { Filter, str(OPENTRACK_LIBRARY_PREFIX "opentrack-filter-*." OPENTRACK_LIBRARY_EXTENSION), }, + { Tracker, str(OPENTRACK_LIBRARY_PREFIX "opentrack-tracker-*." OPENTRACK_LIBRARY_EXTENSION), }, + { Protocol, str(OPENTRACK_LIBRARY_PREFIX "opentrack-proto-*." OPENTRACK_LIBRARY_EXTENSION), }, + { Extension, str(OPENTRACK_LIBRARY_PREFIX "opentrack-ext-*." OPENTRACK_LIBRARY_EXTENSION), }, }; for (const filter_& filter : filters) @@ -127,16 +123,16 @@ struct dylib final return ret; } - Type type; + Type type{Invalid}; QString full_filename; QString module_name; QIcon icon; QString name; - OPENTRACK_CTOR_FUNPTR Dialog; - OPENTRACK_CTOR_FUNPTR Constructor; - OPENTRACK_METADATA_FUNPTR Meta; + module_ctor_t Dialog{nullptr}; + module_ctor_t Constructor{nullptr}; + module_metadata_t Meta{nullptr}; private: QLibrary handle; @@ -150,21 +146,21 @@ private: { in = in.mid(idx + 1); - if (in.startsWith(OPENTRACK_SOLIB_PREFIX) && - in.endsWith("." OPENTRACK_SOLIB_EXT)) + if (in.startsWith(OPENTRACK_LIBRARY_PREFIX) && + in.endsWith("." OPENTRACK_LIBRARY_EXTENSION)) { - constexpr unsigned pfx_len = sizeof(OPENTRACK_SOLIB_PREFIX) - 1; - constexpr unsigned rst_len = sizeof("." OPENTRACK_SOLIB_EXT) - 1; + constexpr unsigned pfx_len = sizeof(OPENTRACK_LIBRARY_PREFIX) - 1; + constexpr unsigned rst_len = sizeof("." OPENTRACK_LIBRARY_EXTENSION) - 1; in = in.mid(pfx_len); in = in.left(in.size() - rst_len); - static constexpr const char* const names[] = + const char* const names[] = { - "opentrack-tracker-", - "opentrack-proto-", - "opentrack-filter-", - "opentrack-ext-", + OPENTRACK_LIBRARY_PREFIX "opentrack-tracker-", + OPENTRACK_LIBRARY_PREFIX "opentrack-proto-", + OPENTRACK_LIBRARY_PREFIX "opentrack-filter-", + OPENTRACK_LIBRARY_PREFIX "opentrack-ext-", }; for (auto name : names) @@ -174,7 +170,7 @@ private: } } } - return QString(); + return {""}; } bool check(bool fail) @@ -213,6 +209,7 @@ struct Modules final dylib_list& trackers() { return tracker_modules; } dylib_list& protocols() { return protocol_modules; } dylib_list& extensions() { return extension_modules; } + private: dylib_list module_list; dylib_list filter_modules; @@ -229,7 +226,7 @@ private: dylib_list filter(dylib::Type t) { QList<std::shared_ptr<dylib>> ret; - for (auto x : module_list) + for (const auto& x : module_list) if (x->type == t) ret.push_back(x); @@ -238,10 +235,10 @@ private: }; template<typename t> -static inline std::shared_ptr<t> make_dylib_instance(const std::shared_ptr<dylib>& lib) +std::shared_ptr<t> make_dylib_instance(const std::shared_ptr<dylib>& lib) { std::shared_ptr<t> ret; if (lib != nullptr && lib->Constructor) - ret = std::shared_ptr<t>(reinterpret_cast<t*>(reinterpret_cast<OPENTRACK_CTOR_FUNPTR>(lib->Constructor)())); + ret = std::shared_ptr<t>(reinterpret_cast<t*>(reinterpret_cast<module_ctor_t>(lib->Constructor)())); return ret; } |