c++ - 根据某些元数据在运行时转换参数包值

标签 c++ templates metaprogramming

我整理了一个类来处理共享库的跨平台加载(Windows 上的 LoadLibrary/GetProcAddress 和 Linux 上的 dlopen/dlsym)。 Call 函数看起来像这样 -

template <typename ReturnT, typename... Args>
ReturnT Call(const std::string& name, Args... args)
{
        void* functionAddress = GetFunctionAddress(name);

        typedef ReturnT(CALLING_CONVENTION * variadicFunctionPointer)(Args...);

        auto functionPointer = reinterpret_cast<variadicFunctionPointer>(functionAddress);
        const auto returnValue = (*functionPointer)(std::forward<Args>(args)...);

        return returnValue;
}

用法如下-

 Library someLib("someLib.dll");

 auto ret = someLib.Call<bool>("SomeCall", "StringParam", 1, true, nullptr, nullptr);   

这很好用,但我遇到的问题是尝试在运行时动态转换每个参数。我想获取一些 json,加载相关的库,转换每个参数(我们从 json 中知道参数类型),然后进行调用。

 auto result = someLib.Call<bool>(function,
        std::stoi(params.at(0)["value"].get<std::string>()),
        reinterpret_cast<const uint8_t*>(params.at(1)["value"].get<std::string>().c_str()),
        std::stoi(params.at(2)["value"].get<std::string>()),
        digest.data(),
        &bufferSize);

我试过在每个参数包 arg 上使用转换函数,但这行不通,因为它需要不同的返回类型。

这可以使用 C++14 吗?

最佳答案

使用 std::variant (C++17),你可能会这样做:

template <typename Ret>
struct Caller
{
    Caller(const std::string& name) : name(name) {}

    template <typename ... Ts>
    Ret operator ()(Ts... args) const
    {
        void* functionAddress = GetFunctionAddress(name);

        using Func =  Ret(Ts...);

        auto functionPointer = reinterpret_cast<Func*>(functionAddress);
        return (*functionPointer)(std::forward<Ts>(args)...);
    }

    std::string name;
};

然后:

// Variant constructed from json
std::variant<int, std::string> s{"Hello"};
std::variant<int, std::string> a{40};
std::variant<int, std::string> b{2};

std::visit(Caller<void>("print"), s);
std::cout << std::visit(Caller<int>("add"), a, b) << std::endl;

Demo

关于c++ - 根据某些元数据在运行时转换参数包值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54200530/

相关文章:

C++ 自定义单例

在 Raku 中测试私有(private)方法

macros - lisp 从字符串创建属性列表

c++ - 巨大的斐波那契模 m C++

c++ - curl 字符串追加问题,curl_formadd C++ 读取带有额外字符的变量但直接工作正常 CURLFORM_COPYNAME, "key"[?]

c++ - 设计模式(GoF patterns)在c++中的实现

C++:映射结构中的结构指针

angularjs - 使用 Angular 模块和 Node 模板以获得更好的重用能力

c++ - 定义常量数组以用作模板参数

ruby - Module#const_set 和 Module#module_eval 之间的区别