Dlang equivalent of #define/#ifdef : not... version
ichneumwn
idonotenjoyemail at idonotenjoyemail.org
Tue Apr 20 18:57:46 UTC 2021
Hi,
Trying to convert a C header to D. The underlying package exists
in different versions and may or may not expose certain
functionality (modules/extensions). One should check at compile
time that the header provides the relevant definitions, through
#ifdef, and do a further run-time check to confirm the
functionality is really present. It is the compile time check
that I am finding tricky to do/emulate.
.h : #define i_am_a_feature 1
The C-code uses this define as a guard:
.c : #ifdef i_am_a_feature
What would the the D equivalent? This is my attempt so far:
features.d:
import std.traits;
private enum capabilities {
i_am_a_feature
}
template supported(string member)
{
enum bool supported = hasMember!(capabilities, member);
}
version = vs_i_am_a_feature;
enum can_i_test_for_this;
and use.d:
import features;
import std.stdio;
void main() {
static if (supported!("i_am_a_feature")) {
writeln("Feature 1!");
}
static if (supported!("i_am_not_a_feature")) {
writeln("Feature 2!");
}
version(vs_i_am_a_feature) {
writeln("If only I worked");
}
}
This produces "Feature 1!", so the supported() path works, but is
a bit of a round-about way and all "capability flags" need to put
in that single enum capabilities instead of being allowed to be
scattered across the features.d module.
As is documented, "version" does not cross the module boundary.
So my questions:
- is there a module-crossing equivalent of "version"?
- if not, is there some way I could test for the existence of the
enum can_i_test_for_this? A SymbolExists!() or ModuleHasSymbol!()
or ModuleHasMember!() ?
Cheers
More information about the Digitalmars-d-learn
mailing list