0

I'm trying to use czmq with scala-native and I haven't found a way to create pointer in scala-native.

here is 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
}

and here is my simple main method:

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
  }
}

So the question is how to make push and pull variables pointers like in c with & ?

1 Answer 1

2

I've reached out to scala-native team on gitter and the answer is to use 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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.