1 module uim.oop.mixins.properties.property;
2 
3 import std..string;
4 
5 auto PROPERTYPREFIX(string dataType, string propertyName, string defaultValue = null) {
6 	return "
7 protected "~dataType~" _"~propertyName~(defaultValue.length > 0 ? " = "~defaultValue : "")~";
8 protected "~dataType~" _default"~propertyName~(defaultValue.length > 0 ? " = "~defaultValue : "")~";
9 
10 auto "~propertyName~"Default() { return _default"~propertyName~"; }
11 void "~propertyName~"Reset() { _"~propertyName~" = _default"~propertyName~"; }
12 void "~propertyName~"Default("~dataType~" value) { _default"~propertyName~" = value; }
13 bool "~propertyName~"IsDefault() { return (_"~propertyName~" == _default"~propertyName~"); }";
14 }
15 
16 template OProperty_get(string dataType, string propertyName, string defaultValue = null) {
17 	const char[] OProperty_get = PROPERTYPREFIX(dataType, propertyName, defaultValue) ~"
18 @property "~dataType~" "~propertyName~"() { return _"~propertyName~"; }";
19 }
20 
21 template OProperty_set(string dataType, string propertyName, string defaultValue = null) {
22 	const char[] OProperty_set = PROPERTYPREFIX(dataType, propertyName, defaultValue) ~"
23 @property O "~propertyName~"(this O)("~dataType~" value) { _"~propertyName~" = value; }";
24 }
25 
26 template OProperty(string dataType, string propertyName, string defaultValue = null, string get = null, string set = null) {
27 	const char[] getFkt = (get.length > 0) ? get : "return _"~propertyName~";";
28 	const char[] setFkt = (set.length > 0) ? set : "_"~propertyName~" = newValue;";
29 	
30 	const char[] OProperty = "
31 	protected "~dataType~" _"~propertyName~(defaultValue.length > 0 ? " = "~defaultValue : "")~";
32 	protected "~dataType~" _default"~propertyName~(defaultValue.length > 0 ? " = "~defaultValue : "")~";
33 	
34 	@safe @property "~dataType~" "~propertyName~"() { "~getFkt~" }
35 	@safe protected void _set"~propertyName~"("~dataType~" newValue) { "~setFkt~" }
36 	@safe @property O "~propertyName~"(this O)("~dataType~" newValue) { _set"~propertyName~"(newValue); return cast(O)this; }";
37 }
38 unittest {
39 	class Test {mixin(OProperty!("string", "name", "`someThing`"));}
40 	assert((new Test).name == "someThing");
41 	assert((new Test).name("test").name == "test");
42 	assert((new Test).name("test").name("test2").name == "test2");
43 }
44 // mixins for Template based properties
45 
46 template TProperty(string dataType, string propertyName, string defaultValue = null, string get = null, string set = null) {
47 	const char[] getFkt = (get.length > 0) ? get : "return _"~propertyName~";";
48 	const char[] setFkt = (set.length > 0) ? set : "_"~propertyName~" = newValue;";
49 	
50 	const char[] TProperty = "
51 	protected "~dataType~" _"~propertyName~(defaultValue.length > 0 ? " = "~defaultValue : "")~";
52 	protected "~dataType~" _default"~propertyName~(defaultValue.length > 0 ? " = "~defaultValue : "")~";
53 	
54 	@safe @property "~dataType~" "~propertyName~"(this O)() { "~getFkt~" }
55 	@safe @property O "~propertyName~"(this O)("~dataType~" newValue) { "~setFkt~" return cast(O)this; }";
56 }
57 
58 template TPropertyAA(string keyDataType, string valueDataType, string propertyName, string defaultValue = null, string get = null, string set = null) {
59 	const char[] getFkt = (get.length > 0) ? get : "return _"~propertyName~";";
60 	const char[] setFkt = (set.length > 0) ? set : "_"~propertyName~" = newValue;";
61 	const char[] aaDataType = valueDataType~"["~keyDataType~"]";
62 	
63 	const char[] TPropertyAA = "
64 	protected "~aaDataType~" _"~propertyName~(defaultValue.length > 0 ? " = "~defaultValue : "")~";
65 	protected "~aaDataType~" _default"~propertyName~(defaultValue.length > 0 ? " = "~defaultValue : "")~";
66 	
67 	@safe @property "~aaDataType~" "~propertyName~"(this O)() { "~getFkt~" }
68 	@safe @property O "~propertyName~"(this O)("~aaDataType~" newValue) { "~setFkt~" return cast(O)this; }
69 	O "~propertyName~"(this O)("~keyDataType~" key, "~valueDataType~" value) { _"~propertyName~"[key] = value; return cast(O)this; }
70 	O "~propertyName~"Add(this O)("~aaDataType~" values) { foreach(k,v;values) _"~propertyName~"[k] = v; return cast(O)this; }";
71 }
72 
73 // mixins for Extended Template based properties
74 
75 template TXProperty_get(string dataType, string propertyName, string defaultValue = null, string get = null) {
76 	const char[] getFkt = (get.length > 0) ? get : "return _"~propertyName~";";
77 
78 	const char[] TXProperty_get = "
79 	protected "~dataType~" _"~propertyName~(defaultValue.length > 0 ? " = "~defaultValue : "")~";
80 	protected "~dataType~" _default"~propertyName~(defaultValue.length > 0 ? " = "~defaultValue : "")~";
81 	
82 	O "~propertyName~"Reset(this O)() { _"~propertyName~" = _default"~propertyName~"; return cast(O)this;}
83 	auto "~propertyName~"Default() { return _default"~propertyName~"; }
84 	O "~propertyName~"Default(this O)("~dataType~" newValue) { _default"~propertyName~" = newValue; return cast(O)this; }
85 	bool "~propertyName~"IsDefault() { return (this._"~propertyName~" == _default"~propertyName~"); }
86 
87 	@property "~dataType~" "~propertyName~"(this O)() { "~getFkt~" }";
88 }
89 
90 template TXProperty(string dataType, string propertyName, string defaultValue = null, string get = null, string set = null) {
91 	const char[] getFkt = (get.length > 0) ? get : "return _"~propertyName~";";
92 	const char[] setFkt = (set.length > 0) ? set : "_"~propertyName~" ~= newValue;";
93 	
94 	const char[] TXProperty = "
95 	protected "~dataType~" _"~propertyName~(defaultValue.length > 0 ? " = "~defaultValue : "")~";
96 	protected "~dataType~" _default"~propertyName~(defaultValue.length > 0 ? " = "~defaultValue : "")~";
97 	
98 	O "~propertyName~"Reset(this O)() { _"~propertyName~" = _default"~propertyName~"; return cast(O)this;}
99 	auto "~propertyName~"Default() { return _default"~propertyName~"; }
100 	O "~propertyName~"Default(this O)("~dataType~" newValue) { _default"~propertyName~" = newValue; return cast(O)this; }
101 	bool "~propertyName~"IsDefault() { return (this._"~propertyName~" == _default"~propertyName~"); }
102 
103 	@property "~dataType~" "~propertyName~"(this O)() { "~getFkt~" }
104 	@property O "~propertyName~"(this O)("~dataType~" newValue) { "~setFkt~" return cast(O)this; }";
105 }
106 
107 unittest {
108 
109 }
110