使用可变模板包装c++类以便导出到动态语言

Wrapping a C++ class for export to a dynamic language using variadic templates

本文关键字:语言 动态 c++ 包装      更新时间:2023-10-16

我已经在没有可变模板的情况下解决了这个问题,但是我想看看是否有一个使用可变模板的更简洁的解决方案。我正在导出大量的类到谷歌的V8 javascript引擎,并正在构建一个编译时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是Local的)。

我定义了以下函数:
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);

构造和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匹配的args的运行时列表…

希望你明白我在努力做什么。

在过去的几年里,你可能已经弄清楚了这一点,但是为了回答你的问题,

关键问题是:是否有可能键入匹配模板包对象的运行时动态强类型值列表,并调用

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

澄清……没有一些分支,如switch语句或if/else if/else或有条件递归调用(在这方面,终止只是叶子调用)。