views:

267

answers:

5

I have an application written in C++ (makes heavy use of templates) that I need to take to the Java ME platform.

I have two questions:

  1. Are there any good tools to convert C++ code to Java - do some basic stuff so I have a platform to start with. I found this - http://tangiblesoftwaresolutions.com/Product_Details/CPlusPlus_to_Java_Converter_Details.html. It would at least eliminate the need for simple but time-consuming stuff like moving classes to different files, namespaces to packages, etc. Has anyone tried it? Or knows of any better ones?

  2. The bigger problem is how to deal with templates - the code makes very heavy use of them. Any suggestions on how to go about this? Are there any tools to expand templates for instance so I have a rudimentary base and then I could work on writing the modules in Java?

Any help would be appreciated.

A: 

Generics is the Java feature that corresponds to C++ templates and they are not supported in J2ME. You can use them with the aid of a framework, which probably uses pre-processing to do the trick. (Actually Generics in Java is a compiler feature - the JVM knows nothing about them.)

Anyway, it will be difficult if not impossible to automatically port even a small portion of your code form C++ to Java Standard Edition - things are much worse with J2ME. There are many and important differences between Java Generics and C++ templates.

kgiannakakis
Java generics are hardly equivalent to C++ templates.
avakar
I didn't meant there are one-to-one replacements. I meant that Java Generics are used to solve the same set of problems that in C++ are solved with templates.
kgiannakakis
Still very wrong. Generics is not code generation, templates are. Generics can be applied to a subset of the problems that c++ templates are used for. In c++, templates are often used together with operator overloading. Have a look at stuff like tr1::auto_ptr, or the eigen2 library, for stuff that you can't really do with Java generics.
gnud
I know that generics and c++ templates are very different, in fact I even wrote that in my answer. Read the last sentence too, not only the first one. When someone asks how to do C++ template like functionality in Java, then Generics is the answer. C# generics, Java generics and C++ are commonly compared - it doesn't mean that they are of equal power. Wikipedia covers Java generics and C++ templates in a single article.
kgiannakakis
I replaced "equivalent" in order to avoid confusion.
kgiannakakis
+9  A: 

For all of Sun's marketing, Java is not simply a better C++, and in fact does not support many of the idioms and paradigms C++ supports. This makes automated translation difficult. How should you automatically turn a multi-inheritance hierarchy into Java's single inheritance hierarchy? (Note, I am not saying that a multi-inheritance hierarchy is a good thing, only that it is explicitly allowed in C++). More fundamentally, how would you represent a pointer-to-member function in Java? Or handle the differences between Java and C++ overload resolution?

Java added generics a few years ago, but purposely made them less powerful than C++ templates. Regardless of whether that was a good idea, it limits what automated translation can do with heavily templatized code.

Aside from using some research compiler that can turn C++ into Java bytecode, I'm afraid you may be stuck doing the translation by hand.

Max Lybbert
A: 

I think for your case a very simple tool would be possible and perhaps worthwhile. Might be a fun weekend-job, though! A friend of mine once did a port from C++ to Java and he just made a list of regular expression substitutions. Like, he had all occurrences of -> replaced by a dot. And so forth. This was some years ago, however, so I don't really feel like asking him.

So, you could do the same, collect some easy substitutions and perhaps publish them somewhere on github?

nes1983
Um, you are gravely underestimating the task
I think I'm just being realistic. You can't parse C++, build a tree out of it and print the same tree as Java. You need to do most of it by hand. And you can simplify it by a tool. But you can't just convert and hit the compile button.
nes1983
+1  A: 

Can you use JNI and call the old C++ code from the new Java code?

zooropa
Unfortunately, JNI is not an option - only pure Java.
Neha
+1  A: 

I don't think it's going to be possible, esp. if your original code is heavily templatized - J2ME doesn't support generics, AFAIK.

Unfortunately, it seems like this will require a lot of manual work to go through the original code and rewrite it (I'm assuming your target platform doesn't support JNI)

zorder