views:

233

answers:

2

I am seeking an opinion from the Mac/iPhone developers about a new project. The idea is to start an open source initiative using swig (www.swig.org) for automatically generating the bridge from objective c to c++ so that one can access c++ applications from cocoa/cocoa touch.

Apple provides a very good support for mixing objective c and c++ via objective c++ but writing those bridges by hand could be tedious and bug prone. What this project intend to do is to provide a way of automatically generating objective c interfaces and wrappers over c++ so that any cocoa or cocoa touch application happens to see the object oriented objective c interface with c++ underneath.

I would greatly appreciate any opinion or suggestions over this idea.

A: 

It strikes me as a good enough idea that I'd be surprised if something like it doesn't already exist. On the other hand, since you can compile C++ directly, (look at what a .mm extension gives you) it might be somewhat trivial.

Charlie Martin
+2  A: 

I do not think this is necessary. With Objective-C++, you do not need a bridge at all. Given a C++ class defined

class Foo {
  public:
    void someMethod(void);
}

you can use that class anywhere in Objective-C++ code. For example, within a method:

- (void)myObjCMethod {
  Foo myFoo;

  myFoo.someMethod();

  //etc.
}

You can have instance variables that point to C++ classes, so you could define an Objective-C class like

@interface Bar : NSObject {
  Foo *foo;
}

@property (assign) Foo * foo;
@end

@implementation
@synthesize foo;

- (void)dalloc {
  delete foo;
  [super dealloc];
}

- (id)init {
  if(self = [super init]) {
    foo = new Foo();
  }

  return self;
}

- (void)aMethod {
   self.foo->barMethod();
}

I don't think you can template an Objective-C method, but C++ template instantiations in Objective-C++ are fair game. Just as Objective-C is a strict superset of C, Objective-C++ is a superset of C++, adding Objective-C-style classes and message passing.

If you did want to "bridge" a C++ class, you're going to have trouble in general, since things like multiple inheritance and operator overloading aren't supported in Objective-C's object model.

Barry Wark