Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle C typedef on different platform using NativeCall

Tags:

c

raku

nativecall

Is there a convenient way handle C typedef which may have different value on various platform ?

For example

#if defined(_WIN32)
    #define foo_t int32_t
#elif defined(_WIN64)
    #define foo_t int64_t
#else
    #define foo_t long
#endif

void handle_foo(foo_t* in) {
    ...
}

Currently I am handle this in Perl 6 like this

sub handle_foo32(int32 is rw) is native(Str) { * }
sub handle_foo64(int64 is rw) is native(Str) { * }
sub handle_foo00(long  is rw) is native(Str) { * }

sub handle-foo(Int $in) {
    if $*DISTRO.is-win {
        given $*KERNEL.bits {
            when 32 {
                handle_foo32(my int32 $ = $in);
            }
            when 64 {
                handle_foo64(my int64 $ = $in);
            }
        }
    } else {
        handle_foo00(my long $ = $in);
    }
}
like image 494
araraloren Avatar asked May 21 '18 12:05

araraloren


2 Answers

Perhaps the most direct translation is to use constant to introduce a new symbol, computed at compile time.

my constant foo_t = do if $*DISTRO.is-win {
    given $*KERNEL.bits {
        when 32 { int32 }
        when 64 { int64 }
    }
} else {
    long
}

A constant bound to a type object acts just like a type, and can be used in the signature.

sub handle_foo(foo_t is rw) is native { * }
like image 73
Jonathan Worthington Avatar answered Oct 21 '22 03:10

Jonathan Worthington


I don't think so. But since $*DISTRO.is-win and $*KERNEL.bits are effectively constants, you can move the decision on which version to use to compile time:

sub handle_foo32(int32 is rw) is native(Str) { * }
sub handle_foo64(int64 is rw) is native(Str) { * }
sub handle_foo00(long  is rw) is native(Str) { * }

my constant &handler = $*DISTRO.is-win
  ?? $*KERNEL.bits == 32
    ?? &handle_foo32
    !! &handle_foo64
  !! &handle_foo00;

Except this won't of course help you with the differently sized parameters for each version. So I guess you would need another layer of indirection for that.

Perhaps as inspiration, here's how I handled similar issues: https://github.com/lizmat/P5getpwnam/blob/master/lib/P5getpwnam.pm6

like image 27
Elizabeth Mattijsen Avatar answered Oct 21 '22 01:10

Elizabeth Mattijsen