Skip to content

Commit

Permalink
Fix Linux build
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed Dec 7, 2023
1 parent dec865c commit e8523d0
Show file tree
Hide file tree
Showing 3 changed files with 404 additions and 0 deletions.
96 changes: 96 additions & 0 deletions Sun/vendor/Soup/soup/SharedLibrary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "SharedLibrary.hpp"

#include "Exception.hpp"

namespace soup
{
SharedLibrary::SharedLibrary(const std::string& path)
{
load(path);
}

SharedLibrary::SharedLibrary(const char* path)
{
load(path);
}

SharedLibrary::SharedLibrary(SharedLibrary&& b)
: handle(b.handle)
{
b.forget();
}

SharedLibrary::~SharedLibrary()
{
unload();
}

void SharedLibrary::operator=(SharedLibrary&& b)
{
unload();
handle = b.handle;
b.forget();
}

bool SharedLibrary::isLoaded() const noexcept
{
return handle != nullptr;
}

bool SharedLibrary::load(const std::string& path)
{
return load(path.c_str());
}

bool SharedLibrary::load(const char* path)
{
#if SOUP_WINDOWS
handle = LoadLibraryA(path);
#else
handle = dlopen(path, RTLD_LAZY);
#endif
return isLoaded();
}

void SharedLibrary::unload()
{
if (isLoaded())
{
#if SOUP_WINDOWS
FreeLibrary(handle);
#else
dlclose(handle);
#endif
forget();
}
}

void SharedLibrary::forget()
{
handle = nullptr;
}

void* SharedLibrary::getAddress(const char* name) const noexcept
{
SOUP_IF_UNLIKELY (!isLoaded())
{
return nullptr;
}
#if SOUP_WINDOWS
return (void*)GetProcAddress(handle, name);
#else
return dlsym(handle, name);
#endif
}

void* SharedLibrary::getAddressMandatory(const char* name) const
{
SOUP_IF_LIKELY (auto addr = getAddress(name))
{
return addr;
}
std::string msg = "Failed to find mandatory symbol: ";
msg.append(name);
SOUP_THROW(Exception(std::move(msg)));
}
}
42 changes: 42 additions & 0 deletions Sun/vendor/Soup/soup/SharedLibrary.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include "base.hpp"

#if SOUP_WINDOWS
#include <Windows.h>
#else
#include <dlfcn.h>
#endif

#include <string>

namespace soup
{
struct SharedLibrary
{
#if SOUP_WINDOWS
using handle_t = HMODULE;
#else
using handle_t = void*;
#endif

handle_t handle = nullptr;

explicit SharedLibrary() = default;
explicit SharedLibrary(const std::string& path);
explicit SharedLibrary(const char* path);
explicit SharedLibrary(SharedLibrary&& b);
~SharedLibrary();

void operator=(SharedLibrary&& b);

[[nodiscard]] bool isLoaded() const noexcept;
bool load(const std::string& path);
bool load(const char* path);
void unload();
void forget();

[[nodiscard]] void* getAddress(const char* name) const noexcept;
[[nodiscard]] void* getAddressMandatory(const char* name) const;
};
}
Loading

0 comments on commit e8523d0

Please sign in to comment.