有没有办法在容器中存储不同的函数指针类型

时间:2013-06-02 07:04:33

标签: c++ c++11

在下面的代码中,我试图将函数指针存储在向量中,但每个函数指针都有不同的签名。

以下代码的想法是,当我加载DLL时,我只是使用循环将DLL中的所有函数加载到向量中。然后,当我想调用一个函数时,我只是通过名称或索引调用它并传递可变数量的参数。但我真的想以与加载它们相同的方式调用函数:通过循环。

#if defined _WIN32 || defined _WIN64
#include <windows.h>
#else
#include <dlfcn.h>
#endif

#include <iostream>
#include <cstdint>
#include <vector>
#include <functional>

class Library
{
    private:
        void* Module;

    public:
        Library(std::string Library);
        ~Library();

        template<typename T>
        T AddressOf(std::string FunctionName);

        template<typename T>
        bool AddressOf(T &FunctionDefinition, std::string FunctionName);

        template<typename T, typename ...Args>
        auto CallFunction(void* Function, Args... args) -> decltype(reinterpret_cast<T>(Function)(args...));
};

Library::Library(std::string Library)
{
    #if defined _WIN32 || defined _WIN64
    this->Module = LoadLibrary(Library.c_str());
    #else
    this->Module = dlopen(Library.c_str(), RTLD_LAZY);
    #endif
}

Library::~Library()
{
    #if defined _WIN32 || defined _WIN64
    FreeLibrary(static_cast<HMODULE>(this->Module));
    #else
    dlclose(this->Module);
    #endif
}

template<typename T>
T Library::AddressOf(std::string FunctionName)
{
    #if defined _WIN32 || defined _WIN64
    return reinterpret_cast<T>(GetProcAddress(static_cast<HMODULE>(this->Module), FunctionName.c_str()));
    #else
    return reinterpret_cast<T>(dlsym(this->Module, FunctionName.c_str()));
    #endif
}

template<typename T>
bool Library::AddressOf(T &FunctionDefinition, std::string FunctionName)
{
    return (FunctionDefinition = this->AddressOf<T>(FunctionName));
}

template<typename T, typename ...Args>
auto Library::CallFunction(void* Function, Args... args) -> decltype(reinterpret_cast<T>(Function)(args...))
{
    return reinterpret_cast<T>(Function)(args...);
}





std::vector<void*> Functions;

typedef void (*Message)(const LPCSTR sometext);
typedef void (*MessageEx)(const LPCSTR sometext, const LPCSTR title);
typedef int (*Add)(int X, int Y);
typedef int (*Subtract)(int X, int Y);

int main()
{
    Library L("TestDll.dll");
    std::array<std::string, 4> List = {"Message", "MessageEx", "Add", "Sub"};

    /** Get Function Addresses.. **/
    for (std::size_t I = 0; I < List.size(); ++I)
    {
        Functions.push_back(L.AddressOf<void*>(List[I]));
    }

    /** Call Functions.. **/
    L.CallFunction<Message>(Functions[0], "Hello World!");
    L.CallFunction<MessageEx>(Functions[1], "Hello World!", "DLL MessageBox Title");

    std::cout<<L.CallFunction<Add>(Functions[2], 5, 7)<<"\n";
    std::cout<<L.CallFunction<Subtract>(Functions[3], 7, 5)<<"\n";

    return 0;
}

有没有更好的方法来重写我的CallFunction类成员或以某种方式保留函数签名的方法或将函数的名称映射到其签名?我不介意写出typedef但是我讨厌将它们作为模板参数放置,以便它可以转换为它,所以如果我能以某种方式使向量存储不同的函数签名将是最好的,但欢迎任何解决方案。

编辑:为了清楚起见,我想以某种方式找出给出其参数的函数签名。例如:

//If I do:

CallFunction<int>(FuncPtr, "Subtract", 10, 5);

//It would do:


template<typename T, typename ...Args>
auto Library::CallFunction(void* Function, Args... args) -> decltype(reinterpret_cast<T>(Function)(args...))
{
    return  //using args, figure out the function signature, call it, and return int.
}

1 个答案:

答案 0 :(得分:1)

对我而言,这似乎颇为做作。对于正确的名称,您需要使用正确的参数在正确的位置使用正确的函数。如果你声明一个函数指针typedef,并使用一行reinterpret_cast来分配函数,你需要一个很好的40多个函数才能完成你在这里提供的代码 - 这是一个简单的步骤,重复类型代码,因此易于遵循和易于维护。没有模板,没有变量arguemnts等。

显然,您仍然必须生成一个从名称返回void *的函数。但那就是你现在减去reinterpret_cast的东西。

相关问题