Covert a complex C header to D

biocyberman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 2 14:43:52 PDT 2017


khash.h 
(http://attractivechaos.github.io/klib/#Khash%3A%20generic%20hash%20table) is a part of klib library in C. I want to covert it to D in the process of learning deeper about D.

First I tried with Dstep 
(https://github.com/jacob-carlborg/dstep) and read the C to D 
article (https://dlang.org/ctod.html). I managed to covert the 
basic statements to D, but all multiline 'define' macros are 
stripped off. So I am trying to recreate them with D way. For 
example:


#define __KHASH_TYPE(name, khkey_t, khval_t) \
	typedef struct kh_##name##_s { \
		khint_t n_buckets, size, n_occupied, upper_bound; \
		khint32_t *flags; \
		khkey_t *keys; \
		khval_t *vals; \
	} kh_##name##_t;


I changed to:

template __KHASH_TYPE(string name){
   "struct  kh_" ~ name ~"_t { " ~
                 "khint_t n_buckets, size, n_occupied, 
upper_bound; " ~
                 "khint32_t *flags; " ~
                 "khkey_t *keys; " ~
                 "khval_t *vals; " ~
         "}"

}

// NEXT: use mixin with this template.

I am currently get a bit intimidated looking at KHASH_INIT2 macro 
in khash.c. How do I convert this to the equivalent and idiomatic 
D?





More information about the Digitalmars-d-learn mailing list