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: Fritz Anderson <email@hidden>
- Date: Fri, 9 Apr 2004 11:19:57 -0500
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.
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.