switched to GPLv3 ONLY, because i don't trust FSF anymore
[gaemu.git] / gaem / parser / utils.d
blob261ed7fd626b099c4a6efcec01e451edbe498a18
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, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 module gaem.parser.utils is aliced;
20 // ////////////////////////////////////////////////////////////////////////// //
21 // handy utility
22 auto selector(RetType=void, T, A...) (T obj, scope A args) if (A.length > 0) {
23 import std.traits : arity, isCallable, Parameters, ReturnType;
24 foreach (immutable aidx, auto arg; args) {
25 static assert(isCallable!(args[aidx]), "non-callable case #"~aidx.stringof);
26 static if (arity!(args[aidx]) == 0) {
27 static if (is(ReturnType!(args[aidx]) == void)) {
28 arg();
29 static if (!is(RetType == void)) return RetType.init; else return;
30 } else {
31 return cast(RetType)arg();
33 } else {
34 static assert(arity!(args[aidx]) == 1, "invalid arity for case #"~aidx.stringof);
35 static assert(is(Parameters!(A[aidx])[0] : T), "invalid delegate argument for case #"~aidx.stringof);
36 // check for common error: `=> {}`
37 static assert(!isCallable!(ReturnType!(A[aidx])), "you probably wrote '=>{}' in case #"~aidx.stringof);
38 if (auto o = cast(Parameters!(A[aidx])[0])obj) {
39 // i found her! ;-)
40 static if (is(ReturnType!(args[aidx]) == void)) {
41 arg(o);
42 static if (!is(RetType == void)) return RetType.init; else return;
43 } else {
44 return cast(RetType)arg(o);
49 static if (!is(RetType == void)) return RetType.init;