Skip to content

Instantly share code, notes, and snippets.

@dpogue
Last active May 6, 2023 10:21
Show Gist options
  • Save dpogue/6ac36a0fdd78397211441825b4ad5405 to your computer and use it in GitHub Desktop.
Save dpogue/6ac36a0fdd78397211441825b4ad5405 to your computer and use it in GitHub Desktop.
/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#ifndef _hsOptionalCall_h
#define _hsOptionalCall_h
#include <memory>
#include <optional>
#include <type_traits>
#include <unordered_map>
#include "HeadSpin.h"
#ifdef HS_BUILD_FOR_WIN32
# include "hsWindows.h"
#else
# include <dlfcn.h>
#endif
namespace hsOptionalCallImpl
{
#ifdef HS_BUILD_FOR_WIN32
using LibraryT = std::remove_pointer<HMODULE>::type;
using LibNameT = const wchar_t*;
#else
typedef void LibraryT;
typedef const char* LibNameT;
#endif
typedef std::unordered_map<LibNameT, std::weak_ptr<LibraryT>> LibraryMapT;
static std::shared_ptr<LibraryT> TryLoadLibrary(LibNameT lib)
{
static LibraryMapT fLoadedLibraries;
LibraryMapT::const_iterator it = fLoadedLibraries.find(lib);
if (it != fLoadedLibraries.end() && !it->second.expired()) {
return std::shared_ptr(it->second);
} else {
#ifdef HS_BUILD_FOR_WIN32
std::shared_ptr<LibraryT> p(LoadLibraryW(lib), [](LibraryT* ptr){
FreeLibrary(ptr);
});
#else
std::shared_ptr<LibraryT> p(dlopen(lib, RTLD_LAZY | RTLD_LOCAL), [](LibraryT* ptr){
dlclose(ptr);
});
#endif
fLoadedLibraries[lib] = std::weak_ptr(p);
return p;
}
}
static void* TryGetFunction(const std::shared_ptr<LibraryT>& lib, const char* func)
{
#ifdef HS_BUILD_FOR_WIN32
return GetProcAddress(lib.get(), func);
#else
return dlsym(lib.get(), func);
#endif
}
};
template<class, class...> class hsOptionalCall;
template<class _ReturnT, class... _ArgsT>
class hsOptionalCall<_ReturnT(_ArgsT...)>
{
using _FuncPtrT = _ReturnT(*)(_ArgsT...);
using _ResultT = typename std::conditional<std::is_void<_ReturnT>::value, std::nullptr_t, _ReturnT>::type;
std::shared_ptr<hsOptionalCallImpl::LibraryT> fLibrary;
_FuncPtrT fProc;
public:
hsOptionalCall(hsOptionalCallImpl::LibNameT lib, const char* func)
: fLibrary(), fProc()
{
fLibrary = hsOptionalCallImpl::TryLoadLibrary(lib);
if (fLibrary) {
fProc = (_FuncPtrT)hsOptionalCallImpl::TryGetFunction(fLibrary, func);
if (!fProc) {
fLibrary = nullptr;
}
}
}
~hsOptionalCall()
{
fLibrary = nullptr;
}
std::optional<_ResultT> operator()(_ArgsT... args) const
{
if (fProc) {
if constexpr (std::is_void<_ReturnT>::value) {
return fProc(std::forward<_ArgsT>(args)...), nullptr;
} else {
return (_ResultT)fProc(std::forward<_ArgsT>(args)...);
}
}
return std::nullopt;
}
operator bool() const
{
return fProc != nullptr;
}
_FuncPtrT Get() const { return fProc; }
};
template<class _ValueT>
class hsOptionalCall<_ValueT>
{
std::shared_ptr<hsOptionalCallImpl::LibraryT> fLibrary;
_ValueT* fValue;
public:
hsOptionalCall(hsOptionalCallImpl::LibNameT lib, const char* func)
: fLibrary(), fValue()
{
fLibrary = hsOptionalCallImpl::TryLoadLibrary(lib);
if (fLibrary) {
fValue = (_ValueT*)hsOptionalCallImpl::TryGetFunction(fLibrary, func);
if (!fValue) {
fLibrary = nullptr;
}
}
}
~hsOptionalCall()
{
fLibrary = nullptr;
}
_ValueT operator *() const {
return *fValue;
}
operator bool() const
{
return fValue != nullptr;
}
_ValueT Get() const { return *fValue; }
};
#define hsOptionalCallDecl(lib, func) \
hsOptionalCall<decltype(func)> __##func(lib, #func);
#endif // _hsOptionalCall_h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment