[futurebasic] Re: [FB] FBtoC Project and Xcode Project

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : June 2008 : Group Archive : Group : All Groups

From: Ross Tulloch <r.tulloch@...>
Date: Mon, 23 Jun 2008 11:31:40 +1000
Hi,

I've updated my FBCocoaIterator sample to show how you can, at  
runtime, add a BASIC (i.e C)
function to an ObjC class to declare a message.

The code boils down to the ObjC runtime function "class_addMethod/s".  
Details here: <http://tinyurl.com/3qo29c>

The first two args to the basic function MUST be two pointer values.
These are, respectively: "self" and "cmd". "self" is the object to  
whom this message is directed and "cmd" is the message (as SEL).

Caveat: The downside of doing this at runtime is that your essentially  
manually doing something you probably want the compiler/linker to do  
for you. It's a maintenance nightmare - you could conceivably have  
tens of thousands of "AddFNToClass" calls in your app.
I don't think you want to write them manually!

<http://zeroonetwenty.com/code/FBCocoaIterator.zip>

The code is very quick and dirty. The zip archive still includes the  
original version of the code if you want to do a compare.
You'll need FBtoC with "Use Objective C compiler" enabled to compile  
it. Only tested under 10.5/Intel.

In the runtime doco linked to above are the details of the low-level  
calls required to send messages to objects also (though I recommend  
reading Apple's Objective-C book first).



On the topic of calling syntax... If you want to use something that  
looks procedural you could use calls that look something like:

oc NSMutableString_insertString_atIndex( myString, aStringToInsert, 10 )

Note the "oc" instead of "fn". From which FBtoC could generate the  
ObjC code:

[myString insertString:aStringToInsert atIndex:10];

...and pass it off to GCC. This is just string juggling. As long as  
the code style can be "mangled" back to ObjC you could, perhaps, do  
some things quite simply. It's another idea to kick around anyway.

Anyway, I'll leave it at that. I hope some of this is helpful and/or  
interesting.



Ross.