public
Last active

  • Download Gist
properties.d
D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
/*
 
Demonstration of a library-level Property implementation.
 
*/
 
import std.stdio;
 
const DefaultGetter = "";
const DefaultSetter = DefaultGetter;
 
private
{
enum DEFAULT_GETTER = q{ return storage; };
enum DEFAULT_SETTER = q{ return storage = value; };
 
string trim(string s)
{
while( s.length > 0
&& (s[0] == ' ' || s[0] == '\t'
|| s[0] == '\f' || s[0] == '\v'
|| s[0] == '\r' || s[0] == '\n') )
s = s[1..$];
 
while( s.length > 0
&& (s[$-1] == ' ' || s[$-1] == '\t'
|| s[$-1] == '\f' || s[$-1] == '\v'
|| s[$-1] == '\r' || s[$-1] == '\n') )
s = s[0..$-1];
 
return s;
}
 
size_t indexOf(T)(T haystack, T needle)
{
if( needle == "" ) return 0;
 
size_t i = 0;
while( haystack.length > 0 )
{
if( haystack.startsWith(needle) )
return i;
++i;
haystack = haystack[1..$];
}
return i;
}
 
bool startsWith(T)(T haystack, T needle)
{
return haystack.length >= needle.length
&& haystack[0..needle.length] == needle;
}
 
bool endsWith(T)(T haystack, T needle)
{
return haystack.length >= needle.length
&& haystack[$-needle.length..$] == needle;
}
 
bool contains(T)(T haystack, T needle)
{
if( needle.length == 0 ) return true;
 
while( haystack.length >= needle.length )
{
if( haystack.startsWith(needle) )
return true;
haystack = haystack[1..$];
}
 
return false;
}
 
string propertyName(string name)
{
return name[0..name.indexOf("=")];
}
 
string propertyInitialiser(string name)
{
size_t i = name.indexOf("=");
if( i<name.length )
return name[i+1..$];
else
return null;
}
 
string protectionHandlerStorage(string prot)
{
return "
if( (var.startsWith(`"~prot~"`)
&& var[`"~prot~"`.length..$].trim().startsWith(`{`)
&& var.trim().endsWith(`}`)) )
{
return recurse(
var[`"~prot~"`.length..$]
.trim()[`{`.length..$][$-`}`.length..$],
DEFAULT
);
}
else if( var == `"~prot~"` )
{
return recurse(DEFAULT);
}
";
}
 
bool usesStorage(string code, string DEFAULT="")
{
code = code.trim();
if( code == "" )
return usesStorage(DEFAULT);
if( code == "-" )
return false;
 
{
alias usesStorage recurse;
alias code var;
mixin(protectionHandlerStorage("public"));
mixin(protectionHandlerStorage("private"));
mixin(protectionHandlerStorage("protected"));
mixin(protectionHandlerStorage("package"));
}
 
return code.contains("storage");
}
 
string storageName(string name)
{
return "__storage_"~name;
}
 
string propertyStorage(string T, string name)
{
auto initialiser = name.propertyInitialiser();
if( initialiser != "" )
initialiser = " = " ~ initialiser;
 
return "private " ~ T ~ " " ~ storageName(name.propertyName())
~ initialiser ~ ";\n";
}
 
string protectionHandler(string prot)
{
return "
if( (var.startsWith(`"~prot~"`)
&& var[`"~prot~"`.length..$].trim().startsWith(`{`)
&& var.trim().endsWith(`}`)) )
{
return `"~prot~" ` ~ recurse(
T,
name,
var[`"~prot~"`.length..$]
.trim()[`{`.length..$][$-`}`.length..$]
);
}
else if( var == `"~prot~"` )
{
return `"~prot~" ` ~ recurse(T, name, DEFAULT);
}
";
}
 
string propertyGetter(string T, string name, string getter)
{
getter = getter.trim();
 
if( getter == "" )
return propertyGetter(T, name, DEFAULT_GETTER);
if( getter == "-" )
return "";
 
{
alias propertyGetter recurse;
alias getter var;
alias DEFAULT_GETTER DEFAULT;
mixin(protectionHandler("public"));
mixin(protectionHandler("private"));
mixin(protectionHandler("protected"));
mixin(protectionHandler("package"));
}
auto storage = `
static if( is( typeof(`~storageName(name)~`) ) )
{
alias `~storageName(name)~` storage;
}
`;
return
T~` `~name~`()
{
`~storage~`
`~getter~`
}
`;
}
 
string propertySetter(string T, string name, string setter)
{
setter = setter.trim();
if( setter == "" )
return propertySetter(T, name, DEFAULT_SETTER);
if( setter == "-" )
return "";
 
{
alias propertySetter recurse;
alias setter var;
alias DEFAULT_SETTER DEFAULT;
mixin(protectionHandler("public"));
mixin(protectionHandler("private"));
mixin(protectionHandler("protected"));
mixin(protectionHandler("package"));
}
auto storage = `
static if( is( typeof(`~storageName(name)~`) ) )
{
alias `~storageName(name)~` storage;
}
`;
return
`auto `~name~`(`~T~` value)
{
`~storage~`
`~setter~`
}
`;
}
}
 
template Property(
T,
string name,
string getter=DefaultGetter,
string setter=DefaultSetter)
{
/*
enum Property = "";
/*/
enum Property =
(usesStorage(getter, DEFAULT_GETTER)
|| usesStorage(setter, DEFAULT_SETTER)
? propertyStorage(T.stringof, name)
: "")
~ propertyGetter(T.stringof, name.propertyName(), getter)
~ propertySetter(T.stringof, name.propertyName(), setter);
//*/
}
 
enum CODE =
 
""
~ "\n// bar: default\n"
~ Property!(int, "bar")
~ "\n// baz: publically read-only\n"
~ Property!(int, "baz", null, "private")
~ "\n// bop: publically read-only with default\n"
~ Property!(int, "bop = 42", null, "private")
~ "\n// qux: read-only\n"
~ Property!(int, "qux", null, "-")
 
~ "\n// zyz: custom reader and writer\n"
~ Property!(int, "zyz",
q{
return 7;
},
q{
writefln("Tried to set foo to %s!", value);
})
 
;
 
pragma(msg, CODE);
 
struct S
{
mixin(CODE);
}
 
void main()
{
S s;
s.bar = 1701;
writefln("s.bar = %s", s.bar);
 
writefln("s.baz = %s", s.baz);
 
writefln("s.bop = %s", s.bop);
 
writefln("s.zyz = %s", s.zyz);
s.zyz = 1984;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.