events renaming
[gaemu.git] / gmlparser / utils.d
blobc51a36e53082360b5ff3b66e2d239cd36cf62106
1 /* GML parser
2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 module gmlparser.utils is aliced;
21 // ////////////////////////////////////////////////////////////////////////// //
22 // handy utility
23 auto selector(RetType=void, T, A...) (T obj, scope A args) if (A.length > 0) {
24 import std.traits : arity, isCallable, Parameters, ReturnType;
25 foreach (immutable aidx, auto arg; args) {
26 static assert(isCallable!(args[aidx]), "non-callable case #"~aidx.stringof);
27 static if (arity!(args[aidx]) == 0) {
28 static if (is(ReturnType!(args[aidx]) == void)) {
29 arg();
30 static if (!is(RetType == void)) return RetType.init; else return;
31 } else {
32 return cast(RetType)arg();
34 } else {
35 static assert(arity!(args[aidx]) == 1, "invalid arity for case #"~aidx.stringof);
36 static assert(is(Parameters!(A[aidx])[0] : T), "invalid delegate argument for case #"~aidx.stringof);
37 // check for common error: `=> {}`
38 static assert(!isCallable!(ReturnType!(A[aidx])), "you probably wrote '=>{}' in case #"~aidx.stringof);
39 if (auto o = cast(Parameters!(A[aidx])[0])obj) {
40 // i found her! ;-)
41 static if (is(ReturnType!(args[aidx]) == void)) {
42 arg(o);
43 static if (!is(RetType == void)) return RetType.init; else return;
44 } else {
45 return cast(RetType)arg(o);
50 static if (!is(RetType == void)) return RetType.init;