I am trying to convert C macro into a template. But I am struggling to understand the C language code.
The macro is (defined here in case you need some context):
#define r(vr) comp->r[vr]
where comp is a struct of type ModelInstance.
I barely remember C, but it looks like comp->r[vr] refers to component vr of the r array within comp struct. Am I right? But the ModelInstance definition doesn't seem to suggest it is an array.
Any help interpreting this please?
Yes that should be right:
template r(vr: untyped): untyped = comp.r[vr]
But the ModelInstance definition doesn't seem to suggest it is an array.
fmi2Real *r;
In C, arrays are a pointer to the first element in the array, thus r is both a pointer to a fmi2Real and an array of fmi2Reals.Are comp a ModelInstance or is it a pointer to a ModelInstance? In C the "->" operator takes a pointer to a struct and the field we want and then dereferences it. The equivalent Nim code in that case is:
template r(vr: untyped): untyped = comp[].r[vr]
And if you want to write it like in the C code this template does it:
template `->`(s, f: untyped): untyped = s[].f
Then the template becomes:
template r(vr: untyped): untyped = comp->r[vr]