c++ - 使用可变参数模板包装 C++ 类以导出到动态语言

标签 c++ templates c++11 variadic-templates template-meta-programming

我已经在没有可变参数模板的情况下解决了这个问题,但想看看是否有使用它们的更简洁的解决方案。我正在将大量类导出到 Google 的 V8 Javascrpt 引擎,并且正在构建一个编译时 DSL 以减轻这项工作的痛苦。

关键问题是:是否可以将模板包与动态强类型值的运行时列表进行类型匹配,并调用适当的 C++ 方法/构造函数。

考虑类 X:

struct X {
    X(int32_t x=42) {cout << "X(" << x << ")" << endl; }
    X(bool x, double y) {cout << "X(" << x << "," << y << ")" << endl; }

    void Do1() { cout << "Do1()" << endl; }
    int Do2(double x, int32_ty=0) { cout << "Do2()" << endl; return 99; }
}

包装器看起来像:

Wrap<X>("X")
    .Constructor<Opt<int32_t>>()
    .Constructor<bool_t,double>()
    .Method<&X::Do1>("Do1")
    .Method<int,&X::Do2,double,Opt<int32_t>>("Do2");

在上面,Opt 是一个结构体,表示参数是可选的。 Opt 的规则与 C++ 函数参数的规则相同。

这将使一个 Javascript 函数符合

function X(x,y) {
    // calls C++ X constructor
    //  new X() if x and y are undefined
    // new X(x) if x is an integer and y is undefined
    // new X(x,y) if x is a boolean and y is a number
    // otherwise throw exception
}

X.prototype.Do1 = function() { 
    // Calls X::Do1() only if no arguments are given

}
X.prototype.Do2 = function(x,y) { 
    // Calls X::Do2(x) if y is undefined
    // Calls X::Do2(x,y) if x and y are defined
    // otherwise throw exception
}

V8的函数调用可以抽象为

struct DynamicArguments {
    DynamicValue operator[](int index);
    int Length();
    void Return<T>(T value);
    void ThrowExcpetion();   // call if arguments do not match any method
};

其中 DynamicValue 是来自 V8 的强类型动态值(如果您知道 V8 的 API,则为本地值)。

我定义了以下函数:

bool Is<T>(DynamicValue value);    // return true if value is mapping to C++ type T
T As<T>(DynmaicValue value);       // returns the mapped value, assuming Is<T>(value) is true

因此有两个函数模板设置运行时数据结构以匹配对静态类方法的动态调用:

template<class C, class... Args>
ClassWrapper& ClassWrapper::Constructor();

template<class C,T(C::*TFun)(), class... Args)
ClassWrapper& Method(const char* dynamicFunctionName);

以及 V8 引擎将调用以将控制权从 Javascript 传递给 C++ 的两个函数

template <class C, class... Args>
void ClassWrapper::Construct(DynamicArguments args);

template <class C, class TRet, T(C::*TFun)(), class... Args>
void ClassWrapper::CallMethod(DynamicArguments args);

Construct 和 CallMethod 执行以下操作:

Foreach defined override
    foreach arg in args, Arg in Args
        if Arg is Opt<T> then Is<T>(value) must be true
        if Arg is T then Is<T>(value> must be true
    if count<Args>() > args.Length()
         all remaining args must be Opt<T>
    if the above is true for this override,
         do new C(As<Args...>(args...) 
        or TRet value = C->method<Args...>(As<Args...>(args...)

其中 args... 是与模板包 Args... 匹配的运行时参数列表

希望我想做的很清楚。

最佳答案

您可能在过去几年已经弄清楚了这一点,但要回答您的问题,

The key questions is: Is it possible to type match a template pack against a runtime list of dynamic strong typed values and call the appropriate c++ method/constructor.

... 没有。当您进入 DynamicValue 领域时,它不再是编译时可测试的,因此不再是强类型的。你不能专注于编译器不知道它实际可能是什么的东西。如果是这样,那么一开始就不是编译时,而是某种形式的 RTTI。

要澄清...并非没有某种分支,例如 switch 语句或 if/else if/else 或条件递归调用(在这方面与终止只是叶调用相同)。

关于c++ - 使用可变参数模板包装 C++ 类以导出到动态语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25220377/

相关文章:

c++ - 如何使用存储在列表中的特定范围的元素初始化 vector ?

c++ - 编译卡在先前的编译C++中

c++ - 测试 std::is_integral 和 std::is_signed?

c++ - C++ 模板函数可以在返回参数上重载吗?

c++ - 如何使用 initializer_ist 在构造函数中初始化动态数组?

c++ - 如何在 Windows 上使用 cmake 构建 google protobuf 环境?

c++ - 函数名称表示什么以及更多

javascript - HTML 模板如何适应后端语言和数据库?

c++ - 为什么我不能在从算术运算符返回时将此类作为引用传递?

c++ - 在不同的命名空间中测试特定的类名(SFINAE?)