我正在尝试将czmq与scala-native一起使用,但我还没有找到在scala-native中创建指针的方法。
下面是extern:
@native.link("czmq")
@native.extern
object czmq {
//struct _zsock_t {
// uint32_t tag; // Object tag for runtime detection
// void *handle; // The libzmq socket handle
// char *endpoint; // Last bound endpoint, if any
// char *cache; // Holds last zsock_brecv strings
// int type; // Socket type
// size_t cache_size; // Current size of cache
// uint32_t routing_id; // Routing ID for server sockets
//};
type zsock_t = native.CStruct7[
native.CUnsignedLongLong,
native.Ptr[Byte],
native.CString,
native.CString,
native.CInt,
native.CSize,
native.CUnsignedLongLong]
def zsock_new_push(endpoint: native.CString): native.Ptr[zsock_t] = native.extern
def zsock_new_pull(endpoint: native.CString): native.Ptr[zsock_t] = native.extern
def zstr_send(dest: native.Ptr[Unit], str: native.CString): native.CInt = native.extern
def zstr_recv(src: native.Ptr[Unit]): native.CString = native.extern
//void zsock_destroy (zsock_t **self_p)
def zsock_destroy(self_p: native.Ptr[native.Ptr[zsock_t]]):Unit = native.extern
}下面是我的简单main方法:
object Main {
def main(args: Array[String]): Unit ={
val push = czmq.zsock_new_push(c"inproc://example")
val pull = czmq.zsock_new_pull(c"inproc://example")
czmq.zstr_send(push.cast[Ptr[Unit]], c"Hello World")
val s = fromCString(czmq.zstr_recv(pull.cast[Ptr[Unit]]))
println("msg: "+s)
czmq.zsock_destroy(push) // doesn't compile
czmq.zsock_destroy(pull) // doesn't compile
}
}所以问题是如何让push和pull变量像在c++和&中一样成为指针?
发布于 2017-05-08 11:23:22
我已经联系了gitter上的原生scala团队,答案是使用stackalloc。
val ppush = stackalloc[Ptr[czmq.zsock_t]]
val ppull = stackalloc[Ptr[czmq.zsock_t]]
!ppush = push
!ppull = pull
czmq.zsock_destroy(ppush)
czmq.zsock_destroy(ppull)https://stackoverflow.com/questions/43730091
复制相似问题