how to do this meta-programming? print the address of random element's address of a variable length of arrays?

mw mingwu at gmail.com
Sun Sep 13 07:00:36 UTC 2020


On Sunday, 13 September 2020 at 01:25:43 UTC, mw wrote:
> On Saturday, 12 September 2020 at 20:29:40 UTC, Paul Backus
>> If you have a "real-life" application in mind for this, I'd be 
>> curious to hear what it is.
>
> I'm wrapping a C library, trying to write a single D function / 
> template that can work with a group of C functions, hence I 
> need that kind of signature I described.
>
> I'll post the code when I'm ready.

Here it is: D wrapper for https://ta-lib.org/

https://github.com/mingwugmail/talibd

I end up using C macro to generate D functions, the single 
template is this one:

https://github.com/mingwugmail/talibd/blob/master/source/talibd.h#L117
#define DECL_TA_FUNC(TA_FUNC, FUNC_INS, FUNC_OUTS, 
expected_lookback) __NL__\


and the macro instantiations are on line 144, 158, 168:

DECL_TA_FUNC(TA_MA, MA_INS, MA_OUTS, (MA_optInTimePeriod-1))
DECL_TA_FUNC(TA_RSI, RSI_INS, RSI_OUTS, RSI_optInTimePeriod)
DECL_TA_FUNC(TA_MACD, MACD_INS, MACD_OUTS, 
(optInSlowPeriod+optInSignalPeriod-2))


the generated D functions are here:

https://github.com/mingwugmail/talibd/blob/master/source/talibd.d#L47

you can take a look of the 3 generated functions to see the 
similarities, and pay attention to:

-- func decl: TA_xxx( <out array params>, <in options> )
-- assertions on <out arrays>
-- func calls: TA_xxx_Lookback(<in options>)
-- func call: talib.TA_xxx(<in options>, <transformed out 
arrays>, <also note: &begin, &num>)


There are a number of C macro tricks was used, which I just did 
some googling to be able to get it done in C -- and with the 
added benefits that I can directly see the generated source file 
to debug in the development process.


I think it's a non-trivial task to get this compile-time 
meta-programming done in D, if it can be done at all (I'm not 
sure).


Anyone want to give it a try? and submit a PR :-)



More information about the Digitalmars-d-learn mailing list