digitalmars.D.learn - help on how to wrap a C macros when binding linux/spi/spidev.h
- dangbinghoo (50/50) Mar 29 2018 hi,
- dangbinghoo (3/7) Mar 29 2018 PS: if I just _IOW!(char[])... it compiles, but does it mean it's
- Nicholas Wilson (2/27) Mar 29 2018
- dangbinghoo (7/36) Mar 29 2018 thanks for your reply, but it will also fail, the compiler just
- dangbinghoo (3/18) Mar 29 2018 sorry, I just don't notice the `()`.
- dangbinghoo (3/23) Mar 29 2018 just got it! `size_t N` need to be template param.
hi, I'm doing a D binding myself to <linux/spi/spidev.h>, and has some problems to wrap the C macros. ``` import core.sys.posix.sys.ioctl; ... /* IOCTL commands */ enum SPI_IOC_MAGIC = 'k'; /** * Orginal C macros. * * #define SPI_MSGSIZE(N) \ ((((N)*(sizeof (struct spi_ioc_transfer))) < (1 << _IOC_SIZEBITS)) \ ? ((N)*(sizeof (struct spi_ioc_transfer))) : 0) */ extern (D) size_t SPI_MSGSIZE(size_t N) { return ((N * (spi_ioc_transfer.sizeof)) < (1 << _IOC_SIZEBITS)) ? (N * (spi_ioc_transfer.sizeof)) : 0; } /* #define SPI_IOC_MESSAGE(N) _IOW(SPI_IOC_MAGIC, 0, char[SPI_MSGSIZE(N)]) */ extern (D) auto SPI_IOC_MESSAGE(size_t N) { size_t n = SPI_MSGSIZE(N); return _IOW!(char[static n])(SPI_IOC_MAGIC, 0); //mixin("return _IOW!(char[n])(SPI_IOC_MAGIC, 0);"); } ``` as shown in the above code, the SPI_IOC_MESSAGE C macro has a param `char[SPI_MSGSIZE(N)]`, what should it be wrapped to the D world? as you can see, I tried to use the type of `char[static n]` or `char[n]`, and the compile failed: ``` source/spidev.d-mixin-129(129,19): Error: expression expected, not static source/spidev.d-mixin-129(129,26): Error: found n when expecting ] source/spidev.d-mixin-129(129,27): Error: found ] when expecting ) following template argument list source/spidev.d-mixin-129(129,28): Error: found ) when expecting ; following return statement ``` or when removed static, ``` source/spidev.d-mixin-129(129,8): Error: variable n cannot be read at compile time ``` Can anyone help me? Thanks!
Mar 29 2018
On Thursday, 29 March 2018 at 08:47:50 UTC, dangbinghoo wrote:hi, I'm doing a D binding myself to <linux/spi/spidev.h>, and has some problems to wrap the C macros. [...]PS: if I just _IOW!(char[])... it compiles, but does it mean it's unsafe? thanks!
Mar 29 2018
On Thursday, 29 March 2018 at 08:47:50 UTC, dangbinghoo wrote:* #define SPI_MSGSIZE(N) \ ((((N)*(sizeof (struct spi_ioc_transfer))) < (1 << _IOC_SIZEBITS)) \ ? ((N)*(sizeof (struct spi_ioc_transfer))) : 0) */ extern (D) size_t SPI_MSGSIZE(size_t N) { return ((N * (spi_ioc_transfer.sizeof)) < (1 << _IOC_SIZEBITS)) ? (N * (spi_ioc_transfer.sizeof)) : 0; } /* #define SPI_IOC_MESSAGE(N) _IOW(SPI_IOC_MAGIC, 0, char[SPI_MSGSIZE(N)]) */ extern (D) auto SPI_IOC_MESSAGE(size_t N) { size_t n = SPI_MSGSIZE(N); return _IOW!(char[static n])(SPI_IOC_MAGIC, 0); //mixin("return _IOW!(char[n])(SPI_IOC_MAGIC, 0);"); } ```tryextern (D) auto SPI_IOC_MESSAGE(size_t N)() { enum n = SPI_MSGSIZE(N); return _IOW!(char[n])(SPI_IOC_MAGIC, 0); //mixin("return _IOW!(char[n])(SPI_IOC_MAGIC, 0);"); }
Mar 29 2018
On Thursday, 29 March 2018 at 09:02:16 UTC, Nicholas Wilson wrote:On Thursday, 29 March 2018 at 08:47:50 UTC, dangbinghoo wrote:thanks for your reply, but it will also fail, the compiler just gives me : ---- source/spidev.d-mixin-129(129,8): Error: variable n cannot be read at compile time ----* #define SPI_MSGSIZE(N) \ ((((N)*(sizeof (struct spi_ioc_transfer))) < (1 << _IOC_SIZEBITS)) \ ? ((N)*(sizeof (struct spi_ioc_transfer))) : 0) */ extern (D) size_t SPI_MSGSIZE(size_t N) { return ((N * (spi_ioc_transfer.sizeof)) < (1 << _IOC_SIZEBITS)) ? (N * (spi_ioc_transfer.sizeof)) : 0; } /* #define SPI_IOC_MESSAGE(N) _IOW(SPI_IOC_MAGIC, 0, char[SPI_MSGSIZE(N)]) */ extern (D) auto SPI_IOC_MESSAGE(size_t N) { size_t n = SPI_MSGSIZE(N); return _IOW!(char[static n])(SPI_IOC_MAGIC, 0); //mixin("return _IOW!(char[n])(SPI_IOC_MAGIC, 0);"); } ```tryextern (D) auto SPI_IOC_MESSAGE(size_t N)() { enum n = SPI_MSGSIZE(N); return _IOW!(char[n])(SPI_IOC_MAGIC, 0); //mixin("return _IOW!(char[n])(SPI_IOC_MAGIC, 0);"); }
Mar 29 2018
On Thursday, 29 March 2018 at 09:13:27 UTC, dangbinghoo wrote:On Thursday, 29 March 2018 at 09:02:16 UTC, Nicholas Wilson wrote:sorry, I just don't notice the `()`. yeah! it compiles, but can you explain it? thanks!On Thursday, 29 March 2018 at 08:47:50 UTC, dangbinghoo wrote:thanks for your reply, but it will also fail, the compiler just gives me : ---- source/spidev.d-mixin-129(129,8): Error: variable n cannot be read at compile time ----[...]try[...]
Mar 29 2018
On Thursday, 29 March 2018 at 09:16:11 UTC, dangbinghoo wrote:On Thursday, 29 March 2018 at 09:13:27 UTC, dangbinghoo wrote:just got it! `size_t N` need to be template param. Thanks for the help!On Thursday, 29 March 2018 at 09:02:16 UTC, Nicholas Wilson wrote:sorry, I just don't notice the `()`. yeah! it compiles, but can you explain it? thanks!On Thursday, 29 March 2018 at 08:47:50 UTC, dangbinghoo wrote:thanks for your reply, but it will also fail, the compiler just gives me : ---- source/spidev.d-mixin-129(129,8): Error: variable n cannot be read at compile time ----[...]try[...]
Mar 29 2018