Skip to content

Instantly share code, notes, and snippets.

@foysavas
Created December 8, 2009 16:13
Show Gist options
  • Save foysavas/251760 to your computer and use it in GitHub Desktop.
Save foysavas/251760 to your computer and use it in GitHub Desktop.
/* Example DataDapper Model */
class User {
struct Properties {
DD_Property!(int,"id") id;
DD_Property!(string,"username") username;
DD_Property!(double,"score") score;
}
Properties properties;
mixin(DD_Property_GetterSetter!(int,"id"));
mixin(DD_Property_GetterSetter!(string,"username"));
mixin(DD_Property_GetterSetter!(double,"score"));
mixin DD_toString;
}
/* Behind the Curtain */
struct DD_Property(T, string s) {
string name = s;
T value;
}
template DD_Type(T) {
static if (is(T == int)) {
const char[] DD_Type = "int";
} else static if (is(T == double)) {
const char[] DD_Type = "double";
} else static if (is(T == string)) {
const char[] DD_Type = "string";
}
}
template DD_Property_Getter(PropType, const char[] PropMethod) {
const char[] DD_Property_Getter =
DD_Type!(PropType) ~ " " ~ PropMethod ~ "() { return this.properties." ~ PropMethod ~ ".value; } \n";
}
template DD_Property_Setter(PropType, const char[] PropMethod) {
const char[] DD_Property_Setter =
"void " ~ PropMethod ~ "(" ~ DD_Type!(PropType) ~ " _v) { this.properties." ~ PropMethod ~ ".value = _v; }\n";
}
template DD_Property_GetterSetter(PropType, const char[] PropMethod) {
const char[] DD_Property_GetterSetter =
DD_Property_Getter!(PropType, PropMethod) ~
DD_Property_Setter!(PropType, PropMethod);
}
template DD_toString() {
string toString() {
string r = "Model Object:\n";
foreach (i, E; this.properties.tupleof)
r = r ~ " " ~ E.name ~ " = " ~ to!string(E.value) ~ "\n";
return r;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment