From 795170635f53a11fb559ccbbdf12a31616a06d36 Mon Sep 17 00:00:00 2001 From: DHrpcs3 Date: Sun, 8 May 2016 10:38:40 +0300 Subject: [PATCH] Added dynamic_library utility --- Utilities/dynamic_library.cpp | 60 +++++++++++++++++++++++++++++++++++ Utilities/dynamic_library.h | 41 ++++++++++++++++++++++++ rpcs3/CMakeLists.txt | 2 +- rpcs3/emucore.vcxproj | 2 ++ rpcs3/emucore.vcxproj.filters | 6 ++++ 5 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 Utilities/dynamic_library.cpp create mode 100644 Utilities/dynamic_library.h diff --git a/Utilities/dynamic_library.cpp b/Utilities/dynamic_library.cpp new file mode 100644 index 0000000000..572edc42ea --- /dev/null +++ b/Utilities/dynamic_library.cpp @@ -0,0 +1,60 @@ +#include "stdafx.h" +#include "dynamic_library.h" + +#ifdef _WIN32 + #include +#else + #include +#endif + +namespace utils +{ + dynamic_library::dynamic_library(const std::string &path) + { + load(path); + } + + dynamic_library::~dynamic_library() + { + close(); + } + + bool dynamic_library::load(const std::string &path) + { +#ifdef _WIN32 + m_handle = LoadLibraryA(path.c_str()); +#else + m_handle = dlopen(path.c_str(), RTLD_LAZY); +#endif + return loaded(); + } + + void dynamic_library::close() + { +#ifdef _WIN32 + FreeLibrary((HMODULE)m_handle); +#else + dlclose(m_handle); +#endif + m_handle = nullptr; + } + + void *dynamic_library::get_impl(const std::string &name) const + { +#ifdef _WIN32 + return GetProcAddress((HMODULE)m_handle, name.c_str()); +#else + return dlsym(m_handle, (char *)name.c_str()); +#endif + } + + bool dynamic_library::loaded() const + { + return !m_handle; + } + + dynamic_library::operator bool() const + { + return loaded(); + } +} diff --git a/Utilities/dynamic_library.h b/Utilities/dynamic_library.h new file mode 100644 index 0000000000..d5578d08e0 --- /dev/null +++ b/Utilities/dynamic_library.h @@ -0,0 +1,41 @@ +#include + +namespace utils +{ + class dynamic_library + { + void *m_handle = nullptr; + + public: + dynamic_library() = default; + dynamic_library(const std::string &path); + + ~dynamic_library(); + + bool load(const std::string &path); + void close(); + + private: + void *get_impl(const std::string &name) const; + + public: + template + Type *get(const std::string &name) const + { + Type *result; + *(void **)(&result) = get_impl(name); + return result; + } + + template + bool get(Type *&function, const std::string &name) const + { + *(void **)(&function) = get_impl(name); + + return !!function; + } + + bool loaded() const; + explicit operator bool() const; + }; +} diff --git a/rpcs3/CMakeLists.txt b/rpcs3/CMakeLists.txt index d4416d6c75..4b4cd91f49 100644 --- a/rpcs3/CMakeLists.txt +++ b/rpcs3/CMakeLists.txt @@ -198,7 +198,7 @@ if(WIN32) target_link_libraries(rpcs3 avformat.lib avcodec.lib avutil.lib swresample.lib swscale.lib png16_static ${wxWidgets_LIBRARIES} ${OPENAL_LIBRARY} ${ADDITIONAL_LIBS}) else() target_link_libraries(rpcs3 ${wxWidgets_LIBRARIES} ${OPENAL_LIBRARY} ${GLEW_LIBRARY} ${OPENGL_LIBRARIES}) - target_link_libraries(rpcs3 libavformat.a libavcodec.a libavutil.a libswresample.a libswscale.a png16_static ${ZLIB_LIBRARIES} ${ADDITIONAL_LIBS}) + target_link_libraries(rpcs3 libavformat.a libavcodec.a libavutil.a libswresample.a libswscale.a -ldl png16_static ${ZLIB_LIBRARIES} ${ADDITIONAL_LIBS}) if (NOT APPLE) target_link_libraries(rpcs3 vulkan glslang OSDependent OGLCompiler SPIRV) endif() diff --git a/rpcs3/emucore.vcxproj b/rpcs3/emucore.vcxproj index 886db41ffd..7ebe0f69b7 100644 --- a/rpcs3/emucore.vcxproj +++ b/rpcs3/emucore.vcxproj @@ -66,6 +66,7 @@ + NotUsing @@ -363,6 +364,7 @@ + diff --git a/rpcs3/emucore.vcxproj.filters b/rpcs3/emucore.vcxproj.filters index ca8e48a36b..5b334d4510 100644 --- a/rpcs3/emucore.vcxproj.filters +++ b/rpcs3/emucore.vcxproj.filters @@ -854,6 +854,9 @@ Emu\PSP2\Modules + + Utilities + @@ -1624,5 +1627,8 @@ Utilities + + Utilities + \ No newline at end of file