这对你们中的一些人来说一定是显而易见的,但我找不到这样的例子:
我需要一个boost :: signals2信号来连接一个C ++类成员函数或函子的插槽回调,所以我可以将模型回调转换为Objective-C / C ++ Controller代码。
该回调需要存储可在C ++回调函数内调用的Objective-C / C ++方法实例的Class和Selector。 (我假设没有可能的方法来实际提供Objective-C / C ++方法的直接回调函数地址)。我假设我需要创建一个C ++类/仿函数的实例来包含调用Objective-C / C ++方法的信息。
我也不确定是否可以将Class和SEL(选择器)分开并将它们存储在C ++类的实例中以进行回调,而不将它们作为void *传递。一旦signal()调用C ++回调,我希望我可以使用class_getInstanceMethod和method_getImplementation将它们转换为可用(可调用)形式。
此外,我可能希望将至少一个带有任意结构(“EventInfo”)的参数发送到信号的插槽中,这可以提供有关信号性质的信息。
任何人都可以在黑暗中发光吗?
答案 0 :(得分:1)
我花了很长时间,但我终于想通了。这可能是更简单的方法,但我发现我需要在.mm文件中创建一个C ++类,它充当boost :: signals2信号和Objective-C回调函数之间的桥梁:
在CPPToCocoaModelMessageCallbacks.h中:
/* ------------------------------------------------------------------------
class CPPToCocoaModelMessageCallback -
--------------------------------------------------------------------------- */
class CPPToCocoaModelMessageCallback
{
public:
CPPToCocoaModelMessageCallback( PMD_Signal_Messenger<PrefEvent> *theSignalClass,
int whichPrefIdxToObserve,
id pObjCClass,
SEL pObjCMethod);
~CPPToCocoaModelMessageCallback();
void CallBackMessage(PrefEvent* pPrefEvent);
private:
id fpObjCClass;
SEL fpObjCMethod;
ls_index fWhichPrefIdxToObserve;
boost::signals2::connection fTheConnection;
}; // CPPToCocoaModelMessageCallback
在CPPToCocoaModelMessageCallbacks.mm
中/* ------------------------------------------------------------------------
CPPToCocoaModelMessageCallback - CONSTRUCTOR
whichPrefIdxToObserve - the preference idx to observe
Pass the id and selector of the Objective-C/C++ object & method to be
called.
--------------------------------------------------------------------------- */
CPPToCocoaModelMessageCallback::CPPToCocoaModelMessageCallback(PMD_Signal_Messenger<PrefEvent> *theSignalClass, int whichPrefIdxToObserve, id pObjCClass, SEL pObjCMethod)
: fpObjCClass (pObjCClass),
fpObjCMethod (pObjCMethod),
fWhichPrefIdxToObserve (whichPrefIdxToObserve)
{
fTheConnection = theSignalClass->ObserveSignal(&CPPToCocoaModelMessageCallback::CallBackMessage, this);
}
/* ------------------------------------------------------------------------
~CPPToCocoaModelMessageCallback - DESTRUCTOR
Pass the id and selector of the Objective-C/C++ object & method to be
called.
--------------------------------------------------------------------------- */
CPPToCocoaModelMessageCallback::~CPPToCocoaModelMessageCallback()
{
fTheConnection.disconnect();
}
/* ------------------------------------------------------------------------
CPPToCocoaModelMessageCallback::CallBackMessage -
Handles single and range-type preference change events.
--------------------------------------------------------------------------- */
void CPPToCocoaModelMessageCallback::CallBackMessage(PrefEvent* pPrefEvent)
{
// Only make the callback if this event is the preference we're observing
if (pPrefEvent->GetChangedPrefIdx() == fWhichPrefIdxToObserve) {
[fpObjCClass performSelector:fpObjCMethod];
}
}
/////////////////////////////////////////////// ////////////////////////////////
在你的controller.mm中:
// set up messaging from Model. The message callback functions must be destructed in dealloc.
// I've done this in awakeFromNib but it could be elsewhere
- (void)awakeFromNib {
PMD_Signal_Messenger<MyEventKind>* theModelClass = GetMyModelClassPointer();
displayMenuPrefChangedCallBack = new CPPToCocoaModelMessageCallback(theModelClass, kAppPrefDictionaryDisplayShortDef, self, @selector(displayMenuChanged));
}
/* ------------------------------------------------------------------------
displayMenuChanged - this gets called when the model fires a signal
(via CPPToCocoaModelMessageCallback::CallBackMessage())
--------------------------------------------------------------------------- */
- (void) displayMenuChanged
{
NSLog(@"displayMenuChanged\n");
// DO SOMETHING TO RESPOND TO THE SIGNAL (in this case I'm reloading an NSWebView):
[self reloadWebViewText];
}
/////////////////////////////////////////////// ///////////////////////////////
与信号观察员的模型类结合的类:
PMD_Signal_Messenger.h:
/* ------------------------------------------------------------------------
class PMD_Signal_Messenger<MyEventKind> -
This class is designed to be multiple inherited with various
Model classes.
--------------------------------------------------------------------------- */
template <class MyEventKind>
class PMD_Signal_Messenger {
public:
PMD_Signal_Messenger() { }
~PMD_Signal_Messenger() { }
template<typename Fn, typename Obj>
boost::signals2::connection ObserveSignal(Fn callback, Obj &object) {
return fSignalObservers.connect(boost::bind(callback, object, _1));
}
protected:
boost::signals2::signal<void (MyEventKind*)> fSignalObservers; // all observers of my preference changes
private:
PMD_Signal_Messenger(const PMD_Signal_Messenger& thePMD_Signal_Messenger) { assert(false); } // prevent copy constructor
};
在.cpp MODEL文件中,您要发出模型更改信号:
// construct theEvent (your own struct) and fire the signal with your event structure that gets passed to CPPToCocoaModelMessageCallback::CallBackMessage()
MyEventKind theEvent(someUsefulParams);
fSignalObservers(&theEvent);
答案 1 :(得分:1)
您可以使用此解决方案: https://github.com/godexsoft/objc_callback
#pragma once
#ifndef _OBJC_CALLBACK_H_
#define _OBJC_CALLBACK_H_
template<typename Signature> class objc_callback;
template<typename R, typename... Ts>
class objc_callback<R(Ts...)>
{
public:
typedef R (*func)(id, SEL, Ts...);
objc_callback(SEL sel, id obj)
: sel_(sel)
, obj_(obj)
, fun_((func)[obj methodForSelector:sel])
{
}
inline R operator ()(Ts... vs)
{
return fun_(obj_, sel_, vs...);
}
private:
SEL sel_;
id obj_;
func fun_;
};
#endif // _OBJC_CALLBACK_H_