Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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&, jsval) { return new CCmp##cname(); } \
46  static void Deallocate(IComponent* cmp) { delete static_cast<CCmp##cname*> (cmp); } \
47 
48 #define DEFAULT_SCRIPT_WRAPPER(cname) \
49  static void ClassInit(CComponentManager& UNUSED(componentManager)) { } \
50  static IComponent* Allocate(ScriptInterface& scriptInterface, jsval instance) \
51  { \
52  return new CCmp##cname(scriptInterface, instance); \
53  } \
54  static void Deallocate(IComponent* cmp) \
55  { \
56  delete static_cast<CCmp##cname*> (cmp); \
57  } \
58  CCmp##cname(ScriptInterface& scriptInterface, jsval instance) : m_Script(scriptInterface, instance) { } \
59  static std::string GetSchema() \
60  { \
61  return "<a:component type='script-wrapper'/><empty/>"; \
62  } \
63  virtual void Init(const CParamNode& paramNode) \
64  { \
65  m_Script.Init(paramNode, GetEntityId()); \
66  } \
67  virtual void Deinit() \
68  { \
69  m_Script.Deinit(); \
70  } \
71  virtual void HandleMessage(const CMessage& msg, bool global) \
72  { \
73  m_Script.HandleMessage(msg, global); \
74  } \
75  virtual void Serialize(ISerializer& serialize) \
76  { \
77  m_Script.Serialize(serialize); \
78  } \
79  virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) \
80  { \
81  m_Script.Deserialize(paramNode, deserialize, GetEntityId()); \
82  } \
83  virtual jsval GetJSInstance() const \
84  { \
85  return m_Script.GetInstance(); \
86  } \
87  private: \
88  CComponentTypeScript m_Script; \
89  public:
90 
91 #define DEFAULT_MOCK_COMPONENT() \
92  virtual void Init(const CParamNode& UNUSED(paramNode)) \
93  { \
94  } \
95  virtual void Deinit() \
96  { \
97  } \
98  virtual void Serialize(ISerializer& UNUSED(serialize)) \
99  { \
100  } \
101  virtual void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& UNUSED(deserialize)) \
102  { \
103  } \
104 
105 #endif // INCLUDED_COMPONENT