Created
September 19, 2018 13:10
-
-
Save rudolfovich/b91f8f2b06ca688f1fdf6b4035c33b5c to your computer and use it in GitHub Desktop.
(draft) Custom function pointer from dll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <type_traits> | |
template<typename TReturn, typename... TArgs> | |
class _Proc | |
{ | |
protected: | |
typedef TReturn(*ProcType)(TArgs...); | |
public: | |
_Proc(void * dll, ProcType proc) | |
: dll_(dll) | |
, proc_(proc) | |
{} | |
TReturn operator() (TArgs... args) | |
{ | |
std::cout << "ARGS!" << std::endl; | |
if (proc_) | |
{ | |
return proc_(args...); | |
} | |
return TReturn(); | |
} | |
private: | |
ProcType proc_ = nullptr; | |
void * dll_; | |
}; | |
template<typename TReturn, typename... TArgs> | |
class Proc | |
{}; | |
template<typename TReturn, typename... TArgs> | |
class Proc<TReturn(*)(TArgs...)> | |
: public _Proc<TReturn, TArgs...> | |
{ | |
public: | |
Proc(void * dll, _Proc<TReturn, TArgs...>::ProcType proc) | |
: _Proc(dll, proc) | |
{} | |
}; | |
template<typename TReturn, typename... TArgs> | |
class Proc<TReturn(TArgs...)> | |
: public _Proc< TReturn, TArgs...> | |
{ | |
public: | |
Proc(void * dll, _Proc<TReturn, TArgs...>::ProcType proc) | |
: _Proc(dll, proc) | |
{} | |
}; | |
std::string TestFn(const char *str1, const std::string & str2) | |
{ | |
std::cout << "TestFn(" << str1 << ", " << str2 << ")" << std::endl; | |
return "OK!"; | |
} | |
int main() | |
{ | |
typedef int(*ProcX)(int); | |
Proc<ProcX> x(nullptr, nullptr); | |
auto r = x(2); | |
Proc<decltype(TestFn)> test(nullptr, TestFn); | |
auto r2 = test("string #1", "string #2"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment