我花了很長的時間,但我終於想通了這一點。這可能是更簡單的方法,但是我發現我需要在.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模型文件:
// construct theEvent (your own struct) and fire the signal with your event structure that gets passed to CPPToCocoaModelMessageCallback::CallBackMessage()
MyEventKind theEvent(someUsefulParams);
fSignalObservers(&theEvent);