summaryrefslogtreecommitdiff
path: root/plugins/Clist_modern/src/modern_sync.h
blob: 0b5a827f1151b0f36cc69f8c3103ee8cbd0b1bf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef modern_sync_h__
#define modern_sync_h__

typedef INT_PTR(*PSYNCCALLBACKPROC)(WPARAM, LPARAM);

int SyncCall(void * vproc, int count, ...);

// Experimental sync caller

int DoCall(PSYNCCALLBACKPROC pfnProc, WPARAM wParam, LPARAM lParam);

// Have to be here due to MS Visual C++ does not support 'export' keyword

// 3 params

template<class RET, class A, class B, class C> class PARAMS3
{
	typedef RET(*proc_t)(A, B, C);
	proc_t _proc;  A _a; B _b; C _c; RET _ret;

public:
	PARAMS3(proc_t __proc, A __a, B __b, C __c) : _proc(__proc), _a(__a), _b(__b), _c(__c){};
	static int DoSyncCall(WPARAM, LPARAM lParam)
	{
		PARAMS3 * params = (PARAMS3 *)lParam;
		params->_ret = params->_proc(params->_a, params->_b, params->_c);
		return 0;
	};
	RET GetResult() { return _ret; }
};

template< class RET, class Ap, class Bp, class Cp, class A, class B, class C> RET Sync(RET(*proc)(Ap, Bp, Cp), A a, B b, C c)
{
	PARAMS3<RET, Ap, Bp, Cp> params(proc, a, b, c);
	DoCall((PSYNCCALLBACKPROC)PARAMS3<RET, Ap, Bp, Cp>::DoSyncCall, 0, (LPARAM)&params);
	return params.GetResult();
};


// 2 params

template<class RET, class A, class B> class PARAMS2
{
	typedef RET(*proc_t)(A, B);
	proc_t _proc;  A _a; B _b; RET _ret;

public:
	PARAMS2(proc_t __proc, A __a, B __b) : _proc(__proc), _a(__a), _b(__b){};
	static int DoSyncCall(WPARAM, LPARAM lParam)
	{
		PARAMS2 * params = (PARAMS2 *)lParam;
		params->_ret = params->_proc(params->_a, params->_b);
		return 0;
	};
	RET GetResult() { return _ret; }
};

template< class RET, class Ap, class Bp, class A, class B> RET Sync(RET(*proc)(Ap, Bp), A a, B b)
{
	PARAMS2<RET, Ap, Bp> params(proc, a, b);
	DoCall((PSYNCCALLBACKPROC)PARAMS2<RET, Ap, Bp>::DoSyncCall, 0, (LPARAM)&params);
	return params.GetResult();
};


// 1 param
template<class RET, class A> class PARAMS1
{
	typedef RET(*proc_t)(A);
	proc_t _proc;  A _a; RET _ret;

public:
	PARAMS1(proc_t __proc, A __a) : _proc(__proc), _a(__a){};
	static int DoSyncCall(WPARAM, LPARAM lParam)
	{
		PARAMS1 * params = (PARAMS1 *)lParam;
		params->_ret = params->_proc(params->_a);
		return 0;
	};
	RET GetResult() { return _ret; }
};

template< class RET, class Ap, class A> RET Sync(RET(*proc)(Ap), A a)
{
	PARAMS1<RET, Ap> params(proc, a);
	DoCall((PSYNCCALLBACKPROC)PARAMS1<RET, Ap>::DoSyncCall, 0, (LPARAM)&params);
	return params.GetResult();
};

#endif // modern_sync_h__