我有一个来自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
};它用于传递和获取set对象的数据成员。
要恢复,请执行以下操作:
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);对于有限类型的方法,我需要一个类似的东西,而不是数据成员(嗯,“有限的”不是“几个”),意思是一些方法,比如:
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 );
...
};解决方案是什么?
谢谢。
发布于 2016-01-19 06:24:13
所以,解决方案是...
..。模板函数和方法调用如下:
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
...
}https://stackoverflow.com/questions/34861573
复制相似问题