On 11 May 2012 01:02, Igor Stasenko <siguctua@gmail.com> wrote:
On 11 May 2012 00:33, Lawson English <lenglish5@cox.net> wrote:
On 5/10/12 2:12 PM, Igor Stasenko wrote:
oops. i didn't meant to turn it into private conversation.. there are some bits which can be useful/interesting to others :)
---------- Forwarded message ---------- From: Igor Stasenko<siguctua@gmail.com> Date: 10 May 2012 23:07 Subject: Re: [Pharo-project] Native Boost and Slang To: lenglish5@cox.net
On 10 May 2012 22:07, Lawson English<lenglish5@cox.net> Â wrote:
On 5/10/12 12:00 PM, Igor Stasenko wrote:
woot .. 68 videos.. you are really productive! :)
Thanks.
BTW, I was wondering if there would be an easy way to return the formatted string from a printf() call.
printing: Â NBCPrinter printf: 'aString'
returns nil
because printf() function returns void. (nothing) which replaced by NB by nil (since unlike from C, every method has to answer something)
for returning formatted string you can use snprintf() function.
  int   snprintf(char * restrict str, size_t size, const char * restrict format, ...);
here in first argument you should pass a ByteArray instance, or ByteString, big enough to accomodate formatted string, size is maximum number of characters for that buffer (pass the size of bytearray/string) the rest is like in printf. But NB does not supports variable arguments.
Will NativeBoost automatically handle the room for '\0' Â in the buffer (char * restrict str) or should really pass in str size+1?
nope. you should pass a buffer , big enough to hold everything.
Also, I'm looking at providing bindings to a few of the functions in gmplib  http://gmplib.org/
and the examples http://web.mit.edu/gnu/doc/html/gmp_4.html show that I should always call mpz_clear (&result) unless I am "exiting the program". I assume this means I should always call mpz_clear (&result) but of course, if I do that, I don't have a valid return value.
you clear it , when you no longer need it. in terms of smalltalk, you must finalize the object which represents external resource to call mpz_clear there. See WeakRegistry& Â friends.
You can also use NBExternalResourceManager, which i added recently. See its comment for instructions how to use it.
Can I embed several external calls within a single {} or should I make each gmp call a "private" method and wrap the calls in a public method or...?
It is easy to write a code in NB which makes multiple calls to different functions inside same primitive. But of course you will need to make your hands dirty with some assembly.
The high level interface allows you to make callouts only to a single function.
OK, I'm starting to see, but not sure how to pass IN a struct that I have defined to an external function.
E.G. Â how would I create a method for
void mpz_int(&num)
where num is a structure defined below?
GmpMpzT>>fieldsDesc " self createAccessors " Â Â ^ #(
    long mpAlloc;     long mpSize;     ulong mpLimbT;     )
best method is by using copy & paste:
MyClass >> mpz_int: x  <primitive ... > "Function: void mpz_init (mpz_t x)" ^ self nbCall: #( void mpz_init (mpz_t x))
just don't forget to declare mpz_t alias.. either by using classvar/poolvar
mpz_t := GmpMpzT.
or
MyClass >> externalTypeAlias: typename  typename = 'mpz_t' ifTrue: [ ^ 'GmpMpzT' ].  ^ nil
(actually i never use #externalTypeAlias:, since pool var way is much more convenient.)
and of course you can do it more object-oriented way: GmpMpzT>>initialize self mpz_init. NBExternalResourceManager addResource: self data: self copy copy because otherwise it will be never GCed. and this may not work, if clear method relies on data in struct fields which may change when you using it (check it's C implementation). otherwise, you will need to keep struct instance as an inst var in your wrapper class and finalize the wrapper instead. GmpMpzT>>mpz_init <primitive ... > "Function: void mpz_init (mpz_t x)" ^ self nbCall: #( void mpz_init (self)) "see the difference" GmpMpzT class>>finalizeResourceData: mpz mpz mpz_clear. GmpMpzT>>mpz_clear <primitive ... > "Function: void mpz_init (mpz_t x)" ^ self nbCall: #( void mpz_clear (self)) something like that...
-- Best regards, Igor Stasenko.
-- Best regards, Igor Stasenko.