Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NativeWrapperDecls.h
Go to the documentation of this file.
1 /* Copyright (C) 2009 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. 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.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <boost/preprocessor/punctuation/comma_if.hpp>
19 #include <boost/preprocessor/repetition/repeat.hpp>
20 
21 // Define lots of useful macros:
22 
23 // Varieties of comma-separated list to fit on the head/tail/whole of another comma-separated list
24 #define NUMBERED_LIST_HEAD(z, i, data) data##i,
25 #define NUMBERED_LIST_TAIL(z, i, data) ,data##i
26 #define NUMBERED_LIST_BALANCED(z, i, data) BOOST_PP_COMMA_IF(i) data##i
27 // Some other things
28 #define TYPED_ARGS(z, i, data) , T##i a##i
29 #define CONVERT_ARG(z, i, data) T##i a##i; if (! ScriptInterface::FromJSVal<T##i>(cx, i < argc ? JS_ARGV(cx, vp)[i] : JSVAL_VOID, a##i)) return JS_FALSE;
30 
31 // List-generating macros, named roughly after their first list item
32 #define TYPENAME_T0_HEAD(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_HEAD, typename T) // "typename T0, typename T1, "
33 #define TYPENAME_T0_TAIL(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_TAIL, typename T) // ", typename T0, typename T1"
34 #define T0(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_BALANCED, T) // "T0, T1"
35 #define T0_HEAD(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_HEAD, T) // "T0, T1, "
36 #define T0_TAIL(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_TAIL, T) // ", T0, T1"
37 #define T0_A0(z, i) BOOST_PP_REPEAT_##z (i, TYPED_ARGS, ~) // "T0 a0, T1 a1"
38 #define A0(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_BALANCED, a) // "a0, a1"
39 #define A0_TAIL(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_TAIL, a) // ", a0, a1"
40 
41 // Define RegisterFunction<TR, T0..., f>
42 #define OVERLOADS(z, i, data) \
43  template <typename R, TYPENAME_T0_HEAD(z,i) R (*fptr) ( void* T0_TAIL(z,i) )> \
44  void RegisterFunction(const char* name) { \
45  Register(name, call<R, T0_HEAD(z,i) fptr>, nargs<0 T0_TAIL(z,i)>()); \
46  }
47 BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
48 #undef OVERLOADS
49 
50 // JSFastNative-compatible function that wraps the function identified in the template argument list
51 // (Definition comes later, since it depends on some things we haven't defined yet)
52 #define OVERLOADS(z, i, data) \
53  template <typename R, TYPENAME_T0_HEAD(z,i) R (*fptr) ( void* T0_TAIL(z,i) )> \
54  static JSBool call(JSContext* cx, uintN argc, jsval* vp);
55 BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
56 #undef OVERLOADS
57 
58 // Similar, for class methods
59 #define OVERLOADS(z, i, data) \
60  template <typename R, TYPENAME_T0_HEAD(z,i) JSClass* CLS, typename TC, R (TC::*fptr) ( T0(z,i) )> \
61  static JSBool callMethod(JSContext* cx, uintN argc, jsval* vp);
62 BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
63 #undef OVERLOADS
64 
65 // Argument-number counter
66 #define OVERLOADS(z, i, data) \
67  template <int dummy TYPENAME_T0_TAIL(z,i)> /* add a dummy parameter so we still compile with 0 template args */ \
68  static size_t nargs() { return i; }
69 BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
70 #undef OVERLOADS
#define SCRIPT_INTERFACE_MAX_ARGS
#define OVERLOADS(z, i, data)