数据以本机代码存储 [英] Data storing in native code

查看:58
本文介绍了数据以本机代码存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C ++上编写向量类(或其他).我想从Java使用它的方法.这该怎么做?我想为此目的使用JNI.但是javah为我生成了c原型.我想将数据存储在C ++使用区域中,并且仅在Java中使用没有字段的接口.因此,问题在于如何将矢量数据存储在C代码中.

I want to write a vector class (or something other) on C++. And I want to use its methods from Java. How to do this? I want to use JNI for this purpose. But javah generate me c prototypes. I want to store data in the C++ use area and use in java only an interface without fields. So, the problem is how to store the vector data in C-code.

注意:

简单来说,我需要使用JNI包装C ++接口,并在Java中使用此接口.

Simply speaking, I need to wrap C++ interface by JNI and to have this interface in Java.

推荐答案

您将需要在C ++代码周围使用C调用包装器.这种方法类似于允许纯C语言使用C ++库的方式.这是一个简单的示例(完全没有错误检查-请勿用于实际生产代码):

You will need to have a C-callable wrapper around your C++ code. The approach is similar to how you would allow a C++ library to be usable from pure C. Here is a simple example (with no error checking at all -- do not use for real production code):

void * MyVectorCreate()
{
    return new MyVector<int>();
}

void MyVectorAdd(void * vector, int item)
{
    static_cast<MyVector<int> *>(vector)->Add(item);
}

void MyVectorDestroy(void * vector)
{
    delete static_cast<MyVector<int> *>(vector);
}

这篇关于数据以本机代码存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆