简体   繁体   English

将严格模板化的C ++代码移植到Java

[英]Moving heavily templatized C++ code to Java

I have an application written in C++ (makes heavy use of templates) that I need to take to the Java ME platform. 我有一个用C ++编写的应用程序(大量使用模板),我需要将它带到Java ME平台。

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. 有没有什么好的工具可以将C ++代码转换为Java - 做一些基本的东西,所以我有一个平台可以开始。 I found this - http://tangiblesoftwaresolutions.com/Product_Details/CPlusPlus_to_Java_Converter_Details.html . 我找到了这个 - 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? 有没有任何工具来扩展模板,所以我有一个基本的基础,然后我可以用Java编写模块?

Any help would be appreciated. 任何帮助,将不胜感激。

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. 对于Sun的所有营销,Java不仅仅是一个更好的C ++,实际上并不支持C ++支持的许多习语和范例。 This makes automated translation difficult. 这使得自动翻译变得困难。 How should you automatically turn a multi-inheritance hierarchy into Java's single inheritance hierarchy? 您应该如何自动将多继承层次结构转换为Java的单继承层次结构? (Note, I am not saying that a multi-inheritance hierarchy is a good thing, only that it is explicitly allowed in C++). (注意,我并不是说多继承层次结构是好事,只是在C ++中明确允许它)。 More fundamentally, how would you represent a pointer-to-member function in Java? 更基本的是,您如何在Java中表示指向成员的函数? Or handle the differences between Java and C++ overload resolution? 或者处理Java和C ++重载决策之间的差异?

Java added generics a few years ago, but purposely made them less powerful than C++ templates. Java几年前添加了泛型,但故意使它们不如C ++模板强大。 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. 除了使用一些可以将C ++转换为Java字节码的研究编译器之外,我担心你可能会被困在手工翻译中。

您可以使用JNI并从新的Java代码中调用旧的C ++代码吗?

Generics is the Java feature that corresponds to C++ templates and they are not supported in J2ME. 泛型是与C ++模板对应的Java功能,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.) (实际上Java中的泛型是一种编译器功能--JVM对它们一无所知。)

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. 无论如何,即使不是不可能自动将C ++代码的一小部分自动移植到Java标准版也很困难 - 但J2ME的情况要糟糕得多。 There are many and important differences between Java Generics and C++ templates. Java Generics和C ++模板之间存在许多重要的差异。

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. 我的一个朋友曾经做过一个从C ++到Java的端口,他刚刚制作了一个正则表达式替换列表。 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? 所以,你可以这样做,收集一些简单的替换,并可能在github上的某个地方发布它们?

I don't think it's going to be possible, esp. 我认为这不可能,尤其是 if your original code is heavily templatized - J2ME doesn't support generics, AFAIK. 如果您的原始代码严重模板化 - J2ME不支持泛型,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) 不幸的是,似乎这需要大量的手工工作来完成原始代码并重写它(我假设你的目标平台不支持JNI)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM