[ create a new paste ] login | about

Link: http://codepad.org/xa1mI004    [ raw code | output | fork ]

D, pasted on Sep 4:
import std.stdio;

template HANDLE_MSG(alias hwnd, string message, string fn) {
	const string HANDLE_MSG = "case " ~ message ~ ": " ~
		"return HANDLE_" ~ message ~ "(" ~
			hwnd.stringof ~ ", wParam, lParam, &" ~ fn ~ ");";
}
version(D_Version2) {
	template HANDLE_MSG(alias hwnd, alias message, alias fn) {
		const string HANDLE_MSG = "case " ~ message.stringof ~ ": " ~
			"return HANDLE_" ~ message.stringof ~ "(" ~
				hwnd.stringof ~ ", wParam, lParam, " ~ (&fn).stringof ~ ");";
	}
}

int HANDLE_WM_MSG1(void* h, int w, int l, int function(void*, int, int) fn) {
	return fn(h, w, l);
}

int HANDLE_WM_MSG2(void* h, int w, int l, int function(void*, int, int) fn) {
	return fn(h, w, l);
}

int handler1(void* h, int w, int l) {
	writef("WM_MSG1: wParam = %d, lParam = %d\n", w, l);
	return 0;
}

int handler2(void* h, int w, int l) {
	writef("WM_MSG2: wParam = %d, lParam = %d\n", w, l);
	return 0;
}

const int WM_MSG1 = 1;
const int WM_MSG2 = 2;

int test(int msg) {
	void* hwnd = null;
	int wParam = 1, lParam = 2;
	version(D_Version2) {
		switch(msg) {
			mixin(HANDLE_MSG!(hwnd, WM_MSG1, handler1));
			mixin(HANDLE_MSG!(hwnd, WM_MSG2, handler2));
			default:
		}
	} else {
		switch(msg) {
			mixin(HANDLE_MSG!(hwnd, "WM_MSG1", "handler1"));
			mixin(HANDLE_MSG!(hwnd, "WM_MSG2", "handler2"));
			default:
		}
	}
	return 0;
}

void main() {
	test(WM_MSG1);
	test(WM_MSG2);
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement
Line 48: found 'EOF' instead of statement


Create a new paste based on this one


Comments: