mixin string to template - advice needed
Marek Janukowicz
marek at janukowicz.net
Fri Jul 26 13:33:26 PDT 2013
Hello
I have a repetitive piece of code that I'm now generating using
string mixin. I humbly ask someone more skilled with D to review
the code and help me transforming it into regular template mixin
(as I would gladly avoid string mixin if possible).
string flaggedAttr(string type, string name, string capName,
string upName ) {
return "
@property bool has" ~ capName ~" () {
return (_wildcards & MatchWildcard." ~ upName ~") > 0;
}
@property bool has" ~ capName ~ " ( bool has ) {
if (has) _wildcards |= MatchWildcard." ~ upName ~ ";
else _wildcards &= ~MatchWildcard." ~ upName ~ ";
return has;
}
@property " ~ type ~ " " ~ name ~ " ()
in {
assert( has" ~ capName ~ "(), \"" ~ capName ~ " not set
in wildcards\" );
}
body {
return _" ~ name ~ ";
}
@property " ~ type ~ " " ~ name ~ " (" ~ type ~ " val) {
has" ~ capName ~ " = true;
_" ~ name ~ " = val;
return _" ~ name ~ ";
}
";
}
( ... and the in a struct ... )
mixin(flaggedAttr( "PortNumber", "inPort", "InPort", "IN_PORT" ));
( ... which results in ... )
@property bool hasInPort () {
return (_wildcards & MatchWildcard.IN_PORT) > 0;
}
@property bool hasInPort ( bool has ) {
if (has) _wildcards |= MatchWildcard.IN_PORT;
else _wildcards &= ~MatchWildcard.IN_PORT;
return has;
}
@property PortNumber inPort ()
in {
assert( hasInPort(), "InPort not set in wildcards" );
}
body {
return _inPort;
}
@property PortNumber inPort (PortNumber port) {
hasInPort = true;
_inPort = port;
return _inPort;
}
The problems I struggle to solve:
* dynamic method names (like "hasInPort" created from argument
"inPort")
* different versions of argument string (eg. "InPort" and
"IN_PORT" from argument "inPort")
* getting value from enum (MatchWildcard) give the name of the
value (eg. "IN_PORT")
Thank you for any help
More information about the Digitalmars-d-learn
mailing list