> Hi,
> I need to write few functions, which I need to pass to a method in
[quoted text clipped - 7 lines]
> .h and .mm files of the class.
> Ankush
Consider adding a userData parameter to your fast C++ functions. Define
a struct that has a pointer to your Objective C object instance
members. Add an initBridge Objective-C method that initializes the
struct with pointers to objective-C members. Now, your fast function
gets a pointer to the initialized struct in its userData, and can
indirect through it to get at the Obj-C data.
silly example:
// bridge struct, a C struct, holds pointers into an Obj-C instance
typedef struct BridgeStruct {
float *mAry1;
float *mAry2;
float *mResult;
}BridgeStruct;
typedef void (*Operation)(BridgeStruct* bridge);
// our sample Obj-C object.
@interface Example : NSObject {
float mAry1[1000];
float mAry2[1000];
float mResult[1000];
}
- (void) initBridge: (BridgeStruct*) bridge;
- (void) doFastOperation: (Operation) operation;
@end
// ordinary C function, takes a pointer to a bridge struct
void Multiply(BridgeStruct* bridge){
int i;
for(i = 0; i < 1000; ++i){
bridge->mResult[i] = bridge->mAry1[i] * bridge->mAry2[i];
}
}
@implementation Example
// initialize the bridgestruct
- (void) initBridge: (BridgeStruct*) bridge {
bridge->mAry1 = mAry1;
bridge->mAry2 = mAry2;
bridge->mResult = mResult;
}
// effectively apply the operation to self.
- (void) doFastOperation: (Operation) operation {
BridgeStruct bridge;
[self initBridge: &bridge];
(*operation)(&bridge);
}
@end
// example of usage.
int main(int argc, char *argv[]){
Example *example = [[Example alloc] init];
[example doFastOperation: Multiply];
return 0;
}
ankushiitk@gmail.com - 20 Jun 2006 21:09 GMT
Thanks David,
I has definitely helped me to move ahead.
Ankush