Pyrogenesis  trunk
IComponent.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 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_ICOMPONENT
19 #define INCLUDED_ICOMPONENT
20 
21 #include "Components.h"
22 #include "Message.h"
23 #include "Entity.h"
24 #include "SimContext.h"
26 
27 class CParamNode;
28 class CMessage;
29 class ISerializer;
30 class IDeserializer;
31 
33 {
34 public:
35  // Component allocation types
36  using AllocFunc = IComponent* (*)(const ScriptInterface& scriptInterface, JS::HandleValue ctor);
37  using DeallocFunc = void (*)(IComponent*);
38 
39  virtual ~IComponent();
40 
41  static std::string GetSchema();
42 
43  static void RegisterComponentType(CComponentManager& mgr, EInterfaceId iid, EComponentTypeId cid, AllocFunc alloc, DeallocFunc dealloc, const char* name, const std::string& schema);
44  static void RegisterComponentTypeScriptWrapper(CComponentManager& mgr, EInterfaceId iid, EComponentTypeId cid, AllocFunc alloc, DeallocFunc dealloc, const char* name, const std::string& schema);
45 
46  virtual void Init(const CParamNode& paramNode) = 0;
47  virtual void Deinit() = 0;
48 
49  virtual void HandleMessage(const CMessage& msg, bool global);
50 
53 
55 
57 
58  const CSimContext& GetSimContext() const { return *m_SimContext; }
59  void SetSimContext(const CSimContext& context) { m_SimContext = &context; }
60 
61  static u8 GetSerializationVersion() { return 0; }
62  virtual void Serialize(ISerializer& serialize) = 0;
63  virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) = 0;
64 
65  /**
66  * Returns false by default, indicating that a scripted wrapper of this IComponent is not supported.
67  * Derrived classes should return true if they implement such a wrapper.
68  */
69  virtual bool NewJSObject(const ScriptInterface& scriptInterface, JS::MutableHandleObject out) const;
70  virtual JS::Value GetJSInstance() const;
71  virtual int GetComponentTypeId() const = 0;
72 
73 private:
76 };
77 
78 #endif // INCLUDED_ICOMPONENT
An entity initialisation parameter node.
Definition: ParamNode.h:150
Definition: IComponent.h:32
EComponentTypeId
Definition: Components.h:50
const CSimContext * m_SimContext
Definition: IComponent.h:75
virtual JS::Value GetJSInstance() const
Definition: IComponent.cpp:55
virtual void Serialize(ISerializer &serialize)=0
Serialization interface; see serialization overview.
Definition: ISerializer.h:120
virtual void Init(const CParamNode &paramNode)=0
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:421
Object wrapping an entity_id_t, with a SEntityComponentCache to support fast QueryInterface() / CmpPt...
Definition: Entity.h:79
virtual int GetComponentTypeId() const =0
void(*)(IComponent *) DeallocFunc
Definition: IComponent.h:37
CEntityHandle GetSystemEntity() const
Definition: IComponent.h:56
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
uint8_t u8
Definition: types.h:37
static void RegisterComponentType(CComponentManager &mgr, EInterfaceId iid, EComponentTypeId cid, AllocFunc alloc, DeallocFunc dealloc, const char *name, const std::string &schema)
Definition: IComponent.cpp:36
Config::Value_type Value
Definition: json_spirit_value.h:182
Definition: ComponentManager.h:39
static u8 GetSerializationVersion()
Definition: IComponent.h:61
entity_id_t GetEntityId() const
Definition: IComponent.h:54
virtual bool NewJSObject(const ScriptInterface &scriptInterface, JS::MutableHandleObject out) const
Returns false by default, indicating that a scripted wrapper of this IComponent is not supported...
Definition: IComponent.cpp:50
void SetEntityHandle(CEntityHandle ent)
Definition: IComponent.h:52
virtual ~IComponent()
Definition: IComponent.cpp:26
virtual void Deserialize(const CParamNode &paramNode, IDeserializer &deserialize)=0
CEntityHandle GetSystemEntity() const
Definition: SimContext.h:49
const CSimContext & GetSimContext() const
Definition: IComponent.h:58
CEntityHandle GetEntityHandle() const
Definition: IComponent.h:51
entity_id_t GetId() const
Definition: Entity.h:88
virtual void HandleMessage(const CMessage &msg, bool global)
Definition: IComponent.cpp:46
virtual void Deinit()=0
Abstraction around a SpiderMonkey JS::Realm.
Definition: ScriptInterface.h:71
IComponent *(*)(const ScriptInterface &scriptInterface, JS::HandleValue ctor) AllocFunc
Definition: IComponent.h:36
static std::string GetSchema()
Definition: IComponent.cpp:30
u32 entity_id_t
Entity ID type.
Definition: Entity.h:23
void SetSimContext(const CSimContext &context)
Definition: IComponent.h:59
EInterfaceId
Definition: Components.h:39
CEntityHandle m_EntityHandle
Definition: IComponent.h:74
static void RegisterComponentTypeScriptWrapper(CComponentManager &mgr, EInterfaceId iid, EComponentTypeId cid, AllocFunc alloc, DeallocFunc dealloc, const char *name, const std::string &schema)
Definition: IComponent.cpp:41
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:34
Definition: Message.h:23