JavaScript 调用 C++ 函数
QCAD
源码工程模块 src\\scripting\\ecmaapi
重新封装了底层模块 core
、entity
、gui
等的函数接口,供应用层(界面)ECMAJavaScript
调用。
示例:
- 在源码
src\\core\\RDocument
中添加三个public
函数及实现。
void addCustomTables(const QString& group, REntity::Id id);
QSet<REntity::Id> getCustomTableValues(const QString& group);
QSet<QString> getCustomTableKeys();
- 相应的在源码
src\\scripting\\ecmaapi\\generated\\REcmaDocument
中添加对应的三个函数及实现。
.h
static QScriptValue addCustomTables(QScriptContext* context, QScriptEngine* engine);
static QScriptValue getCustomTableKeys(QScriptContext* context, QScriptEngine* engine);
static QScriptValue getCustomTableValues(QScriptContext* context, QScriptEngine* engine);
.cpp
- 注册:
REcmaHelper::registerFunction(&engine, proto, addCustomTables, "addCustomTables");
REcmaHelper::registerFunction(&engine, proto, getCustomTableKeys, "getCustomTableKeys");
REcmaHelper::registerFunction(&engine, proto, getCustomTableValues,"getCustomTableValues");
- 转换调用:
QScriptValue REcmaDocument::getCustomTableKeys(QScriptContext* context, QScriptEngine* engine)
{
//REcmaHelper::functionStart("REcmaDocument::getCustomTableKeys", context, engine);
//qDebug() << "ECMAScript WRAPPER: REcmaDocument::getCustomTableKeys";
//QCoreApplication::processEvents();
QScriptValue result = engine->undefinedValue();
// public function: can be called from ECMA wrapper of ECMA shell:
RDocument* self = getSelf("getCustomTableKeys", context);
//Q_ASSERT(self!=NULL);
if (self==NULL) {
return REcmaHelper::throwError("self is NULL", context);
}
if( context->argumentCount() ==0){
// prepare arguments:
// end of arguments
// call C++ function:
// return type 'QSet < QString >'
QSet <QString> cppResult =self->getCustomTableKeys();
// return type: QSet < QString >
// QSet (convert to QVariantList):
result = REcmaHelper::setToScriptValue(engine, cppResult);
} else
{
return REcmaHelper::throwError("Wrong number/types of arguments for RDocument.getCustomTableKeys().",context);
}
//REcmaHelper::functionEnd("REcmaDocument::getCustomTableKeys", context, engine);
return result;
}
QScriptValue REcmaDocument::getCustomTableValues(QScriptContext* context, QScriptEngine* engine)
{
//REcmaHelper::functionStart("REcmaDocument::getCustomTableValues", context, engine);
//qDebug() << "ECMAScript WRAPPER: REcmaDocument::getCustomTableValues";
//QCoreApplication::processEvents();
QScriptValue result = engine->undefinedValue();
// public function: can be called from ECMA wrapper of ECMA shell:
RDocument* self = getSelf("getCustomTableValues", context);
//Q_ASSERT(self!=NULL);
if (self==NULL) {
return REcmaHelper::throwError("self is NULL", context);
}
if( context->argumentCount() ==1 && (context->argument(0).isString())){
// prepare arguments:
// argument isStandardType
QString a0 =(QString)context->argument(0).toString();
// end of arguments
// call C++ function:
// return type 'QSet<REntity::Id>'
QSet<REntity::Id> cppResult =self->getCustomTableValues(a0);
// return type: QSet < REntity::Id >
// QSet (convert to QVariantList):
result = REcmaHelper::setToScriptValue(engine, cppResult);
} else
{
return REcmaHelper::throwError("Wrong number/types of arguments for RDocument.getCustomTableValues().",context);
}
//REcmaHelper::functionEnd("REcmaDocument::getCustomTableValues", context, engine);
return result;
}
QScriptValue REcmaDocument::addCustomTables(QScriptContext* context, QScriptEngine* engine)
{
//REcmaHelper::functionStart("REcmaDocument::addCustomTables", context, engine);
//qDebug() << "ECMAScript WRAPPER: REcmaDocument::addCustomTables";
//QCoreApplication::processEvents();
QScriptValue result = engine->undefinedValue();
// public function: can be called from ECMA wrapper of ECMA shell:
RDocument* self = getSelf("addCustomTables", context);
//Q_ASSERT(self!=NULL);
if (self==NULL) {
return REcmaHelper::throwError("self is NULL", context);
}
if( context->argumentCount() ==2 && (context->argument(0).isString())
&& (context->argument(1).isNumber())){
// prepare arguments:
// argument isStandardType
QString a0 =(QString)context->argument(0).toString();
REntity::Id a1 =(REntity::Id)(int)context->argument( 1 ).toNumber();
// end of arguments
// call C++ function:
// void
self->addCustomTables(a0,a1);
}else
{
return REcmaHelper::throwError("Wrong number/types of arguments for RDocument.addCustomTables().",context);
}
return result;
}
ECMAJavaScript
调用
var doc=EAction.getDocument();
doc.addCustomTables("扩展",1000);
var ids=doc.getCustomTableValues("扩展");
// ids=[1000]
var keys=doc.getCustomTableKeys();
// keys=[Name,Sex,...]
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容