mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-10-13 15:34:37 +00:00
Employ some variadic macros to make some of the C++ -> JS function calling code nicer.
Template-ize CallFunctionVoid. Changes CallFunction parameter order to make template parameter deduction with variadic parameters work nicely. Reviewed By: Itms, wraitii, Yves Differential Revision: https://code.wildfiregames.com/D77 This was SVN commit r19183.
This commit is contained in:
@@ -30,10 +30,6 @@ template <typename T> struct MaybeRef;
|
||||
#define NUMBERED_LIST_TAIL_MAYBE_REF(z, i, data) , typename MaybeRef<data##i>::Type
|
||||
#define NUMBERED_LIST_BALANCED(z, i, data) BOOST_PP_COMMA_IF(i) data##i
|
||||
#define NUMBERED_LIST_BALANCED_MAYBE_REF(z, i, data) BOOST_PP_COMMA_IF(i) typename MaybeRef<data##i>::Type
|
||||
// Some other things
|
||||
#define TYPED_ARGS(z, i, data) , T##i a##i
|
||||
#define TYPED_ARGS_MAYBE_REF(z, i, data) , typename MaybeRef<T##i>::Type a##i
|
||||
#define TYPED_ARGS_CONST_REF(z, i, data) , const T##i& a##i
|
||||
|
||||
// TODO: We allow optional parameters when the C++ type can be converted from JS::UndefinedValue.
|
||||
// FromJSVal is expected to either set a##i or return false (otherwise we could get undefined
|
||||
@@ -54,23 +50,17 @@ template <typename T> struct MaybeRef;
|
||||
|
||||
// List-generating macros, named roughly after their first list item
|
||||
#define TYPENAME_T0_HEAD(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_HEAD, typename T) // "typename T0, typename T1, "
|
||||
#define TYPENAME_T0_TAIL(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_TAIL, typename T) // ", typename T0, typename T1"
|
||||
#define T0(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_BALANCED, T) // "T0, T1"
|
||||
#define T0_MAYBE_REF(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_BALANCED_MAYBE_REF, T) // "const T0&, T1"
|
||||
#define T0_HEAD(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_HEAD, T) // "T0, T1, "
|
||||
#define T0_TAIL(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_TAIL, T) // ", T0, T1"
|
||||
#define T0_TAIL_MAYBE_REF(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_TAIL_MAYBE_REF, T) // ", const T0&, T1"
|
||||
#define T0_A0(z, i) BOOST_PP_REPEAT_##z (i, TYPED_ARGS, ~) // ",T0 a0, T1 a1"
|
||||
#define T0_A0_MAYBE_REF(z, i) BOOST_PP_REPEAT_##z (i, TYPED_ARGS_MAYBE_REF, ~) // ",const T0& a0, T1 a1"
|
||||
#define T0_A0_TAIL_CONST_REF(z, i) BOOST_PP_REPEAT_##z (i, TYPED_ARGS_CONST_REF, ~) // ", const T0& a0, const T1& a1"
|
||||
#define A0(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_BALANCED, a) // "a0, a1"
|
||||
#define A0_TAIL(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_TAIL, a) // ", a0, a1"
|
||||
|
||||
// Define RegisterFunction<TR, T0..., f>
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template <typename R, TYPENAME_T0_HEAD(z,i) R (*fptr) ( ScriptInterface::CxPrivate* T0_TAIL_MAYBE_REF(z,i) )> \
|
||||
void RegisterFunction(const char* name) { \
|
||||
Register(name, call<R, T0_HEAD(z,i) fptr>, nargs<0 T0_TAIL(z,i)>()); \
|
||||
Register(name, call<R T0_TAIL(z,i), fptr>, nargs<T0(z,i)>()); \
|
||||
}
|
||||
BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef OVERLOADS
|
||||
@@ -98,33 +88,24 @@ BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef OVERLOADS
|
||||
|
||||
// Argument-number counter
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template <int dummy TYPENAME_T0_TAIL(z,i)> /* add a dummy parameter so we still compile with 0 template args */ \
|
||||
static size_t nargs() { return i; }
|
||||
BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef OVERLOADS
|
||||
template<typename... Ts>
|
||||
static size_t nargs() { return sizeof...(Ts); }
|
||||
|
||||
// Call the named property on the given object
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template <typename R TYPENAME_T0_TAIL(z, i)> \
|
||||
bool CallFunction(JS::HandleValue val, const char* name T0_A0_TAIL_CONST_REF(z,i), R& ret) const;
|
||||
BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef OVERLOADS
|
||||
template<typename R, typename... Ts>
|
||||
bool CallFunction(JS::HandleValue val, const char* name, R& ret, const Ts&... params) const;
|
||||
|
||||
// Implicit conversion from JS::Rooted<R>* to JS::MutableHandle<R> does not work with template argument deduction
|
||||
// (only exact type matches allowed). We need this overload to allow passing Rooted<R>* using the & operator
|
||||
// (as people would expect it to work based on the SpiderMonkey rooting guide).
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template <typename R TYPENAME_T0_TAIL(z, i)> \
|
||||
bool CallFunction(JS::HandleValue val, const char* name T0_A0_TAIL_CONST_REF(z,i), JS::Rooted<R>* ret) const;
|
||||
BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef OVERLOADS
|
||||
template<typename R, typename... Ts>
|
||||
bool CallFunction(JS::HandleValue val, const char* name, JS::Rooted<R>* ret, const Ts&... params) const;
|
||||
|
||||
// This overload is for the case when a JS::MutableHandle<R> type gets passed into CallFunction directly and
|
||||
// without requiring implicit conversion.
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template <typename R TYPENAME_T0_TAIL(z, i)> \
|
||||
bool CallFunction(JS::HandleValue val, const char* name T0_A0_TAIL_CONST_REF(z,i), JS::MutableHandle<R> ret) const;
|
||||
BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef OVERLOADS
|
||||
template<typename R, typename... Ts>
|
||||
bool CallFunction(JS::HandleValue val, const char* name, JS::MutableHandle<R> ret, const Ts&... params) const;
|
||||
|
||||
// Call the named property on the given object, with void return type
|
||||
template<typename... Ts> \
|
||||
bool CallFunctionVoid(JS::HandleValue val, const char* name, const Ts&... params) const;
|
||||
|
||||
Reference in New Issue
Block a user