Hello,

Trying to create ldap bindings I found the following questions in my head:

I was trying to make bindings not just to make `ptr ptr Ldap` or `pointer` but 
reduce amount of work to wrap it later

On the C-side:
    
    
    typedef struct ldap LDAP;
    int ldap_initialize(ldp **LDAP, const char *url);
    
    
    Run

It is pretty standard for C to have double ref to initialize something on 
pointer

The first idea was to make simple, but wrong because Nim does not expect that 
ref can be changed
    
    
    type LdapRef = ref object
    proc ldap_initialize(ldp: var LdapRef, url: csring) {.importc.}
    
    var ldp: LdapRef
    ldap_initialize(ldp, "url")
    
    
    Run

But the second idea became more complicate:
    
    
    type Ldap = object
    type LdapInit = object
      r: ptr Ldap
    
    proc ldap_initialize(ldp: var LdapInit, url: csring) {.importc.}
    
    var ldpInit: LdapInit
    ldap_initialize(ldpInit, "url")
    
    
    Run

or to remove var and make LdapInit = ref object

Because we own the Ldap structure we are responsible to free it, and I added
    
    
    proc `=destroy`(x: var BerVal) =
      ldap_free x.r # the same like simple free
    
    
    Run

The question is that the second way looks +- ok, but a bit overcomplicated for 
the simple problem.

Questions are:
    

  * Is it possible to make it without additional LdapInit structure but without 
`ptr ptr` also?
  * Is it possible to own the ptr from Nim to make the destructor unnecessary 
and Nim will free it automatically?


Reply via email to