Objective C to native C/C++

The key difference between native iOS apps and native BlackBerry 10 apps is the language that's used to write them. iOS apps are written in Objective C, while native BlackBerry 10 apps are written in C/C++. In Cascades apps, you can also use QML, and JavaScript. Because of the similarities between Objective C and C/C++, you can save time when you port your apps by using wrapping.

Wrapping is a way of using private implementations to wrap your Objective C types and functions in C++ equivalents. This approach allows you to use your Objective C code in pure C++ projects or pure C++ code in your Objective C projects. In the context of porting an app, wrapping allows you to use your existing application code and Objective C libraries in BlackBerry 10.

Wrapping only applies to porting Objective C to native C/C++. If you want to port your app to Cascades, check out, Objective C to Cascades.

The most important aspect of wrapping your code is making sure not to include Objective C object types in your C++ headers; otherwise the compiler throws an error when it builds the project.

Here is a quick look at how the wrapping process works.

  1. In the public header, add a forward declaration of the implementation struct.
  2. Use the struct type to hold the members you want to exclude from the public header.
  3. Add a pointer to the struct as a class instance variable.
  4. Define the members of the struct’s in the .cpp file.
  5. Construct an instance of the struct using the new operator.
  6. Set the instance variable to the newly created struct.
  7. Make sure delete is called on destruction.

This article on wrapping recommends that you disable Automatic Reference Counting (ARC) in your project when you decide to port your code. You can disable ARC in specific files in Xcode by adding -fno-objc-arc to the compiler flags in the Compile Sources section of the Build Phases tab.

Objective C to C/C++ equivalency list

The following table lists some Objective C to C/C++ equivalencies that can be a helpful reference when you wrap your code.

Objective C

C/C++

Libraries used

Foundation Framework

Standard Template Library, BlackBerry Platform Services, Qt, Cascades

Version

gcc 4.2

g++ 4.2

 

Hello World

#include <stdio.h>
int main(int argc, char **argv) {
  printf("Hello, World!\n");
}
#include <iostream>
using namespace std;
int main(int argc, char**arg) {
  cout << "Hello, World!" << endl;
}

File extensions

<file_name>.m
<file_name>.h
<file_name>.o
<file_name>.cpp
<file_name>.hpp
<file_name>.o

Local variable

int i;
int j = 3; 
int i;
int j = 3;
int k(7);

Uninitialized local variable

The behavior is undefined.

Most implementations do not set the intial value of stack variables, so the value is what is stored in memory.

The behavior is undefined.

Most implementations do not set the intial value of stack variables, so the value is what is stored in memory.

Constant 

const int i = 7;
 
const int i = 7;

Allocate heap

#include <stdlib.h> 

int *ip = malloc(sizeof(int));
 
int *ip = new int;
 

Free heap

#include <stdlib.h> 

free(ip);
 
delete ip;
 

Null

NULL

NULL

Coalesce

NSString *s1 = s2 || @"was null";
string s1 = s2 || "was null";

For a more comprehensive comparison list, see http://hyperpolyglot.org/cpp.

Last modified: 2013-12-21

comments powered by Disqus