-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback.h
84 lines (73 loc) · 3.54 KB
/
callback.h
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
#pragma once
#ifndef _OBJC_CALLBACK_H_
#define _OBJC_CALLBACK_H_
#include <boost/preprocessor/repetition.hpp>
#include <boost/preprocessor/iteration/local.hpp>
#ifndef CALLBACK_arg_max
# define CALLBACK_arg_max 10
#endif
//
// Generate all obj-c callback bridges with safe signatures
//
#define CALLBACK_operator_params(z, n, text) BOOST_PP_COMMA_IF(n) \
BOOST_PP_CAT(T,n) BOOST_PP_CAT(text,n)
#define CALLBACK_generator(z, n, unused) \
template<typename R BOOST_PP_COMMA_IF(n) \
BOOST_PP_ENUM_PARAMS(n, typename T)> \
class objc_callback_##n \
{ \
public: \
typedef R (*func)( \
id, SEL BOOST_PP_COMMA_IF(n) \
BOOST_PP_ENUM_PARAMS(n, T) \
); \
objc_callback_##n(SEL sel, id obj) \
: sel_(sel) \
, obj_(obj) \
, fun_((func)[obj methodForSelector:sel]) \
{} \
inline R operator () \
(BOOST_PP_REPEAT_FROM_TO( \
0, n, CALLBACK_operator_params, par)) \
{ \
return fun_(obj_, sel_ BOOST_PP_COMMA_IF(n) \
BOOST_PP_ENUM_PARAMS(n, par)); \
} \
private: \
SEL sel_; \
id obj_; \
func fun_; \
};
#define BOOST_PP_LOCAL_MACRO(n) CALLBACK_generator(~, n, ~)
#define BOOST_PP_LOCAL_LIMITS (0, CALLBACK_arg_max-1)
#include BOOST_PP_LOCAL_ITERATE()
#undef CALLBACK_generator
//
// Now define the preferred syntax. This will be just a wrapper though
//
template<typename Signature> class objc_callback;
#define CALLBACK_generator(z, n, unused) \
template<typename R BOOST_PP_COMMA_IF(n) \
BOOST_PP_ENUM_PARAMS(n, typename T)> \
class objc_callback<R( \
BOOST_PP_ENUM_PARAMS(n, T) \
)> \
: public objc_callback_##n<R BOOST_PP_COMMA_IF(n) \
BOOST_PP_ENUM_PARAMS(n, T)> \
{ \
public: \
objc_callback(SEL sel, id obj) \
: objc_callback_##n< \
R BOOST_PP_COMMA_IF(n) \
BOOST_PP_ENUM_PARAMS(n, T) \
>(sel, obj) \
{ \
} \
};
#define BOOST_PP_LOCAL_MACRO(n) CALLBACK_generator(~, n, ~)
#define BOOST_PP_LOCAL_LIMITS (0, CALLBACK_arg_max-1)
#include BOOST_PP_LOCAL_ITERATE()
#undef CALLBACK_operator_params
#undef CALLBACK_generator
#undef CALLBACK_arg_max
#endif // _OBJC_CALLBACK_H_