Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ScriptComponent.h
Go to the documentation of this file.
1 /* Copyright (C) 2010 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 #ifndef INCLUDED_SCRIPTCOMPONENT
19 #define INCLUDED_SCRIPTCOMPONENT
20 
22 
23 #include "ps/CLogger.h"
24 
25 #include <boost/preprocessor/repetition/enum_params.hpp>
26 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
27 #include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
28 
29 class CSimContext;
30 class CParamNode;
31 class ISerializer;
32 class IDeserializer;
33 
35 {
36 public:
37  CComponentTypeScript(ScriptInterface& scriptInterface, jsval instance);
38 
39  jsval GetInstance() const { return m_Instance.get(); }
40 
41  void Init(const CParamNode& paramNode, entity_id_t ent);
42  void Deinit();
43  void HandleMessage(const CMessage& msg, bool global);
44 
45  void Serialize(ISerializer& serialize);
46  void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent);
47 
48  // Use Boost.PP to define:
49  // template<typename R> R Call(const char* funcname);
50  // template<typename R, typename T0> R Call(const char* funcname, const T0& a0);
51  // ...
52  // void CallVoid(const char* funcname);
53  // template<typename T0> void CallVoid(const char* funcname, const T0& a0);
54  // ...
55 #define OVERLOADS(z, i, data) \
56  template<typename R BOOST_PP_ENUM_TRAILING_PARAMS(i, typename T)> \
57  R Call(const char* funcname BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(i, const T, &a)) \
58  { \
59  R ret; \
60  if (m_ScriptInterface.CallFunction(m_Instance.get(), funcname BOOST_PP_ENUM_TRAILING_PARAMS(i, a), ret)) \
61  return ret; \
62  LOGERROR(L"Error calling component script function %hs", funcname); \
63  return R(); \
64  } \
65  BOOST_PP_IF(i, template<, ) BOOST_PP_ENUM_PARAMS(i, typename T) BOOST_PP_IF(i, >, ) \
66  void CallVoid(const char* funcname BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(i, const T, &a)) \
67  { \
68  if (m_ScriptInterface.CallFunctionVoid(m_Instance.get(), funcname BOOST_PP_ENUM_TRAILING_PARAMS(i, a))) \
69  return; \
70  LOGERROR(L"Error calling component script function %hs", funcname); \
71  }
72 BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
73 #undef OVERLOADS
74 
75 private:
81 
83 };
84 
85 #endif // INCLUDED_SCRIPTCOMPONENT
An entity initialisation parameter node.
Definition: ParamNode.h:112
NONCOPYABLE(CComponentTypeScript)
Serialization interface; see serialization overview.
Definition: ISerializer.h:120
CComponentTypeScript(ScriptInterface &scriptInterface, jsval instance)
#define SCRIPT_INTERFACE_MAX_ARGS
Contains pointers to various &#39;global&#39; objects that are needed by the simulation code, to allow easy access without using real (evil) global variables.
Definition: SimContext.h:32
void Serialize(ISerializer &serialize)
void Deserialize(const CParamNode &paramNode, IDeserializer &deserialize, entity_id_t ent)
#define OVERLOADS(z, i, data)
ScriptInterface & m_ScriptInterface
jsval get() const
Returns the current value (or JSVAL_VOID if uninitialised).
Definition: ScriptVal.cpp:45
Abstraction around a SpiderMonkey JSContext.
CScriptValRooted m_Instance
jsval GetInstance() const
u32 entity_id_t
Entity ID type.
Definition: Entity.h:24
void HandleMessage(const CMessage &msg, bool global)
void Init(const CParamNode &paramNode, entity_id_t ent)
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:34