Pyrogenesis  trunk
ScriptComponent.h
Go to the documentation of this file.
1 /* Copyright (C) 2017 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 class CSimContext;
26 class CParamNode;
27 class ISerializer;
28 class IDeserializer;
29 
31 {
33 public:
34  CComponentTypeScript(ScriptInterface& scriptInterface, JS::HandleValue instance);
35 
36  JS::Value GetInstance() const { return m_Instance.get(); }
37 
38  void Init(const CParamNode& paramNode, entity_id_t ent);
39  void Deinit();
40  void HandleMessage(const CMessage& msg, bool global);
41 
42  void Serialize(ISerializer& serialize);
43  void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent);
44 
45  template<typename R, typename... Ts>
46  R Call(const char* funcname, const Ts&... params) const
47  {
48  R ret;
49  if (m_ScriptInterface.CallFunction(m_Instance, funcname, ret, params...))
50  return ret;
51  LOGERROR("Error calling component script function %s", funcname);
52  return R();
53  }
54 
55  // CallRef is mainly used for returning script values with correct stack rooting.
56  template<typename R, typename... Ts>
57  void CallRef(const char* funcname, R ret, const Ts&... params) const
58  {
59  if (!m_ScriptInterface.CallFunction(m_Instance, funcname, ret, params...))
60  LOGERROR("Error calling component script function %s", funcname);
61  }
62 
63  template<typename... Ts>
64  void CallVoid(const char* funcname, const Ts&... params) const
65  {
66  if (!m_ScriptInterface.CallFunctionVoid(m_Instance, funcname, params...))
67  LOGERROR("Error calling component script function %s", funcname);
68  }
69 
70 private:
72  JS::PersistentRootedValue m_Instance;
76 };
77 
78 #endif // INCLUDED_SCRIPTCOMPONENT
An entity initialisation parameter node.
Definition: ParamNode.h:148
NONCOPYABLE(CComponentTypeScript)
#define LOGERROR(...)
Definition: CLogger.h:36
JS::PersistentRootedValue m_Instance
Definition: ScriptComponent.h:72
CComponentTypeScript(ScriptInterface &scriptInterface, JS::HandleValue instance)
Definition: ScriptComponent.cpp:25
bool CallFunction(JS::HandleValue val, const char *name, R &ret, const Ts &...params) const
Definition: NativeWrapperDefns.h:183
Serialization interface; see serialization overview.
Definition: ISerializer.h:120
bool m_HasCustomSerialize
Definition: ScriptComponent.h:73
void CallRef(const char *funcname, R ret, const Ts &...params) const
Definition: ScriptComponent.h:57
#define R(t)
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
bool CallFunctionVoid(JS::HandleValue val, const char *name, const Ts &...params) const
Definition: NativeWrapperDefns.h:222
void Serialize(ISerializer &serialize)
Definition: ScriptComponent.cpp:69
void Deinit()
Definition: ScriptComponent.cpp:51
void Deserialize(const CParamNode &paramNode, IDeserializer &deserialize, entity_id_t ent)
Definition: ScriptComponent.cpp:93
Definition: ScriptComponent.h:30
R Call(const char *funcname, const Ts &...params) const
Definition: ScriptComponent.h:46
Config::Value_type Value
Definition: json_spirit_value.h:181
JS::Value GetInstance() const
Definition: ScriptComponent.h:36
bool m_HasCustomDeserialize
Definition: ScriptComponent.h:74
void CallVoid(const char *funcname, const Ts &...params) const
Definition: ScriptComponent.h:64
bool m_HasNullSerialize
Definition: ScriptComponent.h:75
ScriptInterface & m_ScriptInterface
Definition: ScriptComponent.h:71
Abstraction around a SpiderMonkey JSContext.
Definition: ScriptInterface.h:71
u32 entity_id_t
Entity ID type.
Definition: Entity.h:23
void HandleMessage(const CMessage &msg, bool global)
Definition: ScriptComponent.cpp:56
void Init(const CParamNode &paramNode, entity_id_t ent)
Definition: ScriptComponent.cpp:44
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:34
Definition: Message.h:24