How to convert these constructs to D?

Rémy Mouëza remy.moueza at gmail.com
Sat Nov 30 08:42:02 PST 2013


For the two first ones, I would use an alias:
     alias GetCurrentThreadId pthread_self;
     alias void *  pthread_handler_t;

The third one is a function pointer alias:
     alias void * function (void *) pthread_handler;

For the last one, I am not sure, being rusty with the C preprocessor 
rules. I would use a template function:
     import core.stdc.config : c_long;

     void set_timespec_nsec (T, U) (ref T abstime, U nsec) {
        GetSystemTimeAsFileTime (& abstime.tv.ft);
        abstime.tv.i64 += cast (long) (nsec) / 100;
        abstime.max_timeout_msec = cast (c_long) (nsec /1000000);
     }
Though, with that last one, you lost the ability to get it inlined (but 
there is still the -inline computer switch if performance is a real issue).
As for the type mapping, it seems that:
  - __int64 in C corresponds to long in D
  - long in C corresponds to c_long defined in core.stdc.config.

Don't hesitate to double check all this, I may have written something wrong.

On 11/30/2013 03:53 PM, Gary Willoughby wrote:
> I'm porting some C headers and wondered how would i convert the
> following to D:
>
> #define pthread_self() GetCurrentThreadId()
>
> #define pthread_handler_t void * __cdecl
>
> typedef void * (__cdecl *pthread_handler)(void *);
>
> #define set_timespec_nsec(ABSTIME,NSEC) { \
>    GetSystemTimeAsFileTime(&((ABSTIME).tv.ft)); \
>    (ABSTIME).tv.i64+= (__int64)(NSEC)/100; \
>    (ABSTIME).max_timeout_msec= (long)((NSEC)/1000000); \
> }



More information about the Digitalmars-d-learn mailing list