首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >作为模板参数的C++方法

作为模板参数的C++方法
EN

Stack Overflow用户
提问于 2016-01-19 02:26:28
回答 1查看 151关注 0票数 0

我有一个来自SpiderMonkey的函数类型:

代码语言:javascript
复制
typedef bool (* JSNative)(JSContext* cx, unsigned argc, JS::Value* vp);

我需要构造一个结构数组,其中包含对此类型方法的引用。

我需要使用带参数的函数模板作为对某些类的方法的引用。

我已经设法将类数据成员的指针传递给了这些模板:

代码语言:javascript
复制
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'
...
}

并将其用作:

代码语言:javascript
复制
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对象的数据成员。

要恢复,请执行以下操作:

代码语言:javascript
复制
template <typename pT, typename jspT, pT PrivateType::*Property> static bool GetProperty(JSContext *cx, unsigned argc, JS::Value *vp)

变成:

代码语言:javascript
复制
jsAIDocumentMiPrintRecord::GetProperty<AIRect, jsAIRect, &AIDocumentMiPrintRecord::paperRect>(JSContext *cx, unsigned argc, JS::Value *vp)

与以下各项匹配:

代码语言:javascript
复制
typedef bool (* JSNative)(JSContext* cx, unsigned argc, JS::Value* vp);

对于有限类型的方法,我需要一个类似的东西,而不是数据成员(嗯,“有限的”不是“几个”),意思是一些方法,比如:

代码语言:javascript
复制
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
...
}

..。但这似乎不是像这样匹配的:

代码语言:javascript
复制
typedef bool (* JSNative)(JSContext* cx, unsigned argc, JS::Value* vp);

用作:

代码语言:javascript
复制
const JSFunctionSpec jsAIDocumentSuite::fFunctions[] = {
JS_FN("GetDocumentFileSpecification", (jsAIDocumentSuite::GetMethod<jsAIFilePath, ai::FilePath, &AIDocumentSuite::GetDocumentFileSpecification>), 1, 0),
...
    JS_FS_END
};

其中我们有:

代码语言:javascript
复制
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 );
...
};

解决方案是什么?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-01-19 06:24:13

所以,解决方案是...

..。模板函数和方法调用如下:

代码语言:javascript
复制
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
...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34861573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档