使用可变模板包装c++类以便导出到动态语言
Wrapping a C++ class for export to a dynamic language using variadic templates
我已经在没有可变模板的情况下解决了这个问题,但是我想看看是否有一个使用可变模板的更简洁的解决方案。我正在导出大量的类到谷歌的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
或有条件递归调用(在这方面,终止只是叶子调用)。
相关文章:
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- C++中的动态铸造故障
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 内联映射初始化的动态atexit析构函数崩溃
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 控制允许动态运行c++的并发操作数
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 今天的主流编程语言主要使用动态还是静态(词汇)作用域?
- 使用 LLVM IR 实现动态类型语言
- 拥有用于动态机会/概率的简单负载均衡器(在C++中,但语言无关)
- 使用动态语言的QT应用程序(QTranslator)不起作用
- 动态C++与其他语言的比较 - 在不知道类型的情况下无法在对象上调用方法
- 如何为动态类型语言构建编译器
- 如何使用C,C++,Java等语言动态生成HTML
- 为什么我不能进行动态演员表?C 语言中的策略模式
- 一种语言可以有编译时检查但具有动态类型的特性吗
- 使用可变模板包装c++类以便导出到动态语言
- 用C语言调用动态Cpp库
- 为什么有些语言更喜欢静态方法绑定而不是动态方法绑定