forked from mirrors/0ad
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:
@@ -22,10 +22,6 @@
|
||||
|
||||
#include "ps/CLogger.h"
|
||||
|
||||
#include <boost/preprocessor/repetition/enum_params.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_trailing_params.hpp>
|
||||
#include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
|
||||
|
||||
class CSimContext;
|
||||
class CParamNode;
|
||||
class ISerializer;
|
||||
@@ -33,6 +29,7 @@ class IDeserializer;
|
||||
|
||||
class CComponentTypeScript
|
||||
{
|
||||
NONCOPYABLE(CComponentTypeScript);
|
||||
public:
|
||||
CComponentTypeScript(ScriptInterface& scriptInterface, JS::HandleValue instance);
|
||||
|
||||
@@ -45,43 +42,30 @@ public:
|
||||
void Serialize(ISerializer& serialize);
|
||||
void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent);
|
||||
|
||||
// Use Boost.PP to define:
|
||||
// template<typename R> R Call(const char* funcname) const;
|
||||
// template<typename R, typename T0> R Call(const char* funcname, const T0& a0) const;
|
||||
// ...
|
||||
// template<typename R> void CallRef(const char* funcname, R ret) const;
|
||||
// template<typename R, typename T0> void CallRef(const char* funcname, const T0& a0, R ret) const;
|
||||
// ...
|
||||
// void CallVoid(const char* funcname) const;
|
||||
// template<typename T0> void CallVoid(const char* funcname, const T0& a0) const;
|
||||
// ...
|
||||
|
||||
// CallRef is mainly used for returning script values with correct stack rooting.
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template<typename R BOOST_PP_ENUM_TRAILING_PARAMS(i, typename T)> \
|
||||
R Call(const char* funcname BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(i, const T, &a)) const \
|
||||
{ \
|
||||
R ret; \
|
||||
if (m_ScriptInterface.CallFunction(m_Instance, funcname BOOST_PP_ENUM_TRAILING_PARAMS(i, a), ret)) \
|
||||
return ret; \
|
||||
LOGERROR("Error calling component script function %s", funcname); \
|
||||
return R(); \
|
||||
} \
|
||||
template<typename R BOOST_PP_ENUM_TRAILING_PARAMS(i, typename T)> \
|
||||
void CallRef(const char* funcname BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(i, const T, &a), R ret) const \
|
||||
{ \
|
||||
if (!m_ScriptInterface.CallFunction(m_Instance, funcname BOOST_PP_ENUM_TRAILING_PARAMS(i, a), ret)) \
|
||||
LOGERROR("Error calling component script function %s", funcname); \
|
||||
} \
|
||||
BOOST_PP_IF(i, template<, ) BOOST_PP_ENUM_PARAMS(i, typename T) BOOST_PP_IF(i, >, ) \
|
||||
void CallVoid(const char* funcname BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(i, const T, &a)) const \
|
||||
{ \
|
||||
if (m_ScriptInterface.CallFunctionVoid(m_Instance, funcname BOOST_PP_ENUM_TRAILING_PARAMS(i, a))) \
|
||||
return; \
|
||||
LOGERROR("Error calling component script function %s", funcname); \
|
||||
template<typename R, typename... Ts>
|
||||
R Call(const char* funcname, const Ts&... params) const
|
||||
{
|
||||
R ret;
|
||||
if (m_ScriptInterface.CallFunction(m_Instance, funcname, ret, params...))
|
||||
return ret;
|
||||
LOGERROR("Error calling component script function %s", funcname);
|
||||
return R();
|
||||
}
|
||||
|
||||
// CallRef is mainly used for returning script values with correct stack rooting.
|
||||
template<typename R, typename... Ts>
|
||||
void CallRef(const char* funcname, R ret, const Ts&... params) const
|
||||
{
|
||||
if (!m_ScriptInterface.CallFunction(m_Instance, funcname, ret, params...))
|
||||
LOGERROR("Error calling component script function %s", funcname);
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
void CallVoid(const char* funcname, const Ts&... params) const
|
||||
{
|
||||
if (!m_ScriptInterface.CallFunctionVoid(m_Instance, funcname, params...))
|
||||
LOGERROR("Error calling component script function %s", funcname);
|
||||
}
|
||||
BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef OVERLOADS
|
||||
|
||||
private:
|
||||
ScriptInterface& m_ScriptInterface;
|
||||
@@ -89,8 +73,6 @@ private:
|
||||
bool m_HasCustomSerialize;
|
||||
bool m_HasCustomDeserialize;
|
||||
bool m_HasNullSerialize;
|
||||
|
||||
NONCOPYABLE(CComponentTypeScript);
|
||||
};
|
||||
|
||||
#endif // INCLUDED_SCRIPTCOMPONENT
|
||||
|
||||
Reference in New Issue
Block a user