From 8eb134dbbd44002f3acd90d91550875f161136fa Mon Sep 17 00:00:00 2001 From: Neil Date: Fri, 13 Dec 2019 12:09:32 +1100 Subject: Backport: Implement DynamicLibrary on Cocoa. Backport of changeset 7865:a5c2f8a3f171. --- cocoa/PlatCocoa.mm | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'cocoa') diff --git a/cocoa/PlatCocoa.mm b/cocoa/PlatCocoa.mm index 52e5399a9..8f49d5eed 100644 --- a/cocoa/PlatCocoa.mm +++ b/cocoa/PlatCocoa.mm @@ -24,6 +24,7 @@ #include #include +#include #include #import @@ -2096,6 +2097,43 @@ void Platform::Assert(const char *c, const char *file, int line) //----------------- DynamicLibrary ----------------------------------------------------------------- +/** + * Platform-specific module loading and access. + * Uses POSIX calls dlopen, dlsym, dlclose. + */ + +class DynamicLibraryImpl : public DynamicLibrary { +protected: + void *m; +public: + explicit DynamicLibraryImpl(const char *modulePath) noexcept { + m = dlopen(modulePath, RTLD_LAZY); + } + // Deleted so DynamicLibraryImpl objects can not be copied. + DynamicLibraryImpl(const DynamicLibraryImpl&) = delete; + DynamicLibraryImpl(DynamicLibraryImpl&&) = delete; + DynamicLibraryImpl&operator=(const DynamicLibraryImpl&) = delete; + DynamicLibraryImpl&operator=(DynamicLibraryImpl&&) = delete; + + ~DynamicLibraryImpl() override { + if (m) + dlclose(m); + } + + // Use dlsym to get a pointer to the relevant function. + Function FindFunction(const char *name) override { + if (m) { + return dlsym(m, name); + } else { + return nullptr; + } + } + + bool IsValid() override { + return m != nullptr; + } +}; + /** * Implements the platform specific part of library loading. * @@ -2104,8 +2142,7 @@ void Platform::Assert(const char *c, const char *file, int line) */ DynamicLibrary* DynamicLibrary::Load(const char* /* modulePath */) { - // Not implemented. - return nullptr; + return static_cast(new DynamicLibraryImpl(modulePath)); } //-------------------------------------------------------------------------------------------------- -- cgit v1.2.3