I'm currently working on a WebGL-like OpenGL wrapper in c++, which involves verifying that arguments are actually valid. The problem, more or less, is that OpenGL has a ton, and I'm not exaggerating here, of valid arguments for certain functions.
(Image of a naive conditional (possible
internalformat
constants for glTexImage2D
, if you're interested. I literally wrote a JS table parser/conditional maker for a few of those) to make my point. That's an assert
...)
The logic of such checks is simple enough:
bool check(const unsigned int var)
{
return var == CONSTANT_1 || var == CONSTANT_2 || var == CONSTANT_...;
}
But, as per the above example, this can stretch for up to 84 possible constants, which is... yeah. And I'm fully aware that a macro wouldn't cut this down by an absurd amount, but it would still make a difference, and I feel like it would be cleaner too. Not to mention the possibility of other macros of a similar fashion with different operators.
So my idea was to use a macro. After all, this check would be trivial to do at runtime, with a std::initializer_list``,
std::vector,
std::array` or some other container, but since all of these constants are known at compile time, I feel as though this is unnecessary.
Since the number of possible constants is variable, I see no way of not using a variadic macro. Yet I don't know how I would accomplish this. One possible way I thought of doing this is basically overloading a macro based on the number of arguments (akin to this), but this seems unnecessarily complex.
In essence, I'm looking for a macro that would fulfill the following:
MACRO(var, CONSTANT_1, CONSTANT_2, CONSTANT_3)
expands to
var == CONSTANT_1 || var == CONSTANT_2 || var == CONSTANT_3
with any number of constants (important!, and the hard part).