2023-06-16 23:06:18 +00:00
|
|
|
// Copyright 2023 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2023-06-20 12:24:24 +00:00
|
|
|
namespace Common {
|
2023-06-16 23:06:18 +00:00
|
|
|
|
|
|
|
class DynamicLibrary {
|
|
|
|
public:
|
2023-06-20 12:24:24 +00:00
|
|
|
explicit DynamicLibrary();
|
2023-06-16 23:06:18 +00:00
|
|
|
explicit DynamicLibrary(std::string_view name, int major = -1, int minor = -1);
|
|
|
|
~DynamicLibrary();
|
|
|
|
|
2023-06-20 12:24:24 +00:00
|
|
|
/// Returns true if the library is loaded, otherwise false.
|
|
|
|
[[nodiscard]] bool IsLoaded() {
|
2023-06-16 23:06:18 +00:00
|
|
|
return handle != nullptr;
|
|
|
|
}
|
|
|
|
|
2023-06-20 12:24:24 +00:00
|
|
|
/// Loads (or replaces) the handle with the specified library file name.
|
|
|
|
/// Returns true if the library was loaded and can be used.
|
|
|
|
[[nodiscard]] bool Load(std::string_view filename);
|
|
|
|
|
|
|
|
/// Returns a string containing the last generated load error, if it occured.
|
|
|
|
[[nodiscard]] std::string_view GetLoadError() const {
|
2023-06-16 23:06:18 +00:00
|
|
|
return load_error;
|
|
|
|
}
|
|
|
|
|
2023-06-20 12:24:24 +00:00
|
|
|
/// Obtains the address of the specified symbol, automatically casting to the correct type.
|
2023-06-16 23:06:18 +00:00
|
|
|
template <typename T>
|
2023-06-20 12:24:24 +00:00
|
|
|
[[nodiscard]] T GetSymbol(std::string_view name) const {
|
2023-06-16 23:06:18 +00:00
|
|
|
return reinterpret_cast<T>(GetRawSymbol(name));
|
|
|
|
}
|
|
|
|
|
2023-06-20 12:24:24 +00:00
|
|
|
/// Returns the specified library name in platform-specific format.
|
|
|
|
/// Major/minor versions will not be included if set to -1.
|
|
|
|
/// If libname already contains the "lib" prefix, it will not be added again.
|
|
|
|
[[nodiscard]] static std::string GetLibraryName(std::string_view name, int major = -1,
|
|
|
|
int minor = -1);
|
2023-06-16 23:06:18 +00:00
|
|
|
|
|
|
|
private:
|
2023-06-20 12:24:24 +00:00
|
|
|
void* GetRawSymbol(std::string_view name) const;
|
2023-06-16 23:06:18 +00:00
|
|
|
|
|
|
|
void* handle;
|
|
|
|
std::string load_error;
|
|
|
|
};
|
|
|
|
|
2023-06-20 12:24:24 +00:00
|
|
|
} // namespace Common
|