Skip to content

Instantly share code, notes, and snippets.

@schveiguy
Last active June 14, 2022 16:30
Show Gist options
  • Save schveiguy/2eea5d38c83fc76e52a4a2d7af03a289 to your computer and use it in GitHub Desktop.
Save schveiguy/2eea5d38c83fc76e52a4a2d7af03a289 to your computer and use it in GitHub Desktop.
D1 operator overloads
mixin template D1Ops() {
// binary operations
static foreach(op, d1;
["+" : "opAdd", "-" : "opSub", "*" : "opMul", "/" : "opDiv",
"%" : "opMod", "&" : "opAnd", "^" : "opXor", "|" : "opOr",
"<<" : "opShl", ">>" : "opShr", ">>>" : "opUShr", "~" : "opCat",
"in" : "opIn"]) {
static if(__traits(hasMember, typeof(this), d1))
alias opBinary(string s : op) = mixin(d1);
// _r version
static if(__traits(hasMember, typeof(this), d1 ~ "_r"))
alias opBinaryRight(string s : op) = mixin(d1 ~ "_r");
// assignment (everything except opIn)
static if(op != "in" && __traits(hasMember, typeof(this), d1 ~ "Assign"))
alias opOpAssign(string s : op) = mixin(d1 ~ "Assign");
}
// unary operations
static foreach(op, d1;
["-" : "opNeg", "~" : "opCom", "++" : "opPostInc",
"--" : "opPostDec", "*" : "opStar"]) {
static if(__traits(hasMember, typeof(this), d1))
alias opUnary(string s : op) = mixin(d1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment