The Games
Slay
Conquest
Critical Mass
The General
Mother Of All Battles
Firefight
Capture The Flag
Niggle
End Of Atlantis
Football-o-saurus
Rats!
Foosball
UFOs
Topple
WordStorm
Info
Download free versions
Buy full versions
History of the games
Link to this site
Thanks to...
Contact
Forums
Email me
About Sean O'Connor
CV
Other Projects
Photos
|
Convert Objective C to C++
I needed a way to convert my Objective C code for my iOS games into C++ so I could port them to Windows and Android. I couldn't find anything available to do it so I wrote a Windows app which will do most of the tedious work and preserve all my code comments.
Here's some Objective C
// imports
#import "header.h"
// defines
#define TEST_DEFINITION 10
#define TEST_MACRO(x) (x+1)
@implementation TestClass
#pragma mark -
#pragma mark Init
-(id)init
{
if (self = [super init]) // it doesn't handle this well so 'init's need to be fixed by hand
{
m_iMemberVariable = 0;
}
return (self);
}
-(BOOL)testMethod:(TestObject1*)pObject1 (TestObject2*)pObject2
{
short int iLocalVariable;
// single line comment
/* multiple
line
comment
*/
pObject = [[Object alloc] init];
[pObject release];
iTestArray[64] = 3; // mustn't be fooled into thinking the [ is the beginning of a method call!
strcpy(szBuffer, "[don't be fooled by the [!");
[self methodNoParamaters]; // comment on end of line
[self methodWithOneParameter:8];
[self methodWithMultipleParameters:8 and:10];
[pObject methodWithMethodAsParameter:[pObject test]];
[pObject methodWithMultiLineParameters:[pObject test1]
and:[pObject test2:9]];
#ifdef CONDTIONAL_COMPILATION_TEST
i = 3;
#endif
for (iBracketTest=0; iBracketTest<1; iBracketTest++)
{
switch ([pObject method:test1 and:test2])
{
case 1: break;
case 2: [pObject test]; break;
default: return FALSE;
}
}
return TRUE;
}
@end
And this is it automatically converted to C++:
// imports
#include "header.h"
// defines
#define TEST_DEFINITION 10
#define TEST_MACRO(x) (x+1)
#pragma mark -
#pragma mark Init
TestClass::TestClass()
{
if (self = __super::->init()) // it doesn't handle this well so 'init's need to be fixed by hand
{
m_iMemberVariable = 0;
}
return (self);
}
BOOL TestClass::testMethod(TestObject1* pObject1, TestObject2* pObject2)
{
short int iLocalVariable;
// single line comment
/* multiple
line
comment
*/
pObject = new Object;
pObject delete;
iTestArray[64] = 3; // mustn't be fooled into thinking the [ is the beginning of a method call!
strcpy(szBuffer, "[don't be fooled by the [!");
this->methodNoParamaters(); // comment on end of line
this->methodWithOneParameter(8);
this->methodWithMultipleParameters(8, 10);
pObject->methodWithMethodAsParameter(pObject->test());
pObject->methodWithMultiLineParameters(pObject->test1(), pObject->test2(9));
#ifdef CONDTIONAL_COMPILATION_TEST
i = 3;
#endif
for (iBracketTest=0; iBracketTest<1; iBracketTest++)
{
switch (pObject->method(test1, test2))
{
case 1: break;
case 2: pObject->test(); break;
default: return FALSE;
}
}
return TRUE;
}
What it doesn't do:
- It doesn't handle all the [super init] stuff and return 'self' in an init as C++ constructors are different.
- It doesn't convert any NS or CG functions because those are Apple functions.
- It doesn't do any of that crazy Objective C retain, auto release memory management stuff.
- It doesn't do properties, protocols, categories etc...
- Objective C doesn't mind you calling a method on a NULL object. C++ will crash if you do that.
|
|