使用C++模板框架在Java中

huangapple go评论92阅读模式
英文:

Using C++ template framework in Java

问题

以下是翻译好的内容:

我目前正在进行一个学校项目,我们在C++中创建了一个模糊逻辑框架(FuzzyLogicFramework)。现在,我尝试在Java中使用这个框架,以便在Android应用程序中使用该框架。在C++项目中,我有一个名为 FuzzyFactory 的工厂类。其方法的原型如下:

Expression<T>* newAnd(Expression<T>*, Expression<T>*);

我并不一定要使用所有可用的类型,所以我创建了一个类,类似于这样:

template<> class FuzzyFactory<float>;

我使用这个类生成了一个DLL库。

我的问题如下:
我想在一个Java文件中使用这个库:我应该使用什么技术?
我听说过JNI,并且能够使用它运行基本函数,但我能够管理对象实例化吗?我如何在Java中将指针传递给我的函数参数?我能够使用 JNI 在Java中运行我的C++项目主函数,但似乎在其中使用一个工厂类已经超出了我的能力范围。

我还听说过 wrappersJNA

我只需要有人告诉我一个可以使用的技术,这是我第一次尝试跨语言实现,所以我有点不知所措。

如果您想了解项目的当前状态,可以访问:https://gitlab.com/MelvinC/languageframeworkproject-ensisa-2020

我的Java项目和C++项目应该在同一个git仓库中吗?

如果我的英语表达不够好,我为此道歉。

非常感谢您提前阅读并帮助我。

英文:

I'm currently on a school project, we made a FuzzyLogicFramework in C++.
Now, I try to use this framework in Java in order to do an android app using the framework, I have a factory called FuzzyFactory in the c++ project.
the prototype for its methods are like this :
Expression&lt;T&gt;* newAnd(Expression&lt;T&gt;*, Expression&lt;T&gt;*);

I don't necesserly want to use all the avaiable types that is why I created a class like this :

template&lt;&gt; class FuzzyFactory&lt;float&gt;;

I generated a DLL library with this class.

My questions are the following:
I want to use this library in a Java file : what technology could I use?
I heard about JNI and was able to run basic functions with it, but would I be able to manage objects instanciation ? How would I be able to give pointers in my functions arguments in Java ? I was able to run my c++ project main in Java using JNI, but it seems using a Factory class with it is on another level.

I also heard about wrappers and JNA

I just need somebody to told me a technology to use, It's my first time trying to do cross language implementation so I'm a bit overwhelmed.

if you want to see a bit more about the current state of the project : https://gitlab.com/MelvinC/languageframeworkproject-ensisa-2020

Should my Java project and c++ project be in the same git repository ?

Sorry for my english, if it is not good enough.

Thank you in advance for reading and helping me.

答案1

得分: 0

SWIG对C++模板有一些支持,并且可以从C++头文件生成Java绑定代码。这将为您提供一个非常底层的界面,但希望是可用的接口。

或者,您可能希望考虑一些外部表示逻辑程序的方法。然后,您可以使用普通的Java代码构造程序,然后将其序列化为外部表示,并将其传递到C++端。
作为灵感,Z3 SMT求解器接受一种称为SMT-LIB的表示形式。
或者,您可以使用S表达式、XML甚至只是嵌套的JSON来创建您自己的格式。

英文:

SWIG has some support for C++ templates and can generate Java binding code from C++ headers. That will give you a very low-level, but hopefully usable interface.

Alternatively, you might want to consider some sort of external representation of your logic program. You can then use plain Java code to construct the program, then serialize it into the external representation and pass it to the C++ side.
For inspiration, the Z3 SMT solver accepts a representation called SMT-LIB.
Alternatively, you can create your own format using S-expressions, XML, or even just nested JSON.

huangapple
  • 本文由 发表于 2020年4月6日 03:34:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61048349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定