Re: Mixing Obj-C and C methods

2013-08-07 Thread Maxthon Chan
My common way of handling this would be NSNotificationCenter. It is a singleton so I am always sure that it is there, and I can wrap all parameters into the userInfo dictionary. Sent from my iPhone On 2013年7月30日, at 21:19, KappA rejek...@gmail.com wrote: I sometimes just access my

Re: Mixing Obj-C and C methods

2013-08-07 Thread Tom Davie
On 30 Jul 2013, at 15:44, Maxthon Chan xcvi...@me.com wrote: My common way of handling this would be NSNotificationCenter. It is a singleton so I am always sure that it is there, and I can wrap all parameters into the userInfo dictionary. NSNotificationCenter is not a singleton: $ cat

Re: Mixing Obj-C and C methods

2013-08-07 Thread Jean-Daniel Dupas
While all theses methods may look valid, what not simply use a static variable declared in your file ? Instead of trying to use complex approach to hide the fact you need a global, just use one, and don't try to reuse the existing one for things there are not designed to do. static id

Re: Mixing Obj-C and C methods

2013-08-07 Thread Maxthon Chan
Sorry, clicked a wrong button. On Aug 7, 2013, at 16:00, Maxthon Chan xcvi...@me.com wrote: Well here is a reason I think that is valid enough to implement a callback using notifications: that is what Objective-C use for what callbacks used to do, besides target-actions and delegations. In

Re: Mixing Obj-C and C methods

2013-08-07 Thread Scott Ribe
On Aug 7, 2013, at 1:47 AM, Jean-Daniel Dupas devli...@shadowlab.org wrote: While all theses methods may look valid, what not simply use a static variable declared in your file ? DING DING DING DING! WE HAVE A WINNER! Actually, much of the history behind other methods for singletons revolves

Re: Mixing Obj-C and C methods

2013-08-07 Thread Andy Lee
On Aug 7, 2013, at 3:47 AM, Jean-Daniel Dupas devli...@shadowlab.org wrote: Instead of trying to use complex approach to hide the fact you need a global, just use one, and don't try to reuse the existing one for things there are not designed to do. static id myCallbackHandler; void

Re: Mixing Obj-C and C methods

2013-08-07 Thread Jean-Daniel Dupas
Le 7 août 2013 à 17:32, Andy Lee ag...@mac.com a écrit : On Aug 7, 2013, at 3:47 AM, Jean-Daniel Dupas devli...@shadowlab.org wrote: Instead of trying to use complex approach to hide the fact you need a global, just use one, and don't try to reuse the existing one for things there are not

Re: Mixing Obj-C and C methods

2013-08-07 Thread Andy Lee
On Aug 7, 2013, at 12:04 PM, Jean-Daniel Dupas devli...@shadowlab.org wrote: If you intend to use it from multiple threads, so use a tls. __thread id myCallbackHandler; I did not know about __thread, thanks for this. By using tls you're effectively having each thread store the info that

Re: Mixing Obj-C and C methods

2013-08-07 Thread Jean-Daniel Dupas
Le 7 août 2013 à 18:34, Andy Lee ag...@mac.com a écrit : On Aug 7, 2013, at 12:04 PM, Jean-Daniel Dupas devli...@shadowlab.org wrote: If you intend to use it from multiple threads, so use a tls. __thread id myCallbackHandler; I did not know about __thread, thanks for this. By using tls

Re: Mixing Obj-C and C methods

2013-07-31 Thread Andy Lee
On Jul 30, 2013, at 5:29 PM, Michael Crawford li...@warplife.com wrote: That class object occupies a non-zero quantity of memory, at its lowest level being somewhat like the combination of a single copy of a C struct, as well as some C functions, that from the Objective-C point of view, appear

Mixing Obj-C and C methods

2013-07-30 Thread Vincent Habchi
Hi everybody, I have a very simple question: if I embed a C-function (more precisely, a callback from an external C-library) in an Obj-C object, can I expect this function to behave like a regular method? I.e. can it freely access ‘self’ and other attributes? Thanks a lot! Vincent

Re: Mixing Obj-C and C methods

2013-07-30 Thread Rick Mann
On Jul 30, 2013, at 00:59 , Vincent Habchi vi...@macports.org wrote: I have a very simple question: if I embed a C-function (more precisely, a callback from an external C-library) in an Obj-C object, can I expect this function to behave like a regular method? I.e. can it freely access

Re: Mixing Obj-C and C methods

2013-07-30 Thread Michael Crawford
I don't think so, not it if has a C-language prototype rather than an Objective-C method prototype. However, I expect there is a way you could call an Objective-C method from vanilla C. Possibly you will need some assembly-language glue. Ultimately, Objective-C method calls are implemented, I

Re: Mixing Obj-C and C methods

2013-07-30 Thread Vincent Habchi
Rick, thanks for answering, because what I found on the Internet seems contradictory. Some say that if the C function is placed inside the implementation block, then it can access attributes as if it were a true Obj-C method; some say otherwise. So it’s a bit difficult to find a definitive

Re: Mixing Obj-C and C methods

2013-07-30 Thread Rick Mann
On Jul 30, 2013, at 01:27 , Vincent Habchi vi...@macports.org wrote: Rick, thanks for answering, because what I found on the Internet seems contradictory. Some say that if the C function is placed inside the implementation block, then it can access attributes as if it were a true Obj-C

Re: Mixing Obj-C and C methods

2013-07-30 Thread Jean-Daniel Dupas
Le 30 juil. 2013 à 10:27, Vincent Habchi vi...@macports.org a écrit : Rick, thanks for answering, because what I found on the Internet seems contradictory. Some say that if the C function is placed inside the implementation block, then it can access attributes as if it were a true

Re: Mixing Obj-C and C methods

2013-07-30 Thread lowell
The first two parameters to the function have to be an id and a SEL ... typedef id (*IMP)(id, SEL, ...); ... (this is where we get self and _cmd, by the way) followed by the rest of the method params, if any. lowell On Jul 30, 2013, at 12:59 AM, Vincent Habchi vi...@macports.org wrote:

Re: Mixing Obj-C and C methods

2013-07-30 Thread KappA
I sometimes just access my objc-objects from a C thread-proc via the AppDelegate (providing there's a trail to the object I need, which there usually is)... If the callback void pointer parameter isn't being used for something else, you can simply cast the object in there... or if you need

Re: Mixing Obj-C and C methods

2013-07-30 Thread Andy Lee
On Jul 30, 2013, at 4:19 AM, Rick Mann rm...@latencyzero.com wrote: On Jul 30, 2013, at 00:59 , Vincent Habchi vi...@macports.org wrote: I have a very simple question: if I embed a C-function (more precisely, a callback from an external C-library) in an Obj-C object, can I expect this

Re: Mixing Obj-C and C methods

2013-07-30 Thread Scott Ribe
On Jul 30, 2013, at 8:35 AM, Andy Lee ag...@mac.com wrote: The only effect, as others have explained, is on scope; if you put the function inside the @implementation and the function has a reference to an instance of MyClass, then it can use myObj-myIvar for direct access to instance

Re: Mixing Obj-C and C methods

2013-07-30 Thread Andy Lee
On Jul 30, 2013, at 4:27 AM, Vincent Habchi vi...@macports.org wrote: Yes, right; it’s a SQLite callback, the first parameter is a void *. I wanted to pass a pointer to a structure containing both a unique query id (out of uuid) and a pointer to self, but got told off by ARC because it

Re: Mixing Obj-C and C methods

2013-07-30 Thread Andy Lee
On Jul 30, 2013, at 10:42 AM, Scott Ribe scott_r...@elevated-dev.com wrote: On Jul 30, 2013, at 8:35 AM, Andy Lee ag...@mac.com wrote: The only effect, as others have explained, is on scope; if you put the function inside the @implementation and the function has a reference to an instance

Re: Mixing Obj-C and C methods

2013-07-30 Thread Scott Ribe
On Jul 30, 2013, at 9:08 AM, Andy Lee ag...@mac.com wrote: I think it's subject to the same criticisms as *any* direct access to ivars, although I agree it feels sketchier when done in plain C for some reason. Yes. Because what is the point of plain C functions in Objective-C files? Local

Re: Mixing Obj-C and C methods

2013-07-30 Thread Andy Lee
On Jul 30, 2013, at 4:26 AM, Michael Crawford li...@warplife.com wrote: However, I expect there is a way you could call an Objective-C method from vanilla C. Possibly you will need some assembly-language glue. The nice thing is, you don't need glue. You can send Objective-C messages from

Re: Mixing Obj-C and C methods

2013-07-30 Thread Andy Lee
On Jul 30, 2013, at 11:25 AM, Scott Ribe scott_r...@elevated-dev.com wrote: On Jul 30, 2013, at 9:08 AM, Andy Lee ag...@mac.com wrote: I think it's subject to the same criticisms as *any* direct access to ivars, although I agree it feels sketchier when done in plain C for some reason.

Re: Mixing Obj-C and C methods

2013-07-30 Thread Jean-Daniel Dupas
Le 30 juil. 2013 à 17:25, Scott Ribe scott_r...@elevated-dev.com a écrit : On Jul 30, 2013, at 9:08 AM, Andy Lee ag...@mac.com wrote: I think it's subject to the same criticisms as *any* direct access to ivars, although I agree it feels sketchier when done in plain C for some reason.

Re: Mixing Obj-C and C methods

2013-07-30 Thread Vincent Habchi
Hi and thanks a lot to anybody! I posted some answer before, but since it included a screenshot I’m afraid it didn’t make it through. I was just trying to show that when I access an iVar of ‘self’ in the C-function (e.g. self - _egg), Xcode autocompletion pop-up shows the iVars list, but each

Re: Mixing Obj-C and C methods

2013-07-30 Thread John McCall
On Jul 30, 2013, at 8:54 AM, Vincent Habchi vi...@macports.org wrote: Apple's recommended alternative to having a struct contain an object is to use a class instead of a struct. You could create a MyCallbackInfo class with two properties: the query id and the pointer to self. You'd still

Re: Mixing Obj-C and C methods

2013-07-30 Thread Quincey Morris
On Jul 30, 2013, at 08:54 , Vincent Habchi vi...@macports.org wrote: The way SQLite works, after a query is submitted, the thread is suspended and a callback is repeatedly executed There's one other consideration, in the case when the callback is called a thread other than the main thread,

Re: Mixing Obj-C and C methods

2013-07-30 Thread Lee Ann Rucker
On Jul 30, 2013, at 8:48 AM, Andy Lee wrote: On Jul 30, 2013, at 11:25 AM, Scott Ribe scott_r...@elevated-dev.com wrote: On Jul 30, 2013, at 9:08 AM, Andy Lee ag...@mac.com wrote: I think it's subject to the same criticisms as *any* direct access to ivars, although I agree it feels

Re: Mixing Obj-C and C methods

2013-07-30 Thread Kyle Sluder
On Tue, Jul 30, 2013, at 11:49 AM, Lee Ann Rucker wrote: On Jul 30, 2013, at 8:48 AM, Andy Lee wrote: On Jul 30, 2013, at 11:25 AM, Scott Ribe scott_r...@elevated-dev.com wrote: On Jul 30, 2013, at 9:08 AM, Andy Lee ag...@mac.com wrote: I think it's subject to the same criticisms

Re: Mixing Obj-C and C methods

2013-07-30 Thread Michael Crawford
The same is true for class methods, by the way. If a class method has a reference to an instance of the class, it can directly access the instance's ivars the same way. Objective-C shares a property with some languages, but not with others, that classes are actual objects. That is, if my