f



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

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

0
ankushiitk
6/19/2006 10:22:35 PM
comp.sys.mac.programmer.help 4653 articles. 1 followers. Post Follow

2 Replies
830 Views

Similar Articles

[PageSpeed] 45

In article <1150755755.068251.207990@h76g2000cwa.googlegroups.com>,
 ankushiitk@gmail.com wrote:

> 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

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;
}
0
David
6/20/2006 5:27:00 AM
Thanks David,
I has definitely helped me to move ahead. 

Ankush

0
ankushiitk
6/20/2006 8:09:06 PM
Reply:

Similar Artilces:

Better C/C++ Than C/C++?
I am looking for a good systems programming language that can be used instead of C/C++. My qualifications for the language are: * mature compiler(s) that produce native-code binaries (for Windows) * open source - preferable, but not 100% necessary Thanks, Kevin "Kevin Albrecht" <kevin@albrecht.net> writes: > I am looking for a good systems programming language > that can be used instead of C/C++. My qualifications > for the language are: > > * mature compiler(s) that produce native-code > binaries (for Windows) Ocaml, D, cyclone, Eiffel, Beta >...

jython and C-c C-c
How do I get C-c C-c to work with jython? I have the jpython command set to jython, and I can start the interpreter with C-C ! and then use C-c C-c, but this is rather frustrating. If I try to use C-c C-c without first starting the interpreter in another window I get wrong type argument: sequencep, jpython Thanks, Dave Cook ...

[Objective-C++] Objective-C wrappers around C++ classes and KVC
Suppose I have a C++ class named Bar, and an Objective-C wrapper for it called BarWrapper that I use so that BarWrapper has a few KVC-compliant methods (essentially, Bar is the model of something I need to link to an NSArrayController through BarWrapper). Now inside Bar I have a std::vector<Bar*> object, and of course I need to make indexed accessors of the BarWrapper* type. Now, I have two issues, both with the NSArrayController that the BarWrappers are connected to: - the first (and the easier one) is deleting BarWrappers: If I remove a BarWrapper from the master list, I ...

C/C++ in MATALB or MATLAB in C/C++?
Hi I've been asking on the net how to use MEX properly so that I can use C/C++ in MATLAB, but information is not forthcoming. There seems to be a lot on using MATLAB in C/C++ but not the other way around. I understand Mathworks is trying very hard to sell the Matlab Compiler, but even so, why is it SO hard to get information about MEX? Or is it that I've missed something very fundamental, meaning that I'm struggling over something that is actually quite easy? Any thoughts and info would be great. Thanks BullDog BullDog wrote: > Hi > > I've been asking on the net ...

slime C-c C-c annoyance
Does anyone have a good workaround for this problem other than: just be careful! When I've compiled a large package and slime has given me a *slime-compilation* buffer, I like to work through this buffer issue by issue, fixing the errors, warnings, etc. But If I ever press C-c C-c in the editor window while fixing an error, slime destroys the *(slime-compilation* buffer with the compilation output of C-c C-c. Jim Newton <jimka.issy@gmail.com> writes: > Does anyone have a good workaround for this problem other than: just be careful! > > When I've compiled a ...

C as a Subset of C++ (or C++ as a superset of C)
Isn't it a lame use of human time and effort to maintain completely separate C and C++ standards? As in the words of Betty White about Facebook: "It seems like an incredible waste of time". Why don't the two standards groups get together and agree on a common specification for the ground which both standards cover? There would still be two separate standards, but they'd both be exactly the same for the common ground. The common ground document could be referred to by both standards instead of being maintained by both groups in individual efforts resulting in...

C/C++ in matlab and matlab in C/C++ ?
Is it possible to read C/C++ code in matlab and the other way around too? Hi, as long as C or C++ are written in text files you can edit them in the matlab editor. To see the special character of C or C++ coding language, in the editor go to menus: file>preferences>Edito/Debugger>language and on popup "Language" select "C/C++". This does not allow you to run the C/C++ code, just to view it i nicer way... regards, chris saneman <asdfsdf@asd.com> wrote in message <frl846 $f8i$2@news.net.uni-c.dk>... > Is it possible to read C/C...

c------->c++ and c++------>c
We can write c program in turbo c++ editor.But reverse is not true why? i.e The program will not compile if u write c++ program in turbo c editor vim wrote: > We can write c program in turbo c++ editor.But reverse is not true why? > i.e The program will not compile if u write c++ program in turbo c > editor This isn't about C; this is about some editor. Why the fircone should /we/ know? Ask in a relevant group. [And /why/, in an editor specialised to some specific programming languages, would you expect it to compile a /different/ one? Would you expect turbo perl to compi...

C,, C++, Java, C#
I come from C,C++,Java and C#. What can SmallTalk do for me and for what cost? Regarding the Squeak thing, it's interesting though confusing. Is it an IDE? Why are the menus so wacky? "DM McGowan II" <nospam@nospam.net> wrote in message news:PLednX2vnLd11IncRVn-rA@comcast.com... > I come from C,C++,Java and C#. What can SmallTalk do for me ...? Ask not what Smalltalk can do for, but what can you do with Smalltalk. :-) In very few words, it significantly reduces time to market, while enhancig the development experience. This is because: 1. It...

c,c++,obj-c
Of the 3 languages titled in this post is c++ older than obj-c ? Would someone experienced in C be able to dive right into C++ rather than obj-c ? Java is out for me now except applet programming I think I'm going to go with just C and C++. Bill Bill Cunningham wrote: > Of the 3 languages titled in this post is c++ older than obj-c ? Why does that matter? Do you want to use an old language? Try Fortran or LISP. > Would > someone experienced in C be able to dive right into C++ rather than > obj-c ? Not quite dive in, at least not into the OO parts of C++. ...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! Throne Software wrote: > Throne Software has opened up C, C++ and C# Forums at: > > http://www.thronesoftware.com/forum/ For all of your forums newsgroups already exist. > Join us! Why? Don't see any reason to support your marketing. ...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! In article <1120963701.862698.160260@g14g2000cwa.googlegroups.com>, Throne Software <mail@thronesoftware.com> writes >Throne Software has opened up C, C++ and C# Forums at: > >http://www.thronesoftware.com/forum/ > >Join us! Why? The beauty of news groups is that they come to me I don't have to go to them. Also all the people who have been around a while know what they are doing and are here. I noticed that there are some C and C++ Forums on another system...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! ...

class C: vs class C(object):
Hi, I'm not an experienced developer, and I came across this statement by reading a code. I search for explanation, but can't find anything meaningful. I read the entire document written by python's creator about the features of version 2.2 The one named unifying types and classes. But This document only blew my head away. I ended here and think somebody can explain me more about this. The only thing I have noticed is that when you do dir(C) on a classic class you get a bunch of attributes; and when you do the same thing on a class defined using class C(object), you get less attr...

Web resources about - How to declare a C or C++ function in a Objective C class - comp.sys.mac.programmer.help

Welcome - Net Objectives Net Objectives - Scrum Agile Training & Agile Development - TDD, Agile Scrum ...
Deliver value to your customers faster. NetObjectives.com is the industry leader in agile scrum training, scrum methodology, lean agile training, ...

Multi-objective optimization - Wikipedia, the free encyclopedia
Multi-objective optimization (also known as multi-objective programming , vector optimization , multicriteria optimization , multiattribute optimization ...

Facebook Power Editor Users Can Now Preview News Feed Ads; Objectives Updated
Facebook began testing the ability for Power Editor users to preview News Feed ads as early as last September, and the social network confirmed ...

Electing Gollum should not be our objective
If you give him power he claims he will fix America, but there is another tradition in America. A tradition that believes that power corrupts, ...

New Facebook ad objective: Local awareness
Small businesses wanting to target Facebook users closer to them have a new advertising objective : Local awareness. Facebook announced this ...

Objective-Cologne Conference
Objective-Cologne is an Objective-C & Cocoa community-organized conference taking place next month in Cologne Germany. Speakers include Mike ...

Net Objectives (@netobjectives) on Twitter
Sign in Sign up To bring you Twitter, we and our partners use cookies on our and other websites. Cookies help personalize Twitter content, tailor ...

Net Objectives Business Driven Software Development - LinkedIn
The purpose of this group is to create a community of practice of people interested in using and learning more about Net Objectives&#39; Business ...

How-to articles for iPhone development and Objective-C
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation ...

Best Decision - choice making analytics that helps you choose the best objective solution for any problem ...
Get Best Decision - choice making analytics that helps you choose the best objective solution for any problem, save time and money, decisions ...

Resources last updated: 1/28/2016 7:53:27 PM