简体   繁体   English

从Java按名称加载C ++类

[英]Load C++ class by name from Java

Let's assume I have a framework written in Java and some C++ code which does resource intensive work. 假设我有一个用Java和一些C ++代码编写的框架,这些框架可以完成资源密集型工作。 — The framework initializes a processing chain based on a database configuration. —框架根据数据库配置初始化处理链。 The processing units (of this chain) are written in C++. (此链的)处理单元用C ++编写。 Every unit implements the following interface: 每个单元都实现以下接口:

class IModule {
public:
    virtual ~IModule() {};
    virtual bool setConfig(ModConfig* config) = 0;
    virtual map<string*,string*>* getStatus() = 0;
};

I want that developers are able to implement the interface IModule in C++ and make a database entry containing the name of the class. 我希望开发人员能够在C ++中实现接口IModule,并创建一个包含类名称的数据库条目。 The Java framework then automatically loads that class. 然后,Java框架会自动加载该类。 — The objective must not be to write additional binding code neither in C++ nor in Java. —目的绝不能是既不使用C ++也不使用Java编写附加的绑定代码。 As you can see in the interface the method setConfig() receives an object of type ModConfig. 正如您在界面中看到的那样,方法setConfig()接收类型为ModConfig的对象。 Meaning that it must be possible to make an instance of that C++ object in Java. 这意味着必须有可能在Java中创建该C ++对象的实例。

I evaluated the following technologies: - JNA: C only -> needs addition binding code - JNIEasy: maps object to object directly - SWIG: maps object to object directly - BridJ: maps object to object directly - JNI: maybe a solution for the problem? 我评估了以下技术:-JNA:仅C->需要附加绑定代码-JNIEasy:直接将对象映射到对象-SWIG:直接将对象映射到对象-BridJ:直接将对象映射到对象-JNI:也许是该问题的解决方案?

Summary: - Load C++ class by name from Java. 摘要:-从Java中按名称加载C ++类。 - Instantiate C++ object in Java. -在Java中实例化C ++对象。

I don't expect anybody to provide me with code. 我不希望有人为我提供代码。 Just point me in the right direction (technology). 只要指出正确的方向(技术)即可。

Thanks in advance 提前致谢

Your question is deeply operating system specific (or I misunderstood it). 您的问题是特定于操作系统的(或者我误解了)。 I don't understand what loading a C++ class at runtime means to you (it certainly is not possible in pure C++11, you need operating system support). 我不明白在运行时加载C ++类对您意味着什么(在纯C ++ 11中当然不可能,您需要操作系统支持)。

You could load a dynamically linkable shared object on Posix systems with dlopen then get a symbol's address in it with dlsym . 您可以使用dlopen在Posix系统上加载可动态链接的共享库,然后使用dlsym在其中获取符号的地址。 Don't forget to declare extern "C" the C++ functions you want to find. 不要忘记将extern "C"声明为要查找的C ++函数。

If you are concerned about C++ classes, then look perhaps also in Qt's Qlibrary and QPluginLoader for inspiration. 如果您担心C ++类,那么也可以在Qt的QlibraryQPluginLoader中寻找灵感。

Read more about plug-ins . 阅读有关插件的更多信息。

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

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