Convert Objective C formatted code to pure C ++

I recently started to study programming in order to make my own 3D OpenGL game on iPhone and still have made quite a decent progress. I started using the basic OpenGL example that comes with the iPhone SDK, which helped me get a good start. However, when I started to understand things, it occurred to me that I needed to program in Objective-C, which would make it difficult to port the game to other platforms in the future. So I decided that it would be better to do it correctly with C ++ in order to avoid additional additional work later.

To clarify: I don't actually use any calls to Apple functions (Objective-C) or anything else, just that I based all my clans on the Objective-C style init / dealloc / etc, so my engine is used when using the Objective-C class. My goal is to replace all C objects with C ++ equivalents ... the problem is that, being fairly new to C ++, I'm not sure if it matches that!

Here is a simple example of one of my classes (myLight) in its current incarnation of Objective-C:

// myLight.h #import <OpenGLES/EAGL.h> #import <OpenGLES/ES1/gl.h> #import <OpenGLES/ES1/glext.h> @interface myLight : NSObject { char *name; GLfloat *ambient, *diffuse, *specular, *position, *spotDirection; GLfloat spotRadius; GLfloat *matAmbient, *matDiffuse, *matSpecular; GLfloat shininess; Byte lightType; } @property (readonly) char *name; @property (assign) GLfloat *position; @property (assign) GLfloat *spotDirection; @property (assign) GLfloat *ambient; @property (assign) GLfloat *diffuse; @property (assign) GLfloat *specular; - (id)initWithContentsFromDatastream:(NSData *)fileData; - (void)set; @end 

And the corresponding .mm file:

 // myLight.m #import "myLight.h" @implementation myLight @synthesize name, ambient, diffuse, specular, position, spotDirection; - (id)initWithContentsFromDatastream:(NSData *)fileData { self = [super init]; NSData *fileContents = fileData; uint ptr = 0; Byte nameLength; [fileContents getBytes:&nameLength range: NSMakeRange(ptr, sizeof(Byte))]; ptr++; name = new char[nameLength]; [fileContents getBytes:name range: NSMakeRange(ptr, (nameLength * sizeof(char)) )]; ptr = ptr + (nameLength * sizeof(char) ); [fileContents getBytes:&lightType range: NSMakeRange(ptr, sizeof(Byte))]; ptr++; position = new GLfloat[4]; for(int j = 0; j < (4); j++) [fileContents getBytes:&position[j] range: NSMakeRange( (j* sizeof(float) ) + ptr, sizeof(float))]; ptr = ptr + (4 * sizeof(float)); if(lightType==2){ spotDirection = new GLfloat[3]; for(int j = 0; j < (3); j++) [fileContents getBytes:&spotDirection[j] range: NSMakeRange( (j* sizeof(float) ) + ptr, sizeof(float))]; ptr = ptr + (3 * sizeof(float)); [fileContents getBytes:&spotRadius range: NSMakeRange(ptr, sizeof(float))]; ptr = ptr + sizeof(float); } else spotDirection = NULL; diffuse = new GLfloat[4]; for(int j = 0; j < (4); j++) [fileContents getBytes:&diffuse[j] range: NSMakeRange( (j* sizeof(float) ) + ptr, sizeof(float))]; ptr = ptr + (4 * sizeof(float)); ambient = new GLfloat[4]; for(int j = 0; j < (4); j++) [fileContents getBytes:&ambient[j] range: NSMakeRange( (j* sizeof(float) ) + ptr, sizeof(float))]; ptr = ptr + (4 * sizeof(float)); specular = new GLfloat[4]; for(int j = 0; j < (4); j++) [fileContents getBytes:&specular[j] range: NSMakeRange( (j* sizeof(float) ) + ptr, sizeof(float))]; ptr = ptr + (4 * sizeof(float)); [self set]; return self; } - (void)set{ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, specular); glLightfv(GL_LIGHT0, GL_POSITION, position); if(lightType==2) glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection); } - (void)dealloc { delete[] specular; delete[] ambient; delete[] diffuse; if (spotDirection) delete[] spotDirection; delete[] position; delete[] name; [super dealloc]; } @end 

If someone can indicate which lines need to be changed, and, more importantly, that they need to be changed in order to compile it as pure C ++, I would really appreciate it.

Many thanks!

+4
source share
2 answers

First of all, keep in mind that Objective-C is not just a โ€œdifferent formatโ€ C, it is a separate language and more important, in the case of Cocoa, a separate framework. Therefore, if you want to be able to port your project to other platforms in the future, you need to not only get rid of Objective-C, but also from the Cocoa structure.

To map classes from Objective-C to C ++, you will need to do the following:

  • create a new C ++ class to replace the old class
  • create constructors for -init... methods -init...
  • create a destructor for the -dealloc method
  • create other methods that duplicate functionality
  • for properties that you would probably create getters and seters instead
  • replace #import with #include directives, since this directive exists only in Objective-C (make sure that the headers you include are protected from multiple inclusions)
  • get rid of the use of NS ... classes and methods since they are part of the Cocoa framework and most likely cannot be ported

You should take some time to think about how to use the code you write. Porting 1: 1 is probably not such a great idea, as there are many differences in idioms between coding in Cocoa and C ++ (or any other language / framework).

+10
source

Most of the application logic can be translated in C ++, but in C ++ there will be things that do not have a direct equivalent. For instance:

  • There is no compiler directive @encode() (which is used for NSValue and several other classes).
  • There is no direct equivalent to doesNotRecognizeSelector: respondsToSelector: conformsToProtocol: or performSelector: (and other methods of a similar nature).
  • In Objective-C, it is normal to send a message to nil , but in C ++ it is not normal to call a member function at a null pointer.
  • Objective-C allows you to distribute existing classes using class categories, for example, you can add a method to NSString that counts the number of spaces called numberOfSpaces , and it will be available for all NSString instances in your application.
  • C ++ does not have a finally block for try / catch , but Objective-C has @finally for @try / @catch .

If you plan to release your application on several platforms, then you can try to divert as much as you can into a separate library. An iPhone application can be written to Objective-C and use functions from your library, and if you plan to transfer it to another system whose framework requires C ++, then you can write a C ++ application and use the same functions from the same library. I believe that your library will consist mainly of [at least] your graphics routines and kernel logic.

+8
source

All Articles