2016-01-18 4 views
0

У меня есть тип функции, от SpiderMonkey:метод C++ в качестве параметра шаблона

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); 

мне нужно нечто подобное для ограниченного типа методов вместо члены данных (ну, «ограниченный» не «несколько»), что означает какой-то метод вроде:

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

Что такое ошибка? – Marqin

+0

Ошибка \t C2440 \t 'initializing': не может преобразовать из 'перегруженной функции' в 'JSNative'' – mike

+0

Попробуйте передать эти функции-члены по ссылке и получить ее ссылку из std :: mem_fun_ref (например, 'std :: mem_fun_ref (& myClass :: myMethod) ') – Marqin

ответ

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 
... 
} 
Смежные вопросы