Hello,
I have the following declaration:
const GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS* = 35661 In the normal code it is used as a cuint. I would like to pass this constant into a template as a parameter and in there extract the name of that parameter's identifier. The best attempt I have is as follows:
type
P = proc(param: cuint, name: string)
Capability_t = object
param: cuint
name: string
get: P
template capability(p: cuint, g: P): Capability_t =
Capability_t(
param: p,
name: p.astToStr,
# name: g.astToStr,
get:g
) As the code stands above, the name will hold the value as an integer ("35661"). The commented line however works fine. The name of the calling function is obtained. Any magic incantations so that I can get the string "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS"? Note that I cannot change the constant's declaration.
TIA
I don't understand what you're trying to achieve, this looks cursed to me. A const is just a value, its identifier is extremely short-lived, I'm not sure you could even access it from a macro. Better write some regular code using types to store necessary information.
I suppose the const values are not unique, since if it were so you could just store strings in a table or hardcode them inside the template? If they aren't unique you'll need to find a way to tie the name to the value in an object at instantiation time.
I think the proper approach is to define a set of enums for all the consts at hand and store their cuint values in an enum-indexed array.
I don't understand what you're trying to achieve, this looks cursed to me.
The goal is to go through an array of these constants and use them to obtain information via a function call. For each of these values, the output would show the name of the constant identifier and the corresponding values.
I suppose the const values are not unique, since if it were so you could just store strings in a table or hardcode them inside the template?
Sure, I can and am hardcoding these unique values. I have:
let capabilities = [
Capability_t(param: GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, name: "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS", get:GLContextParamInt_1),
...] but want something like:
let capabilities = [
capability(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, GLContextParamInt_1),
...
] to make tings "cleaner".I think the proper approach is to define a set of enums for all the consts at hand and store their cuint values in an enum-indexed array.
Don't require all of them. And as I said, this is not my code, so I am not going to redefine things with an enum.
I'm not sure you could even access it from a macro.
I have confirmed it so. p.treeRepr() shows IntLit 35661 and no strVal exists for this node.
So it seem no magic incantation is possible. Pity.
I had already tried that. So
template cap1(pp: untyped) =
echo fmt"*********************** {astToStr(pp)}" called via:
cap1(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS) Generates this during runt-time:
*********************** pp Note that if I pass a function (with correct type), the function name is generated as expected.Works fine without fmt:
const
GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS = 34930
template cap1(pp: untyped) =
echo "*********************** " & astToStr(pp)
cap1(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS)
which is common knowledge.
I can confirm that with concatenation`&` it works. But with fmt it does not. Must be some interaction between the templates and the parameters.
Thanks.
which is common knowledge.