1 /**
2 * Authors: k.inaba
3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/
4 *
5 * Common tricks and utilities for programming in D.
6 */
7 module tricks.tricks;
8 import tricks.test;
9 import core.exception;
10 import std.array : appender;
11 import std.format : formattedWrite;
12 import std.traits;
13 import std.typetuple;
14
15 /// Simple Wrapper for std.format.doFormat
16
17 string sprintf(string fmt, T...)(T params)
18 {
19 auto writer = appender!string();
20 formattedWrite(writer, fmt, params);
21 return writer.data;
22 }
23
24 unittest
25 {
26 assert_eq( sprintf!"%s == %04d"("1+2", 3), "1+2 == 0003" );
27 assert_eq( sprintf!"%2$s == %1$s"("1+2", 5, 8), "5 == 1+2" );
28 assert_throw!Error( sprintf!"%s%s"(1) );
29 }
30
31 /// Create an exception with automatically completed filename and lineno information
32
33 ExceptionType genex(ExceptionType, string fn=__FILE__, int ln=__LINE__, T...)(T params)
34 {
35 static if( T.length > 0 && is(T[$-1] : Throwable) )
36 return new ExceptionType(params[0..$-1], fn, ln, params[$-1]);
37 else
38 return new ExceptionType(params, fn, ln);
39 }
40
41 unittest
42 {
43 assert_ne( genex!Exception("msg").file, "" );
44 assert_ne( genex!Exception("msg").line, 0 );
45 assert_ne( genex!Exception("msg",new Exception("bar")).next, Exception.init );
46 }
47
48 /// Mixing-in the bean constructor for a class
49
50 /*mixin*/
51 template SimpleConstructor()
52 {
53 /// member-by-member constructor
54 static if( is(typeof(super) == Object) || super.tupleof.length==0 )
55 this( typeof(this.tupleof) params )
56 {
57 static if(this.tupleof.length>0)
58 this.tupleof = params;
59 }
60 else
61 this( typeof(super.tupleof) ps, typeof(this.tupleof) params )
62 {
63 // including (only) the direct super class members
64 // may not always be a desirable choice, but should work for many cases
65 super(ps);
66 static if(this.tupleof.length>0)
67 this.tupleof = params;
68 }
69 }
70
71 unittest
72 {
73 class Temp
74 {
75 int x;
76 string y;
77 mixin SimpleConstructor;
78 }
79 assert_eq( (new Temp(1,"foo")).x, 1 );
80 assert_eq( (new Temp(1,"foo")).y, "foo" );
81 assert( !__traits(compiles, new Temp) );
82 assert( !__traits(compiles, new Temp(1)) );
83 assert( !__traits(compiles, new Temp("foo",1)) );
84
85 class Tomp : Temp
86 {
87 real z;
88 mixin SimpleConstructor;
89 }
90 assert_eq( (new Tomp(1,"foo",2.5)).x, 1 );
91 assert_eq( (new Tomp(1,"foo",2.5)).y, "foo" );
92 assert_eq( (new Tomp(1,"foo",2.5)).z, 2.5 );
93 assert( !__traits(compiles, new Tomp(3.14)) );
94
95 // shiyo- desu. Don't use in this way.
96 // Tamp tries to call new Tomp(real) (because it only sees Tomp's members),
97 // but it fails because Tomp takes (int,string,real).
98 assert( !__traits(compiles, {
99 class Tamp : Tomp { mixin SimpleConstructor; }
100 }) );
101 }
102
103 /// Mixing-in the MOST-DERIVED-member-wise comparator for a class
104
105 template SimpleToHash()
106 {
107 override hash_t toHash() const /// member-by-member hash
108 {
109 hash_t h = 0;
110 foreach(mem; this.tupleof)
111 h += typeid(mem).getHash(&mem);
112 return h;
113 }
114 }
115
116 /// Mixing-in the MOST-DERIVED-member-wise comparator for a class
117
118 /*mixin*/
119 template SimpleCompare()
120 {
121 override bool opEquals(Object rhs_) const /// member-by-member equality
122 {
123 if( auto rhs = cast(typeof(this))rhs_ )
124 {
125 foreach(i,_; this.tupleof)
126 if( this.tupleof[i] != (cast(const)rhs).tupleof[i] )
127 return false;
128 return true;
129 }
130 assert(false, sprintf!"Cannot compare %s with %s"(typeid(this), typeid(rhs_)));
131 }
132
133 mixin SimpleToHash;
134
135 override int opCmp(Object rhs_) const /// member-by-member compare
136 {
137 if( auto rhs = cast(typeof(this))rhs_ )
138 {
139 foreach(i,_; this.tupleof)
140 if( this.tupleof[i] != (cast(const)rhs).tupleof[i] ) {
141 auto a = (cast(SC_Unqual!(typeof(this)))this).tupleof[i];
142 auto b = rhs.tupleof[i];
143 return a < b ? -1 : +1;
144 }
145 // not work well for structures???????
146 // if(auto c = typeid(_).compare(&this.tupleof[i],&rhs.tupleof[i]))
147 // return c;
148 return 0;
149 }
150 assert(false, sprintf!"Cannot compare %s with %s"(typeid(this), typeid(rhs_)));
151 }
152 }
153
154 alias std.traits.Unqual SC_Unqual;
155
156 unittest
157 {
158 class Temp
159 {
160 int x;
161 string y;
162 mixin SimpleConstructor;
163 mixin SimpleCompare;
164 }
165 assert_eq( new Temp(1,"foo"), new Temp(1,"foo") );
166 assert_eq( (new Temp(1,"foo")).toHash, (new Temp(1,"foo")).toHash );
167 assert_ne( new Temp(1,"foo"), new Temp(2,"foo") );
168 assert_ne( new Temp(1,"foo"), new Temp(1,"bar") );
169 assert_gt( new Temp(1,"foo"), new Temp(1,"bar") );
170 assert_lt( new Temp(1,"foo"), new Temp(2,"bar") );
171 assert_ge( new Temp(1,"foo"), new Temp(1,"foo") );
172
173 class TempDummy
174 {
175 int x;
176 string y;
177 mixin SimpleConstructor;
178 mixin SimpleCompare;
179 }
180 assert_throw!AssertError( new Temp(1,"foo") == new TempDummy(1,"foo") );
181 assert_throw!AssertError( new Temp(1,"foo") <= new TempDummy(1,"foo") );
182 }
183
184 /// Mixing-in a simple toString method
185
186 /*mixin*/
187 template SimpleToString()
188 {
189 /// member-by-member toString
190 override string toString()
191 {
192 string str = sprintf!"%s("(typeof(this).stringof);
193 foreach(i,mem; this.tupleof)
194 {
195 if(i) str ~= ",";
196 static if( is(typeof(mem) == std.bigint.BigInt) )
197 str ~= std.bigint.toDecimalString(mem);
198 else
199 str ~= sprintf!"%s"(mem);
200 }
201 return str ~ ")";
202 }
203 }
204
205 version(unittest) import std.bigint;
206 unittest
207 {
208 class Temp
209 {
210 int x;
211 string y;
212 BigInt z;
213 mixin SimpleConstructor;
214 mixin SimpleToString;
215 }
216 assert_eq( (new Temp(1,"foo",BigInt(42))).toString(), "Temp(1,foo,42)" );
217 }
218
219 /// Everything is in
220
221 /*mixin*/
222 template SimpleClass()
223 {
224 mixin SimpleConstructor;
225 mixin SimpleCompare;
226 mixin SimpleToString;
227 }
228
229 /// Will be used for dynamic overload resolution pattern
230
231 template firstParam(T)
232 {
233 alias ParameterTypeTuple!(T)[0] firstParam;
234 }