感谢 @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/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option