Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / June 2006



Tip: Looking for answers? Try searching our database.

How to declare a C or C++ function in a Objective C class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ankushiitk@gmail.com - 19 Jun 2006 23:22 GMT
Hi,
I need to write few functions, which I need to pass to a method in
Objective C class using the pointers to those functions. These
functions use the instance variable of the class. Hence they must have
access to the  variables.
I want to write functions in C or C++ because they are crucial to the
efficiency of my code and I don't want to use selectors way of passing
methods for the same reasons.
Then where do I include the function declations and implementations in
.h and .mm files of the class.
Ankush
David Phillip Oster - 20 Jun 2006 06:27 GMT
> 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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.