Re: Passing C Style Function Callbacks in Cocoa
Re: Passing C Style Function Callbacks in Cocoa
- Subject: Re: Passing C Style Function Callbacks in Cocoa
- From: Vince Ackerman <email@hidden>
- Date: Sun, 11 Apr 2004 09:04:38 -0700
On Apr 9, 2004, at 09:19, Fritz Anderson wrote:
1. Does the TMioDin struct contain a void * or long integer labelled as a "user," "client," or "reference" value? That would be space in the struct set aside for your use, and you could use it to store the pointer to your associated object.
That's a possibility.... here's some of the 3rd party's header for one of the usb devices I'm trying to communicate with. The TMioDin is a struct with only one member. Could I add a pointer to the struct with out screwing something up further downline? Most of the callbacks in all of the devices have this same sort of struct. The TMioDin is initialized by calling the create function with the handle to a TMioDin, which is used by all the functions in their libraries to comunicate with the device.
/**
TeleoMioDin reference structure.
This structure exists to permit type safety
for users of the device without
exposing the internals to all callers.
*/
typedef struct
{
uint8 tag;
} TMioDin;
/** Create a TMioDin.
\param tdm TeleoDeviceManager reference
\param address module address (or 0 if don't care)
\param dinDeviceIndex Din device on the MultiIO (0 - 3)
\param bound is the module specified already bound?
\param tmdin a variable to hold the new device
\return TELEO_OK (0) or an error value
*/
TeleoError TMioDin_Create( TeleoDeviceManager* tdm, cchar* address, uint8 dinDeviceIndex, bool* bound, TMioDin** tmdin );
/// This is the function used to set the callback to my function, which is outside of my objects:
/** Set the callback for value updates
\param tmdin TMioDin
\param valueUpdate valueUpdate callback function
\return TELEO_OK or appropriate error code
*/
TeleoError TMioDin_valueCallbackSet( TMioDin* tmdin, TeleoError (*valueUpdate)( TMioDin* tmdin, bool value ) );
I did add a [ myObjectXX retain] in my initialize method and that seems to have stopped the crash and "EXC_BAD_ACCESS" debugger message. Don't know if I understand completely..... I'm definitely not the sharpest tool in the shed.
I'll take some time and study your example below. I'll have to look up NSEnumerator and try and figure out what you're suggesting. I figured I add an array to my objects later when I found out what I was doing wrong.
Thanks for all your help!!
2. Failing that, it would still be a good idea to keep an array of your objects, and in your callback riffle through them to identify the one associated with the TMioDin. If you use an NSMutableArray to keep the objects, you won't have to worry about running out of storage for them (which was a problem with your earlier scheme, which used a fixed number of globals).
@implementation TMioDinObject
static NSMutableArray * sAllObjects = nil;
+ (void) initialize
{
if (! sAllObjects)
sAllObjects = [[NSMutableArray alloc] init];
}
+ (TMioDinObject *) existingObjectForDevice: (TMioDin *) aDevice
{
NSEnumerator * iter = [sAllObjects objectEnumerator];
TMioDinObject * curr;
while (curr = [iter nextObject])
if ([curr device] == aDevice)
return curr;
return nil;
}
- (id) initWithTMioDin: (TMioDin *) aDevice
{
if ([TMioDinObject existingObjectForDevice: aDevice]) {
// Error: attempt to allocate a second object for same device
[self release];
return nil;
}
device = aDevice;
[sAllObjects addObject: self];
return self;
}
- (void) release
{
[super release];
if ([self retainCount] == 1)
[sAllObjects removeObject: self];
}
- (TMioDin *) device { return device; }
On 9 Apr 2004, at 9:08 AM, Vince Ackerman wrote:
Can anyone help?
On Apr 8, 2004, at 07:14, Vince Ackerman wrote:
Clark, Hiro, and others.
Thanks for responding. This C Style function callback is outside of any objects and passed to 3rd party code I'm using with their USB device. The "tmdin" value is an instance variable in one of my objects (the address of one of the USB devices making the callback) and not an object in itself. My problem was to be able to send a message to my object from the callback that the device's variable (aValue) had changed. I originally wanted to be able to pass off a method in my object as the callback, but the third party code only takes a C style function set up with the two variables (TMioDin*, bool). Assigning my object to a "static id myObject" was a work around that seemed to work for a while, but maybe it is being flushed somehow. How do I create a variable like this outside of any other object and retain it?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.