我有一个函数类型,来自SpiderMonkey:
typedef bool (* JSNative)(JSContext* cx, unsigned argc, JS::Value* vp);
我需要构造一个包含对此类方法的引用的结构数组。
我需要使用带参数的函数模板来创建这些参数作为某些类的方法的引用。
我设法将类数据成员指针传递给这些模板:
template<typename PrivateType> class jsObjectDataClass : public jsBaseDataClass<PrivateType>
{
public:
template <typename pT, typename jspT, pT PrivateType::*Property> static bool GetProperty(JSContext *cx, unsigned argc, JS::Value *vp)
{
...
PrivateType* data = ...; // initialize here an object having the Property data member
...
data->*Property; // use here the Property data member of an object called 'data'
...
}
并将其用作:
const JSPropertySpec jsAIDocumentMiPrintRecord::fProperties[] = {
JS_PSGS("paperRect", (jsAIDocumentMiPrintRecord::GetProperty<AIRect, jsAIRect, &AIDocumentMiPrintRecord::paperRect>), (jsAIDocumentMiPrintRecord::SetProperty<AIRect, jsAIRect, &AIDocumentMiPrintRecord::paperRect>), JSPROP_PERMANENT | JSPROP_ENUMERATE),
...
JS_PS_END
};
它可以传递和获取对象的数据成员。
要恢复,请执行以下操作:
template <typename pT, typename jspT, pT PrivateType::*Property> static bool GetProperty(JSContext *cx, unsigned argc, JS::Value *vp)
变为:
jsAIDocumentMiPrintRecord::GetProperty<AIRect, jsAIRect, &AIDocumentMiPrintRecord::paperRect>(JSContext *cx, unsigned argc, JS::Value *vp)
与:
匹配typedef bool (* JSNative)(JSContext* cx, unsigned argc, JS::Value* vp);
对于有限类型的方法而不是数据成员,我需要类似的东西(好吧,&#34;有限&#34;不是&#34;少数&#34;),这意味着某些方法如下:
template <typename jsType, typename PrivateType, AIErr (SuiteType::*SuiteGetMethod)(PrivateType&)> static bool GetMethod(JSContext *cx, unsigned argc, JS::Value *vp)
{
...
SuiteType* fSuite = ...; init the object that contains the method to be called
AIErr aiErr = kNoErr;
PrivateType pt;
if (fSuite)
aiErr = fSuite->*SuiteGetMethod(pt); // call here the method of specific type
...
}
...但似乎这与以下内容不匹配:
typedef bool (* JSNative)(JSContext* cx, unsigned argc, JS::Value* vp);
用作:
const JSFunctionSpec jsAIDocumentSuite::fFunctions[] = {
JS_FN("GetDocumentFileSpecification", (jsAIDocumentSuite::GetMethod<jsAIFilePath, ai::FilePath, &AIDocumentSuite::GetDocumentFileSpecification>), 1, 0),
...
JS_FS_END
};
我们有:
struct AIDocumentSuite {
/** Retrieves the file specification for the current document.
@param file [out] A buffer in which to return the file specification.
*/
AIAPI AIErr (*GetDocumentFileSpecification) ( ai::FilePath &file );
...
};
解决方案是什么?
感谢。
答案 0 :(得分:0)
所以,解决方案是......
...模板函数和对方法的调用是:
template <typename jsType, typename PrivateType, AIErr(*SuiteType::*SuiteGetMethod)(PrivateType&)> static bool GetMethod(JSContext *cx, unsigned argc, JS::Value *vp)
{
...
SuiteType* fSuite = ...; init the object that contains the method to be called
AIErr aiErr = kNoErr;
PrivateType pt;
if (fSuite)
aiErr = (fSuite->*SuiteGetMethod)(pt); // call here the method of specific type
...
}