View Full Version : can you make a c++ class listen to nsnotification?

Jun 3, 2009, 08:54 PM
the current code i'm dealing was written in half objective c and half c++

i have a c++ class here labeled as a .mm file

is it possible to make it listen out to notifications? i tired the standard way of adding the notification observer but it doesn't accept the class as an obj c class (obviously.. seeing as how it's not one)
is there a way to make it listen out for a notification?

Jun 3, 2009, 09:07 PM
This is probably an unfortunate hack... but you could make a very light-weight Objective-C wrapper class that's either single-use for this class, which would handle the notification and call the desired method on the C++ class... or a more general purpose wrapper, calling some sort of "handle" method on your C++ class... you'd probably want to make a base C++ class that has a virtual handleNotification method or something similar, and have the ivar of the Obj-C class be of this type (vs. having an ivar that's a single, concrete C++ class in the first case).


Jun 3, 2009, 09:15 PM
hmm... making an objective c wrapper for it...

heh... i guess it's the only way.... now this class with have a wrapper that has another wrapper.... :rolleyes:

i'm going to kick the original programmer in the nuts if i ever see him again...:mad:

Jun 3, 2009, 09:27 PM
If the existing class isn't doing anything uniquely C++-ish, you could certainly re-write it completely as an Objective-C class. I'm guessing that it's probably not stand-alone, and part of an inheritance tree you doing want to re-implement completely, but if this is an option you may want to pursue it.

If you think this might come up again, the more generic wrapper might be best, so you aren't writing dozens of one-off wrappers.


P.S. Be kind to the nuts of your forbearers, someone will come after you and probably have something to complain about, too. I'm confident that whoever came before you did some things that seem positively daft, but hopefully they didn't do so maliciously.

Jun 3, 2009, 09:35 PM
i'm not entirely sure how you write a generic wrapper... i thought they were always specific

Jun 3, 2009, 09:53 PM
The pattern i would try to use, if it's just for notification's sake, is to write an abstract C++ class that defines an abstract respondToNotification. If you need to know the type of notification that gets a little trickier, but it doesn't seem too far fetched to make a C++ class or struct that's just notificationType, and it has an enum that defines the type, etc.

So anyhow, you have an abstract class notificationRecipient that defines a respondToNotification method that takes a notificationType argument... then any C++ class that needs to accept an NSNotification inherits from notificationRecipient, and implements respondToNotification... and does whatever it needs to do in there, likely it's just a "hub" that calls other methods in the class based on the type of notification received.

Then in your Objective-C wrapper class you have an ivar that is a notificationRecipient *, etc... you initialize it by passing the pointer to any C++ object whose class inherits from notificationRecipient... and when this class gets an NSNotification, it maps the type to a C++ notificationType, and invokes respondToNotification on its notificationRecipient * ivar.

It's a little complicated, but it would allow you to re-use the wrapper with any C++ object that you want to send an NSNotification to.

Note my C++ is rusty. I think i called things the right thing for C++, but forgive any misuses of terms. I think the logic is sound, at least, and works for C++ b/c of multiple inheritance (so anything can inherit from notificationRecipient).


Jun 3, 2009, 10:03 PM
thanks lee, i will investigate that option, sounds tricky but doable

P.S. Be kind to the nuts of your forbearers, someone will come after you and probably have something to complain about, too. I'm confident that whoever came before you did some things that seem positively daft, but hopefully they didn't do so maliciously.

ah, yeah, someone who takes over from me in the new code will also be like 'wtf is this retard doing??'
although i do wonder why someone would make every class a friend of every other class that's entirely in c++
the guy before him was a developer from apple and where he's been the code is actually good and efficient...
of course, when the bosses keep asking for quick fixes and a whole lot of hax code it's no surprise that everything is a mess....
oh well! such is the life of a developer...:rolleyes:

Jun 3, 2009, 10:59 PM
You should be able to do it with CFNotification. Just give it a function and pass it your C++ object, and inside the function call a method on the C++ object.

Jun 3, 2009, 11:16 PM
thanks kainjow,

i'm looking that up now. it sounds a lot easier than spending hours writing a wrapper and modifying the code where the new class should be....

i'm trying to figure out how to post a notification
NSNotification was easy since i could just use [NSNotification defaultCenter]

with CFNotificationCenterPostNotification i'm not sure how to get something to pass through as the first argument

trying to find out now with not much luck yet...

CFNotificationCenterGetDistributedCenter() seems to work

Jun 4, 2009, 12:29 AM
Are you using distributed notifications? If so then use that. Otherwise I think you only need CFNotificationCenterGetLocalCenter()

Jun 4, 2009, 01:14 AM
that might get rid of the error that was occuring when I tried to post a notification. I don't have the code in front of me right now but I think it said there was something wrong when posting the notification... Will try what you've suggested when I get back in front of that mac

Jun 4, 2009, 08:36 AM
ah, yeah, someone who takes over from me in the new code will also be like 'wtf is this retard doing??'

I usually write these long angry diatribes in the code comments. You would be surprised many people contact me for code I wrote about 15 years ago just to say how they laughed when they read the comments and looked at the code.

Depending on where you work, you usually don't have time to do it right all the time, thats when the funniest comments flow! LOL

Jun 4, 2009, 04:26 PM
kainjow, the local center worked, thanks for the correction
when would anyone use the distributed center over the local center?

pilotError, that's funny, people calling you up long after you've left :D
yeah, i realise that you can't do everything right when you're constantly required to make quick fixes... oh well.... i guess that's how things go

Jun 4, 2009, 04:41 PM
Distributed notifications are sent across processes, while local ones are only within the same process.

Jun 4, 2009, 07:26 PM
can i make a c array contain an NSObject?

Jun 4, 2009, 07:42 PM
Can you be clearer on what you're trying to do? You can make a C array of NSObjects. Remember Cocoa objects are just pointers to structs essentially.

Jun 4, 2009, 07:47 PM
sorry, my bad

i will post a CFNotification but i want to post some info with it too, so the observer can pick up that info and do stuff with it like you can with NSNotification

the documentation says the info parameter needs to be a CFDictionaryRef, so i went to make a CFDictionaryRef using CFDictionaryCreate

the arguments i put in were
CFDictionaryCreate(kCFAllocatorDefault, keys, values, 2, NULL, NULL)
i just don't know how to create keys and values with an NSObject

i thought maybe i could create a c class that has an id object as a variable that can be get and set... haven't tried it yet though

Jun 4, 2009, 11:02 PM

still don't get it.. :eek:

i tried using CFNotifcations but i can't stuff an NSObject into the CFDictionary
i tired creating a wrapper for the class but then i got compile errors "redeclared as a differemt kind of symbol" on the class itself
i tried getting access to the class i want to alter directly through this ugly chain of pointers and that failed. even though i can access the methods the compiler hate me for trying to do so and wont let me keep going...

the notifications just seemed so easy and clean and the best and quickest solution..... guess i might have to find another hax to do this if i can't find out how to put an object into the info parameter....