Pyrogenesis  trunk
Component.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_COMPONENT
19 #define INCLUDED_COMPONENT
20 
29 
30 #define REGISTER_COMPONENT_TYPE(cname) \
31  void RegisterComponentType_##cname(CComponentManager& mgr) \
32  { \
33  mgr.RegisterComponentType(CCmp##cname::GetInterfaceId(), CID_##cname, CCmp##cname::Allocate, CCmp##cname::Deallocate, #cname, CCmp##cname::GetSchema()); \
34  CCmp##cname::ClassInit(mgr); \
35  }
36 
37 #define REGISTER_COMPONENT_SCRIPT_WRAPPER(cname) \
38  void RegisterComponentType_##cname(CComponentManager& mgr) \
39  { \
40  mgr.RegisterComponentTypeScriptWrapper(CCmp##cname::GetInterfaceId(), CID_##cname, CCmp##cname::Allocate, CCmp##cname::Deallocate, #cname, CCmp##cname::GetSchema()); \
41  CCmp##cname::ClassInit(mgr); \
42  }
43 
44 #define DEFAULT_COMPONENT_ALLOCATOR(cname) \
45  static IComponent* Allocate(ScriptInterface&, JS::HandleValue) { return new CCmp##cname(); } \
46  static void Deallocate(IComponent* cmp) { delete static_cast<CCmp##cname*> (cmp); } \
47  virtual int GetComponentTypeId() const \
48  { \
49  return CID_##cname; \
50  }
51 
52 #define DEFAULT_SCRIPT_WRAPPER(cname) \
53  static void ClassInit(CComponentManager& UNUSED(componentManager)) { } \
54  static IComponent* Allocate(ScriptInterface& scriptInterface, JS::HandleValue instance) \
55  { \
56  return new CCmp##cname(scriptInterface, instance); \
57  } \
58  static void Deallocate(IComponent* cmp) \
59  { \
60  delete static_cast<CCmp##cname*> (cmp); \
61  } \
62  CCmp##cname(ScriptInterface& scriptInterface, JS::HandleValue instance) : m_Script(scriptInterface, instance) { } \
63  static std::string GetSchema() \
64  { \
65  return "<a:component type='script-wrapper'/><empty/>"; \
66  } \
67  virtual void Init(const CParamNode& paramNode) \
68  { \
69  m_Script.Init(paramNode, GetEntityId()); \
70  } \
71  virtual void Deinit() \
72  { \
73  m_Script.Deinit(); \
74  } \
75  virtual void HandleMessage(const CMessage& msg, bool global) \
76  { \
77  m_Script.HandleMessage(msg, global); \
78  } \
79  virtual void Serialize(ISerializer& serialize) \
80  { \
81  m_Script.Serialize(serialize); \
82  } \
83  virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) \
84  { \
85  m_Script.Deserialize(paramNode, deserialize, GetEntityId()); \
86  } \
87  virtual JS::Value GetJSInstance() const \
88  { \
89  return m_Script.GetInstance(); \
90  } \
91  virtual int GetComponentTypeId() const \
92  { \
93  return CID_##cname; \
94  } \
95  private: \
96  CComponentTypeScript m_Script; \
97  public:
98 
99 #define DEFAULT_MOCK_COMPONENT() \
100  virtual int GetComponentTypeId() const \
101  { \
102  return -1; \
103  } \
104  virtual void Init(const CParamNode& UNUSED(paramNode)) \
105  { \
106  } \
107  virtual void Deinit() \
108  { \
109  } \
110  virtual void Serialize(ISerializer& UNUSED(serialize)) \
111  { \
112  } \
113  virtual void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& UNUSED(deserialize)) \
114  { \
115  } \
116 
117 #endif // INCLUDED_COMPONENT