-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathmagic_macros.hpp
More file actions
326 lines (301 loc) · 12 KB
/
Copy pathmagic_macros.hpp
File metadata and controls
326 lines (301 loc) · 12 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* Copyright (C) 2016 Turi
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD license. See the LICENSE file for details.
*/
#ifndef CPPIPC_MAGIC_MACROS_HPP
#define CPPIPC_MAGIC_MACROS_HPP
#include <graphlab_predefs.hpp>
#ifndef DISABLE_GRAPHLAB_CPPIPC_PROXY_GENERATION
#include <cppipc/client/object_proxy.hpp>
#include <cppipc/cppipc.hpp>
#endif
#include <cppipc/server/comm_server.hpp>
#include <cppipc/ipc_object_base.hpp>
#include <cppipc/registration_macros.hpp>
#include <cppipc/common/ipc_deserializer.hpp>
#include <serialization/serialization_includes.hpp>
#include <boost/preprocessor.hpp>
#define __GENERATE_REGISTRATION__(r, base_name, elem) \
REGISTER(base_name::BOOST_PP_TUPLE_ELEM(3,1,elem))
#define __GENERATE_PROXY_ARGS__(r, ni, i, elem) elem BOOST_PP_CAT(arg_,i) \
BOOST_PP_COMMA_IF(BOOST_PP_GREATER(ni, BOOST_PP_ADD(i, 1)))
#define __GENERATE_PROXY_CALL_ARGS__(r, ni, i, elem) BOOST_PP_CAT(arg_,i) \
BOOST_PP_COMMA_IF(BOOST_PP_GREATER(ni, BOOST_PP_ADD(i, 1)))
#define __GENERATE_BASE__(r, _, elem) \
virtual BOOST_PP_TUPLE_ELEM(3, 0, elem) \
BOOST_PP_TUPLE_ELEM(3,1, elem) ( \
BOOST_PP_SEQ_FOR_EACH_I(__GENERATE_PROXY_ARGS__, \
BOOST_PP_SEQ_SIZE(BOOST_PP_TUPLE_ELEM(3, 2,elem)), \
BOOST_PP_TUPLE_ELEM(3,2, elem)) \
) = 0;
#define __GENERATE_PROXY_CALLS__(r, base_name, elem) \
inline BOOST_PP_TUPLE_ELEM(3, 0, elem) \
BOOST_PP_TUPLE_ELEM(3,1, elem) \
( BOOST_PP_SEQ_FOR_EACH_I(__GENERATE_PROXY_ARGS__, \
BOOST_PP_SEQ_SIZE(BOOST_PP_TUPLE_ELEM(3, 2,elem)), \
BOOST_PP_TUPLE_ELEM(3,2, elem)) ) { \
return proxy.call(&base_name::BOOST_PP_TUPLE_ELEM(3,1,elem) \
BOOST_PP_COMMA_IF(BOOST_PP_GREATER(BOOST_PP_SEQ_SIZE(BOOST_PP_TUPLE_ELEM(3,2,elem)),0)) \
BOOST_PP_SEQ_FOR_EACH_I(__GENERATE_PROXY_CALL_ARGS__, \
BOOST_PP_SEQ_SIZE(BOOST_PP_TUPLE_ELEM(3, 2,elem)), \
BOOST_PP_TUPLE_ELEM(3, 2, elem)) \
); \
}
#define __ADD_PAREN_1__(A, B, C) ((A, B, C)) __ADD_PAREN_2__
#define __ADD_PAREN_2__(A, B, C) ((A, B, C)) __ADD_PAREN_1__
#define __ADD_PAREN_1___END
#define __ADD_PAREN_2___END
#define __ADD_PARENS__(INPUT) BOOST_PP_CAT(__ADD_PAREN_1__ INPUT,_END)
/**
* \ingroup cppipc
* Magic Interface generating macro.
* Like \ref GENERATE_INTERFACE_AND_PROXY but only generates the interface.
*
* To use, call with the 1st argument as the base name of the interface,
* and the 2nd argument as a sequence of:
* \code
* (return_type, function_name, (arg1type)(arg2type)(arg3type))
* \endcode
* To get a function with no arguments simply have an empty 3rd argument.
* (the 3rd argument is still needed. Observe the comma. It is just empty.)
* \code
* (return_type, function_name, )
* \endcode
* For instance,
* \code
* GENERATE_INTERFACE(object_base, object_proxy,
* (std::string, ping, (std::string))
* (int, add_one, (int))
* (int, add, (int)(int))
* )
* \endcode
* will create a base class called object_base with 3 functions, ping,
* add_one, and add, with the appropriate registration functions.
*
* The above macro generates the following code:
* \code
* class object_base : public cppipc::ipc_object_base{
* public:
* typedef object_proxy proxy_object_type;
* virtual ~object_base() { }
* virtual std::string ping(std::string) = 0;
* virtual int add_one(int) = 0;
* virtual int add_one(int, int) = 0;
*
* virtual void save(graphlab::oarchive& oarc) const {}
* virtual void load(graphlab::iarchive& iarc) {}
*
* REGISTRATION_BEGIN(test_object_base)
* REGISTER(test_object_base::ping)
* REGISTER(test_object_base::add)
* REGISTER(test_object_base::add_one)
* REGISTRATION_END
* };
* \endcode
*/
#define GENERATE_INTERFACE_NO_INLINE_DESTRUCTOR(base_name, proxy_name, functions) \
class base_name : public cppipc::ipc_object_base { \
public: \
typedef proxy_name proxy_object_type; \
virtual ~base_name(); \
inline virtual void save(graphlab::oarchive& oarc) const {} \
inline virtual void load(graphlab::iarchive& iarc) {} \
BOOST_PP_SEQ_FOR_EACH(__GENERATE_BASE__, _, __ADD_PARENS__(functions)) \
REGISTRATION_BEGIN(base_name) \
BOOST_PP_SEQ_FOR_EACH(__GENERATE_REGISTRATION__, base_name, __ADD_PARENS__(functions)) \
REGISTRATION_END \
};
#define GENERATE_INTERFACE(base_name, proxy_name, functions) \
class base_name : public cppipc::ipc_object_base { \
public: \
typedef proxy_name proxy_object_type; \
inline virtual ~base_name() { } \
inline virtual void save(graphlab::oarchive& oarc) const {} \
inline virtual void load(graphlab::iarchive& iarc) {} \
BOOST_PP_SEQ_FOR_EACH(__GENERATE_BASE__, _, __ADD_PARENS__(functions)) \
REGISTRATION_BEGIN(base_name) \
BOOST_PP_SEQ_FOR_EACH(__GENERATE_REGISTRATION__, base_name, __ADD_PARENS__(functions)) \
REGISTRATION_END \
};
#define GENERATE_BASE_DESTRUCTOR(base_name) \
base_name::~base_name() { }
/**
* \ingroup cppipc
* Magic proxy generating macro.
*
* Like \ref GENERATE_INTERFACE_AND_PROXY but only generates the proxy.
*
* To use, call with the 1st argument as the base name of the interface,
* the 2nd argument as the name of the proxy class,
* and the 3nd argument as a sequence of:
* \code
* (return_type, function_name, (arg1type)(arg2type)(arg3type))
* \endcode
* To get a function with no arguments simply have an empty 3rd argument.
* (the 3rd argument is still needed. Observe the comma. It is just empty.)
* \code
* (return_type, function_name, )
* \endcode
*
* For instance,
* \code
* GENERATE_PROXY(object_base, object_proxy,
* (std::string, ping, (std::string))
* (int, add_one, (int))
* (int, add, (int)(int))
* )
* \endcode
* will create a base class called object_base with 3 functions, ping,
* add_one, and add, with the appropriate registration functions, as well as
* a proxy object with the appropriate proxy forwarding calls.
*
* The above macro generates all of the following code:
* \code
* class object_proxy : public object_base {
* public:
* cppipc::object_proxy<object_base> proxy;
*
* inline test_object_proxy(cppipc::comm_client& comm,
* bool auto_create = true,
* size_t object_id = (size_t)(-1)):
* proxy(comm, auto_create, object_id){ }
*
* inline test_object_proxy(cppipc::comm_client& comm):proxy(comm){ }
*
* inline std::string ping(std::string arg_0) {
* return proxy.call(&object_base::ping, arg_0)
* }
* inline int add_one(int arg_0) {
* return proxy.call(&object_base::add_one, arg_0)
* }
* inline int add(int arg_0, int arg_1) {
* return proxy.call(&object_base::add, arg_0 , arg_1);
* }
* inline void save(graphlab::oarchive& oarc) const {
* oarc << proxy.get_object_id();
* }
* inline void load(graphlab::iarchive& iarc) {
* size_t objid; iarc >> objid;
* proxy.set_object_id(objid);
* }
* };
* \endcode
*/
#define GENERATE_PROXY(base_name, proxy_name, functions) \
class proxy_name : public base_name { \
public: \
cppipc::object_proxy<base_name> proxy; \
inline proxy_name(cppipc::comm_client& comm, \
bool auto_create = true, \
size_t object_id = (size_t)(-1)): \
proxy(comm, auto_create, object_id){ } \
inline void save(graphlab::oarchive& oarc) const { \
oarc << proxy.get_object_id(); \
} \
inline size_t __get_object_id() const { \
return proxy.get_object_id(); \
} \
inline void load(graphlab::iarchive& iarc) { \
size_t objid; iarc >> objid; \
proxy.set_object_id(objid); \
} \
BOOST_PP_SEQ_FOR_EACH(__GENERATE_PROXY_CALLS__, base_name, __ADD_PARENS__(functions)) \
};
/**
* \ingroup cppipc
* Magic Interface and proxy generating macro.
*
* To use, call with the 1st argument as the base name of the interface,
* the 2nd argument as the name of the proxy class,
* and the 3nd argument as a sequence of:
* \code
* (return_type, function_name, (arg1type)(arg2type)(arg3type))
* \endcode
* To get a function with no arguments simply have an empty 3rd argument.
* (the 3rd argument is still needed. Observe the comma. It is just empty.)
* \code
* (return_type, function_name, )
* \endcode
*
* For instance,
* \code
* GENERATE_INTERFACE_AND_PROXY(object_base, object_proxy,
* (std::string, ping, (std::string))
* (int, add_one, (int))
* (int, add, (int)(int))
* )
* \endcode
* will create a base class called object_base with 3 functions, ping,
* add_one, and add, with the appropriate registration functions, as well as
* a proxy object with the appropriate proxy forwarding calls.
*
* The above macro generates all of the following code:
* \code
* class proxy_object_type;
* class object_base : public cppipc::ipc_object_base {
* public:
* typedef object_proxy proxy_object_type;
* virtual inline ~object_base() { }
* virtual std::string ping(std::string) = 0;
* virtual int add_one(int) = 0;
* virtual int add_one(int, int) = 0;
*
* virtual void save(graphlab::oarchive& oarc) const {}
* virtual void load(graphlab::iarchive& iarc) {}
*
* REGISTRATION_BEGIN(test_object_base)
* REGISTER(test_object_base::ping)
* REGISTER(test_object_base::add)
* REGISTER(test_object_base::add_one)
* REGISTRATION_END
* };
*
* class object_proxy : public object_base {
* public:
* cppipc::object_proxy<object_base> proxy;
*
* inline test_object_proxy(cppipc::comm_client& comm,
* bool auto_create = true,
* size_t object_id = (size_t)(-1)):
* proxy(comm, auto_create, object_id){ }
*
* inline std::string ping(std::string arg_0) {
* return proxy.call(&object_base::ping, arg_0)
* }
* inline int add_one(int arg_0) {
* return proxy.call(&object_base::add_one, arg_0)
* }
* inline int add(int arg_0, int arg_1) {
* return proxy.call(&object_base::add, arg_0 , arg_1);
* }
* inline void save(graphlab::oarchive& oarc) const {
* oarc << proxy.get_object_id();
* }
* inline void load(graphlab::iarchive& iarc) {
* size_t objid; iarc >> objid;
* proxy.set_object_id(objid);
* }
* };
* \endcode
*/
#ifdef DISABLE_GRAPHLAB_CPPIPC_PROXY_GENERATION
#define GENERATE_INTERFACE_AND_PROXY(base_name, proxy_name, functions) \
class proxy_name; \
GENERATE_INTERFACE(base_name, proxy_name, functions)
#define GENERATE_INTERFACE_AND_PROXY_NO_INLINE_DESTRUCTOR(base_name, proxy_name, functions) \
class proxy_name; \
GENERATE_INTERFACE_NO_INLINE_DESTRUCTOR(base_name, proxy_name, functions)
#else
#define GENERATE_INTERFACE_AND_PROXY(base_name, proxy_name, functions) \
class proxy_name; \
GENERATE_INTERFACE(base_name, proxy_name, functions) \
GENERATE_PROXY(base_name, proxy_name, functions)
#define GENERATE_INTERFACE_AND_PROXY_NO_INLINE_DESTRUCTOR(base_name, proxy_name, functions) \
class proxy_name; \
GENERATE_INTERFACE_NO_INLINE_DESTRUCTOR(base_name, proxy_name, functions) \
GENERATE_PROXY(base_name, proxy_name, functions)
#endif
#endif