Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-16-2009, 05:30 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 6
johnno is on a distinguished road
Default Role for C++ in Objective-C development?

Hi,

I've just noticed Xcode's gcc compiler supports mixing C++ and Objective-C in the same source file:
The Objective-C 2.0 Programming Language: Using C++ With Objective-C

So while I'm not the greatest fan of what operator overloading can do to a program I was wondering if there wasn't a role for some moderate use to look after reference counting on assignment and to provide a shorthand for commonly used methods such as [array objectAtIndex:0] or [dict objectForKey:@"KEY"] by overloading the subscript operator.

I've had a go at doing this using a header file and resulting code seems far cleaner and more compact.
ObjCpp.h

Can something really be added to Objective-C using it in this way?

John H.
johnno is offline   Reply With Quote
Old 04-16-2009, 01:56 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by johnno View Post
Hi,

I've just noticed Xcode's gcc compiler supports mixing C++ and Objective-C in the same source file:
The Objective-C 2.0 Programming Language: Using C++ With Objective-C

So while I'm not the greatest fan of what operator overloading can do to a program I was wondering if there wasn't a role for some moderate use to look after reference counting on assignment and to provide a shorthand for commonly used methods such as [array objectAtIndex:0] or [dict objectForKey:@"KEY"] by overloading the subscript operator.

I've had a go at doing this using a header file and resulting code seems far cleaner and more compact.
ObjCpp.h

Can something really be added to Objective-C using it in this way?

John H.
I think you could get the same result with a C macro instead - I know I've seen one for array access before, but I can't find it now.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 04-16-2009, 02:07 PM   #3 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by smasher View Post
I think you could get the same result with a C macro instead - I know I've seen one for array access before, but I can't find it now.
Found it!
Code:
#define x<y> [x objectAtIndex:y]
Now you could write myArray<0> to get object zero from the array.

(copied from cocoabuilder, not tested.)
Cocoabuilder - (Thomas Davie) Re: multidimensional arrays
__________________

Free Games!
smasher is offline   Reply With Quote
Old 04-17-2009, 11:33 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: San Diego, CA
Posts: 406
jtara is on a distinguished road
Default

Quote:
Code:

#define x<y> [x objectAtIndex:y]

Now you could write myArray<0> to get object zero from the array.
That one little macro could vastly improve readability of code that does a lot of array operations.

I'm wondering if anyone has written a comprehensive pre-processor for Objective-C/Cocoa to make is less wordy and more expressive?

A complete set of compact array operators would be helpful.

An implementation of even part of Ruby's string-processing features would be incredibly helpful. (Oh, the joy of just being able to concatenate strings with "+"...) With regular-expression support, it would be fantastic!

All it needs to do is parse the higher-level constructs and generate the corresponding incrediblyLongMethodNamesWith:incrediblyLongParame terNames...
jtara is offline   Reply With Quote
Old 04-18-2009, 03:31 AM   #5 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 6
johnno is on a distinguished road
Default

Hi jtara,

I considered C macros, but the problem is you can't assign to them:

x(y) = object; // would not work.

I even considered looking at the gnu compiler itself but the elements are already there in Xcode to introduce array and string operators to Objective-C by using the C++ part of the gnu compiler as a form of preprocesor. By wrapping XCode pointers in a class you can re-define any operator you like:

class OOString {
NSString *str;
public:
OOString( NSString *aString ) {
str = [aString retain];
}
OOString operator + ( NSString *otherString ) {
return OOString([str stringByAppendingString: otherString]);
}
~OOString() {
[str release];
}
}

The same goes for the subscript operator and arrays:

class OOArray {
NSArray *arr;
public:
OOArray( NSArray *anArray ) {
str = [anArray retain];
}
id operator [] ( int index ) {
return [arr objectAtIndex:index];
}
~OOArray() {
[arr release];
}
}

ObjCpp.h ("Objective-C pre-processor" or "Objective-C plus plus") is a little more involved than this but the idea of wrapping the pointer is the same. It also handles recursive indexing, creating arrays along the way as languages such as Perl and Ruby do for data structures:

a[0][1][33] = anObject;

While overuse of operator overloading can obfiscate code, Xcode is at the other extreme at times with their incredibly long method names!

John



Quote:
Originally Posted by jtara View Post
That one little macro could vastly improve readability of code that does a lot of array operations.

I'm wondering if anyone has written a comprehensive pre-processor for Objective-C/Cocoa to make is less wordy and more expressive?

A complete set of compact array operators would be helpful.

An implementation of even part of Ruby's string-processing features would be incredibly helpful. (Oh, the joy of just being able to concatenate strings with "+"...) With regular-expression support, it would be fantastic!

All it needs to do is parse the higher-level constructs and generate the corresponding incrediblyLongMethodNamesWith:incrediblyLongParame terNames...

Last edited by johnno; 04-18-2009 at 03:36 AM.
johnno is offline   Reply With Quote
Old 04-18-2009, 03:43 AM   #6 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 234
warmi is on a distinguished road
Default

Quote:
Originally Posted by jtara View Post
I'm wondering if anyone has written a comprehensive pre-processor for Objective-C/Cocoa to make is less wordy and more expressive?
Yeah, it is called C++.
warmi is offline   Reply With Quote
Old 04-18-2009, 03:50 AM   #7 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 234
warmi is on a distinguished road
Default

Quote:
Originally Posted by johnno View Post
Hi jtara,



I even considered looking at the gnu compiler itself but the elements are already there in Xcode to introduce array and string operators to Objective-C by using the C++ part of the gnu compiler as a form of preprocesor. By wrapping XCode pointers in a class you can re-define any operator you like:
The problem with this approach is that operators tend to be used heavily and wrapping them in another layer will make things even slower ( with Objective C not being a speed deamon in the first place.)

In other words, what you could accomplish with an inline method evaluating to 1 or 2 asm intructions in C++ is going to take two calls in Objective C.
warmi is offline   Reply With Quote
Old 04-18-2009, 04:09 AM   #8 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 6
johnno is on a distinguished road
Default

Hi warmi,

Speed shouldn't be an issue as all the operators are written as "inline" functions so it will not slow things down at run time. The code generated is all code the developer would have to write longhand anyway so the net result is just to off-load the programmer slightly.

If you have a need for speed consider using the native C++ OOVector container included as it bypasses NSArray altogether. Its pretty much the same as std::Vector but uses Xcode retain counts to manage correct destruction with scope outside a block of code.

I was concerned about compile time but even on my humble Mac-Mini this doesn't seem to be an issue. As a bonus using Objective-C++ mode to compile in Xcode is also a little stricter than straight Objective-C, for example: writing NSLog( "Hello World" ) rather than NSLog( @"Hello World" ) is an error rather than a warning.

John.

Quote:
Originally Posted by warmi View Post
The problem with this approach is that operators tend to be used heavily and wrapping them in another layer will make things even slower ( with Objective C not being a speed deamon in the first place.)

In other words, what you could accomplish with an inline method evaluating to 1 or 2 asm intructions in C++ is going to take two calls in Objective C.

Last edited by johnno; 04-18-2009 at 04:18 AM.
johnno is offline   Reply With Quote
Old 04-18-2009, 05:02 AM   #9 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 234
warmi is on a distinguished road
Default

Quote:
Originally Posted by johnno View Post
Hi warmi,

Speed shouldn't be an issue as all the operators are written as "inline" functions so it will not slow things down at run time. The code generated is all code the developer would have to write longhand anyway so the net result is just to off-load the programmer slightly.

If you have a need for speed consider using the native C++ OOVector container included as it bypasses NSArray altogether. Its pretty much the same as std::Vector but uses Xcode retain counts to manage correct destruction with scope outside a block of code.

I was concerned about compile time but even on my humble Mac-Mini this doesn't seem to be an issue. As a bonus using Objective-C++ mode to compile in Xcode is also a little stricter than straight Objective-C, for example: writing NSLog( "Hello World" ) rather than NSLog( @"Hello World" ) is an error rather than a warning.

John.
I doubt gcc would inline a C++ method which in turn calls another Objective C method ... in any case, I would never use Objective C for anything else but GUI glue code.
Frankly, the prefered approach is to simply wrap API specific stuff in some nice C++ classes and proceed from there ... there is nothing Apple base framework provides that cannot be better accomplished with std or boost.
warmi is offline   Reply With Quote
Old 04-20-2009, 08:41 AM   #10 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 19
sojourner is on a distinguished road
Thumbs up

Quote:
Originally Posted by warmi View Post
I doubt gcc would inline a C++ method which in turn calls another Objective C method ... in any case, I would never use Objective C for anything else but GUI glue code.
Frankly, the prefered approach is to simply wrap API specific stuff in some nice C++ classes and proceed from there ... there is nothing Apple base framework provides that cannot be better accomplished with std or boost.
I am new here and to MAC OS X / iPhone / Objective-C (and this is my first post on the board). Last week I received my mac and installed xcode on the weekend. Spent some time with objective-c books and xcode environment. However, I am not new to C++. Have coded in C++ for about 10 years and used the STL and BOOST before they become vogue. Multi-threaded servers, COM, DCOM, and some *nix programs were mostly what I have worked on.

The short time I have spent in investigating directs me to what you are asserting. IMO, once one understands and utilizes the power of C++ with the standard libraries (including STL), and BOOST, everything else is found lacking in some way or another.

As I get some experience under my belt with cocoa touch and using C++ with Objective-C in the environment, I hope to contribute to the forum. In the meantime, I'll be plugging away and happy that someone is using C++ and only Objective-C at the API touch points.

Cheers!
sojourner is offline   Reply With Quote
Old 04-20-2009, 09:43 AM   #11 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 234
warmi is on a distinguished road
Default

Quote:
Originally Posted by sojourner View Post
I am new here and to MAC OS X / iPhone / Objective-C (and this is my first post on the board). Last week I received my mac and installed xcode on the weekend. Spent some time with objective-c books and xcode environment. However, I am not new to C++. Have coded in C++ for about 10 years and used the STL and BOOST before they become vogue. Multi-threaded servers, COM, DCOM, and some *nix programs were mostly what I have worked on.

The short time I have spent in investigating directs me to what you are asserting. IMO, once one understands and utilizes the power of C++ with the standard libraries (including STL), and BOOST, everything else is found lacking in some way or another.

As I get some experience under my belt with cocoa touch and using C++ with Objective-C in the environment, I hope to contribute to the forum. In the meantime, I'll be plugging away and happy that someone is using C++ and only Objective-C at the API touch points.

Cheers!
Well, it depends :-)

I am lucky enough not to have to worry about Apple GUI and other classes as I am developing using OpenGL ES which means in my code , given about 100 classes, only 4 or 5 are mixed Objective-C , C++ - everything else is pure C++.
Of course, the price I pay for this is not being able to use any of Apple GUI stuff and handling everything thru OpenGL ( which is pretty much what generally is done when doing games anyway).


If you are planning to use iPhone GUI extensively then you will still most likely need to go heavily into Objective-C and related stuff and then there is no escaping their framework classes because a lot of it is built on top of them ( APIs expecting their custom collections classes as parameters etc.)
warmi is offline   Reply With Quote
Old 04-23-2009, 08:15 AM   #12 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 19
sojourner is on a distinguished road
Default

Quote:
Originally Posted by warmi View Post
Well, it depends :-)

I am lucky enough not to have to worry about Apple GUI and other classes as I am developing using OpenGL ES which means in my code , given about 100 classes, only 4 or 5 are mixed Objective-C , C++ - everything else is pure C++.
Of course, the price I pay for this is not being able to use any of Apple GUI stuff and handling everything thru OpenGL ( which is pretty much what generally is done when doing games anyway).


If you are planning to use iPhone GUI extensively then you will still most likely need to go heavily into Objective-C and related stuff and then there is no escaping their framework classes because a lot of it is built on top of them ( APIs expecting their custom collections classes as parameters etc.)
After spending a night with a book, xcode and interface builder I am totally disabused of the erroneous notion that I can minimize objective c.

I did find a great document for people new to objective-c with a strong C++ background. It was written in French and translated to English.
Theocacao: Objective-C Guide for C++ Programmers I must say it is a good reference.
sojourner is offline   Reply With Quote
Old 08-15-2009, 01:15 PM   #13 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 6
johnno is on a distinguished road
Default

This header file has been updated for iPhone OS 3.0 ObjCpp.h.
johnno is offline   Reply With Quote
Old 10-16-2009, 10:08 AM   #14 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 6
johnno is on a distinguished road
Default

objcpp.h has been updated to version 2.1.

The advantages of mixing in a little C++ again as I see it:

Better memory management through object life-cycle management.
Stricter type checking applied during compilation.
More powerful syntax than straight Objective-C.

This isn't an attempt to minimise Objective-C but compilent it wrapping foundation classes to reduce some of the verbiage of Objective-C code. To demonstrate the approach objcpp.h has been extended to include regular expressions, database persistence and xml document parsing along with two example iPhone applications.

Feedback as before to objcpp@johnholdsworth.com.


John

Last edited by johnno; 10-17-2009 at 01:02 AM.
johnno is offline   Reply With Quote
Old 01-01-2012, 12:23 PM   #15 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 6
johnno is on a distinguished road
Default

Objcpp has been updated for Xcode 4.

ObjCpp.h

John
johnno is offline   Reply With Quote
Reply

Bookmarks

Tags
c++, gcc, xcode

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 321
7 members and 314 guests
blueorb, guusleijsten, jbro, Kryckter, mer10, n00b, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:58 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0