gpt4 book ai didi

java - 回调作为 C 结构的参数 - Java 包装器生成

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:30:20 24 4
gpt4 key购买 nike

感谢 @flexo,当自由函数作为参数传递给另一个函数时,我对简单回调没有任何问题。 .

但假设更难的 C 接口(interface):

typedef struct
{
int id;
const char* name;
} Item;

typedef struct
{
int value;
Items_Callback callback;
void *context;
} Items_Call;

typedef int (*Items_Callback)(const Item *item, void *context);

int Items_create(const Item *item, Items_Call *call) {
...
call->callback(item, call->context);
...
}

我打算为这样的代码生成一些不错的 Java 包装器。我假设结果是

class Item {
public int id;
public String name;
}

class Items_Call {
public int value;
public Object context;
public Interface callback;
public void setInterface(Interface i){ callback=i; };
}

public interface Interface {
public int Items_Callback(Item item, Object context);
}

int Items_create(Item item, Items_Call call) {
...
call.callback.Items_Callback(item, call.context);
...
}

我意识到 SWIG 在生成纯 Java 接口(interface)方面存在一些问题,但我相信这不是主要问题。问题是我不知道如何将这种嵌套结构重新解释为可接受的 Java 代码。

最佳答案

不是 SWIG,但以下适用于 JavaCPP (它不带有 JNA 的那种开销,并且在 JNI 工作的任何地方工作):

// items.h
typedef struct
{
int id;
const char* name;
} Item;

typedef int (*Items_Callback)(const Item *item, void *context);

typedef struct
{
int value;
Items_Callback callback;
void *context;
} Items_Call;

int Items_create(const Item *item, Items_Call *call) {
// ...
call->callback(item, call->context);
// ...
return 0;
}

在 Java 中:

import com.googlecode.javacpp.*;
import com.googlecode.javacpp.annotation.*;

@Platform(include="items.h")
public class Items {
static { Loader.load(); }

public static class Item extends Pointer {
public Item() { allocate(); }
private native void allocate();

public native int id(); public native Item id(int id);
@Cast("const char*")
public native BytePointer name(); public native Item name(BytePointer name);
}

public static class Items_Callback extends FunctionPointer {
protected Items_Callback() { allocate(); }
private native void allocate();

public native int call(@Const Item item, Pointer context);
}

public static class Items_Call extends Pointer {
public Items_Call() { allocate(); }
private native void allocate();

public native int value(); public native Items_Call value(int value);
public native Pointer context(); public native Items_Call context(Pointer context);
public native Items_Callback callback(); public native Items_Call callback(Items_Callback value);

public void setInterface(Items_Callback i) { callback(i); }
}

public static native void Items_create(Item item, Items_Call call);

public static void main(String[] args) {
BytePointer s = new BytePointer("Hello");
Item i = new Item();
i.id(42);
i.name(s);

Items_Callback cb = new Items_Callback() {
public int call(Item item, Pointer context) {
System.out.println(item.id() + " " + item.name().getString());
return 0;
}
};
Items_Call ic = new Items_Call();
ic.callback(cb);

Items_create(i, ic);

// if we remove these references, the GC may prematurely deallocate them
s.deallocate();
cb.deallocate();
}
}

输出预期结果:

42 Hello

免责声明:我是 JavaCPP 的作者 :)

关于java - 回调作为 C 结构的参数 - Java 包装器生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20692053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com