Skip to content

Commit 7951706

Browse files
committed
Added dynamic_library utility
1 parent b52e885 commit 7951706

File tree

5 files changed

+110
-1
lines changed

5 files changed

+110
-1
lines changed

Utilities/dynamic_library.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include "stdafx.h"
2+
#include "dynamic_library.h"
3+
4+
#ifdef _WIN32
5+
#include <Windows.h>
6+
#else
7+
#include <dlfcn.h>
8+
#endif
9+
10+
namespace utils
11+
{
12+
dynamic_library::dynamic_library(const std::string &path)
13+
{
14+
load(path);
15+
}
16+
17+
dynamic_library::~dynamic_library()
18+
{
19+
close();
20+
}
21+
22+
bool dynamic_library::load(const std::string &path)
23+
{
24+
#ifdef _WIN32
25+
m_handle = LoadLibraryA(path.c_str());
26+
#else
27+
m_handle = dlopen(path.c_str(), RTLD_LAZY);
28+
#endif
29+
return loaded();
30+
}
31+
32+
void dynamic_library::close()
33+
{
34+
#ifdef _WIN32
35+
FreeLibrary((HMODULE)m_handle);
36+
#else
37+
dlclose(m_handle);
38+
#endif
39+
m_handle = nullptr;
40+
}
41+
42+
void *dynamic_library::get_impl(const std::string &name) const
43+
{
44+
#ifdef _WIN32
45+
return GetProcAddress((HMODULE)m_handle, name.c_str());
46+
#else
47+
return dlsym(m_handle, (char *)name.c_str());
48+
#endif
49+
}
50+
51+
bool dynamic_library::loaded() const
52+
{
53+
return !m_handle;
54+
}
55+
56+
dynamic_library::operator bool() const
57+
{
58+
return loaded();
59+
}
60+
}

Utilities/dynamic_library.h

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <string>
2+
3+
namespace utils
4+
{
5+
class dynamic_library
6+
{
7+
void *m_handle = nullptr;
8+
9+
public:
10+
dynamic_library() = default;
11+
dynamic_library(const std::string &path);
12+
13+
~dynamic_library();
14+
15+
bool load(const std::string &path);
16+
void close();
17+
18+
private:
19+
void *get_impl(const std::string &name) const;
20+
21+
public:
22+
template<typename Type = void>
23+
Type *get(const std::string &name) const
24+
{
25+
Type *result;
26+
*(void **)(&result) = get_impl(name);
27+
return result;
28+
}
29+
30+
template<typename Type>
31+
bool get(Type *&function, const std::string &name) const
32+
{
33+
*(void **)(&function) = get_impl(name);
34+
35+
return !!function;
36+
}
37+
38+
bool loaded() const;
39+
explicit operator bool() const;
40+
};
41+
}

rpcs3/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ if(WIN32)
198198
target_link_libraries(rpcs3 avformat.lib avcodec.lib avutil.lib swresample.lib swscale.lib png16_static ${wxWidgets_LIBRARIES} ${OPENAL_LIBRARY} ${ADDITIONAL_LIBS})
199199
else()
200200
target_link_libraries(rpcs3 ${wxWidgets_LIBRARIES} ${OPENAL_LIBRARY} ${GLEW_LIBRARY} ${OPENGL_LIBRARIES})
201-
target_link_libraries(rpcs3 libavformat.a libavcodec.a libavutil.a libswresample.a libswscale.a png16_static ${ZLIB_LIBRARIES} ${ADDITIONAL_LIBS})
201+
target_link_libraries(rpcs3 libavformat.a libavcodec.a libavutil.a libswresample.a libswscale.a -ldl png16_static ${ZLIB_LIBRARIES} ${ADDITIONAL_LIBS})
202202
if (NOT APPLE)
203203
target_link_libraries(rpcs3 vulkan glslang OSDependent OGLCompiler SPIRV)
204204
endif()

rpcs3/emucore.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
</ItemDefinitionGroup>
6767
<ItemGroup>
6868
<ClCompile Include="..\Utilities\AutoPause.cpp" />
69+
<ClCompile Include="..\Utilities\dynamic_library.cpp" />
6970
<ClCompile Include="..\Utilities\Log.cpp">
7071
<PrecompiledHeader>NotUsing</PrecompiledHeader>
7172
</ClCompile>
@@ -363,6 +364,7 @@
363364
<ClInclude Include="..\Utilities\BEType.h" />
364365
<ClInclude Include="..\Utilities\BitField.h" />
365366
<ClInclude Include="..\Utilities\BitSet.h" />
367+
<ClInclude Include="..\Utilities\dynamic_library.h" />
366368
<ClInclude Include="..\Utilities\event.h" />
367369
<ClInclude Include="..\Utilities\geometry.h" />
368370
<ClInclude Include="..\Utilities\GSL.h" />

rpcs3/emucore.vcxproj.filters

+6
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,9 @@
854854
<ClCompile Include="Emu\PSP2\Modules\sceVoiceQoS.cpp">
855855
<Filter>Emu\PSP2\Modules</Filter>
856856
</ClCompile>
857+
<ClCompile Include="..\Utilities\dynamic_library.cpp">
858+
<Filter>Utilities</Filter>
859+
</ClCompile>
857860
</ItemGroup>
858861
<ItemGroup>
859862
<ClInclude Include="Crypto\aes.h">
@@ -1624,5 +1627,8 @@
16241627
<ClInclude Include="..\Utilities\sync.h">
16251628
<Filter>Utilities</Filter>
16261629
</ClInclude>
1630+
<ClInclude Include="..\Utilities\dynamic_library.h">
1631+
<Filter>Utilities</Filter>
1632+
</ClInclude>
16271633
</ItemGroup>
16281634
</Project>

0 commit comments

Comments
 (0)