-
Notifications
You must be signed in to change notification settings - Fork 1.3k
GSoC 2009 Objective C
This proposal discusses the idea of supporting a new target language with SWIG i.e. Objective C to allow the generation of Objective C wrappers over C++ libraries. Background
With ever-increasing demands of rapid application development and need for more native looking user interface in a client application, a number of desktop and enterprise systems are considering to replace their clients developed in older languages such as C++ with modern frameworks such as Cocoa/Objective C on Mac OS X and .Net/C# on windows. These frameworks and the individual languages with their native support provide natural advantages: The development is quick and stable and maintenance simplicity is another plus. However, for performance and historical reasons, there are codebases which are developed and maintained in C++. There is an outgrowing need for solutions which can bridge this technology gap and allow native applications developed in Objective C or C# to communicate too and fro with C++ applications. Unlike C# and C++ integration, there is no good solution available for automatically generating code for integrating C++ with Objective C.SWIG is a powerful wrapper and interface generator toolkit which allows automatic generation of interfaces and wrappers over C++ for number of target languages including C# but there is no support as yet for Objective-C as a target language in SWIG. This project would add support to SWIG for Objective-C as a target language.
- Cocoa/Objective-C is the preferred development framework on Mac OS X just like .Net/C# on Windows and the use of Objective-C is growing tremendously with increasing popularity of Mac OS X and iPhone. Supporting Objective-C as the target language for SWIG would help SWIG not only to find its way to Mac OS X and iPhone community but would also provide an opportunity to emerge as a solution bridging technology gaps in middle level infrastructure for both desktop and enterprise solutions. For example, there is an immediate need for such solutions for web services and CORBA communication. 2.In future scope, as part of the support for Objective-C, I would like to find a solution for the multiple inheritance problem. Objective-C supports protocols which are similar to Java interfaces so if successful the solution could be applied to other Target languages later on.
The support for Objective-C as a target language, in essence, would be similar to the existing Java and C# support. For every possible C++ construct in the SWIG interface, the corresponding interface header and proxy code would be generated. While, the interface header contains an equialent Objective C representation of every given C++ construct, the proxy (objective c++) provides the glue code for necessary type conversions i.e for primitve types, pointers, references, arrays and custom types. Objective-C is an object oriented extension to C which means that other than constructs for creating and manipulting objects, all valid C constructs are valid constructs in Obj-C. Hence they appear as is in the objective c interface. Specifically, there will be a support to generate objective c constructs and proxies for all C++ and C constructs such as: classes, callbacks, enums, constants, function pointers, global functions, variables and c+ templates. Some example use cases for a set of C++ (base and derived) classes and exception handling are given below:
class Shape {
public:
Shape() {
nshapes++;
}
virtual~Shape() {
nshapes--;
};
double x, y;
void move(double dx, double dy);
virtual double area(void) = 0;
virtual double perimeter(void) = 0;
static int nshapes;
};
class Circle: public Shape {
private: double radius;
public: Circle(double r): radius(r) {};
virtual double area(void);
virtual double perimeter(void);
};
class Square: public Shape {
private: double width;
public: Square(double w): width(w) {};
virtual double area(void);
virtual double perimeter(void);
};
The pointer to shape class held in the below class would possibly a var type to allow automatic memory management.
@interface ObjShape : NSObject
{
Shape* cppObj;
}
- (double)x;
- (double)y;
- (void)setX: (double)x;
- (void)setY: (double)y;
+ (int)setNshapes;
+ (void)nshapes: (int)n;
- (double)area;
- (void)move: (double)dx dy: (double)dy;
- (double)perimeter;
@end
@interface ObjCircle : ObjShape
{
Circle* cppObj;
}
- (double)area;
- (double)perimeter;
@end
@interface ObjSquare : ObjShape
{
Square* cppObj;
}
- (double)area;
- (double)perimeter;
@end
In the following code, the Conversion functions signify the type conversions for to and fro conversion between C++ and Objective C types.
@implementation ObjShape
- (double)area {
return Conversion(cppObj->area());
}
- (void)move: (double)dx dy: (double)dy {
cppObj->move(ConversionToCPP(dx), ConversionToCPP(dy));
}
- (double)perimeter {
return Conversion(cppObj->perimeter());
}
- (id)initWithActualType: (Shape*)actualObj {
self = [super init];
cppObj = actualObj;
return self;
}
- (Shape*)actualType {
return cppObj;
}
- (void)setX: (double)ax {
cppObj->x = ConversionToCPP(ax);
}
- (void)setY: (double)ay {
cppObj->y = ConversionToCPP(ay);
}
- (double)x {
return Conversion(cppObj->x);
}
- (double)y {
return Conversion(cppObj->y);
}
+ (int)setNshapes {
return Conversion(Shape::nshapes);
}
+ (void)nshapes: (int)n {
Shape::nshapes = ConversionToCPP(n);
}
@end
@implementation ObjCircle
- (id)init: (double)r {
if (self = [super init]) {
cppObj = new Circle(r);
}
return self;
}
- (double)area {
return Conversion(cppObj->area());
}
- (double)perimeter {
return Conversion(cppObj->perimeter());
}
- (id)initWithActualType: (Circle*)actualObj {
self = [super init];
cppObj = actualObj;
return self;
}
- (Circle*)actualType {
return cppObj;
}
@end
@implementation ObjSquare
- (id)init: (double)w {
if (self = [super init]) {
cppObj = new Square(w);
}
return self;
}
- (double)area {
return Conversion(cppObj->area());
}
- (double)perimeter {
return Conversion(cppObj->perimeter());
}
- (id)initWithActualType: (Square*)actualObj {
self = [super init];
cppObj = actualObj;
return self;
}
- (Square*)actualType {
return cppObj;
}
@end
int main(int argc, char* argv[])
{
ObjCircle *c = [[ObjCircle alloc] init: 3];
ObjSquare *s = [[ObjSquare alloc] init: 30];
Shape *sc = c;
Shape *ss = s;
printf("The area of the circle is %f\n", sc->area);
printf("The area of the square is %f\n", ss->area);
[circle release];
[square release];
return 0;
}
It is possible to throw an Objective C Exception from Objective C++ code directly. Having a sneak look at the SWIG documentation, it appears that SWIG would be able to associate catch handlers with functions with the throw clause and using the %catches, %exception feature. The conversion of some c++ exception could be done as shown in the simple example below:
class MyException {};
class ExceptionTest {
public:
ExceptionTest() {}
~ExceptionTest() {}
void funct() throw(MyException)
};
@interface ObjCMyException: NSException //just like wrapper objects this would hold the pointer to the original cpp object
@end
@interface ObjCExceptionTest: NSObject {
ExceptionTest *cppObj;
}
- (id)init;
- (void)dealloc;
- (void)funct;
@end
@implementation ObjCExceptionTest: NSObject
- (id)init {...}
- (void)dealloc {...}
- (void)funct {
id anyException = nil; //id: obj-c equivalent of void*
@try {
try {
cppObj->funct(); //In most cases this would also involve some objc++ code for type conversion which can throw NSException
} catch(MyException *e) {
ObjCMyException *ex = Conversion(e);
anyException = ex;
} catch (...) { //default handling
anyException = [NSException exceptionWithName: @"UnknownException" reason: @"" userInfo: NULL];
}
}
@catch(NSException *ex) {
anyException = ex;
}
@finally {
@throw anyException; //throw the caught exception to the client code
}
}
@end
ObjCExceptionTest* ext = [[ObjCExceptionTest alloc] init];
@try {
[ext funct];
}@catch (ObjCMyException *ex) {
print("Caught exception %@",ex);
}
[ext release];
My motivation for the project comes from my experience in developing a SWIG like framework for cross language integration. I designed and developed an extensible framework for automatic generating code for cross-language integration. The framework performs and scales well for languages such as C#, Objective C, C++ and their inter-operability. Developed in Perl, the framework uses different parsing modules for each of these languages and a generator component which generates code in the desired language along with the interfaces and the wrappers. With this framework, I developed an IDL to Objective C Converter and a tool for providing Objective C interfaces over MICO libraries - an open source C++ implementation of CORBA specification. The toolkit made possible the development of QPS 7.0, the next generation editorial workflow system - A running example of what this project intent to achieve but through SWIG.
0th Phase: Analysis and Communication Apr 15th to May 22nd During this time frame, I will dig into the SWIG internals and analyze the changes required to add support for new target langauge. This period will also involve communication with the SWIG community. May 23rd to June 4th Unavailable for the project.
1st Phase:, June 6th to July 11th General design and framework in place. Support for basic constructs and conversions along with the test cases and documentation Week 1, 2: General design and framework for target language support. Week 3,4,5 : Support for c++ classes (including class methods, overloaded methods), functions, test cases and documentation.
2nd Phase: July 14th to August 17th Week 1,2: Support for callbacks, function pointer, exception handling, test cases and documentation. Week 3,4: Support for globals, enums and constants, templates, test cases and documentation.
As a future work, I plan to incroporate the multiple inheritance support for Objective C target mapping. Mutliple inheritance in C++ would be mapped to a single class inheritance and protocols for Obj-C. I have a long term plan of using SWIG to generate Objective C wrappers around MICO CORBA C++ libraries and a neat Objective C solution for SOAP using Axis C++ or gSOAP.
Project-Related: With 4.5 years of professional experience at Quark Inc, I have gained exposure to varied technologies, software languages and platforms as a software programmer. The relevant ones for this project are C++, Objective C, and Mac OS X. I am also well versed with tools like CVS, SVN, vi-editor, Shark, Malloc-debug, and Silk Radar etc. I have little knowledge of SWIG to start with but I am confident of getting a good grasp on it much before I start the project since I already have an experience in developing the code generation frameworks for languages such as C++, C#, IDL and Objective C. Open-Source: My exposure to open source technologies include: MICO orb, OMNI orb, Apache Axis C++, Apache ActiveMQ, Apache Web server. I made some critical changes to the open source MICO project to make it run on Mac OS X. Some of these changes found their way in MICO 2.3.13 release (link here). As a Student: At University of Minnesota-Twin Cities, my research concentration is towards programming languages and interoperability and in the current semester I am building a complier for a subset of C language (C--) using Yacc/Bison.
I am 26 years old and native of Ambala, a pleasant small town in Haryana, India. I am currently enrolled in the Masters of Science program at University of Minnesota - Twin Cities, Computer Science department in Minneapolis, Minnesota with research interests in Programming languages and Human Computer Interaction on Mobile phones. More information about me can be obtained at my homepage - ashishs99.googlepages.com. My IRC name on #swig-gsoc is ashishs99 and my email address is ashishs99@gmail.com.