ezEngine  Milestone 7
ComponentManager.h
1 #pragma once
2 
3 #include <Foundation/Types/Delegate.h>
4 #include <Foundation/Containers/HybridArray.h>
5 #include <Foundation/Containers/IdTable.h>
6 #include <Foundation/Memory/BlockStorage.h>
8 
9 #include <Core/World/Declarations.h>
10 #include <Core/World/Component.h>
11 
18 class EZ_CORE_DLL ezComponentManagerBase
19 {
20 protected:
22  virtual ~ezComponentManagerBase();
23 
24 public:
26  ezWorld* GetWorld() const;
27 
29  bool IsValidComponent(const ezComponentHandle& component) const;
30 
32  bool TryGetComponent(const ezComponentHandle& component, ezComponent*& out_pComponent) const;
33 
35  ezUInt32 GetComponentCount() const;
36 
38  virtual ezComponentHandle CreateComponent() = 0;
39 
41  void DeleteComponent(const ezComponentHandle& component);
42 
44  virtual const ezRTTI* GetComponentType() const = 0;
45 
46 protected:
47  friend class ezWorld;
48  friend class ezInternal::WorldData;
49  friend class ezMemoryUtils;
50 
53 
56  {
57  enum Phase
58  {
59  PreAsync,
60  Async,
61  PostAsync,
62  PostTransform,
63  PHASE_COUNT
64  };
65 
66  UpdateFunctionDesc(const UpdateFunction& function, const char* szFunctionName)
67  {
68  m_Function = function;
69  m_szFunctionName = szFunctionName;
70  m_Phase = PreAsync;
71  m_uiGranularity = 0;
72  }
73 
75  const char* m_szFunctionName;
77  Phase m_Phase;
78  ezUInt32 m_uiGranularity;
79  };
80 
82  void RegisterUpdateFunction(const UpdateFunctionDesc& desc);
83 
85  void DeregisterUpdateFunction(const UpdateFunctionDesc& desc);
86 
88  ezAllocatorBase* GetAllocator();
89 
91  ezInternal::WorldLargeBlockAllocator* GetBlockAllocator();
92 
93 protected:
95  // internal methods
97 
98  ezComponentHandle CreateComponent(ComponentStorageEntry storageEntry, ezUInt16 uiTypeId);
99  void DeinitializeComponent(ezComponent* pComponent);
100  void DeleteComponent(ComponentStorageEntry storageEntry);
101  virtual void DeleteDeadComponent(ComponentStorageEntry storageEntry, ezComponent*& out_pMovedComponent);
102 
103  static ezComponentId GetIdFromHandle(const ezComponentHandle& component);
104  static ezComponentHandle GetHandle(ezGenericComponentId internalId, ezUInt16 uiTypeId);
105 
107 
109 
110 private:
112  virtual ezResult Initialize() { return EZ_SUCCESS; }
113 
115  virtual ezResult Deinitialize() { return EZ_SUCCESS; }
116 
117  ezWorld* m_pWorld;
118 };
119 
120 template <typename T, bool CompactStorage = false>
122 {
123 public:
124  typedef T ComponentType;
125 
127  ezComponentManager(ezWorld* pWorld);
128  virtual ~ezComponentManager();
129 
131  virtual ezComponentHandle CreateComponent() override;
132 
134  ezComponentHandle CreateComponent(ComponentType*& out_pComponent);
135 
137  bool TryGetComponent(const ezComponentHandle& component, ComponentType*& out_pComponent) const;
138 
141 
143  virtual const ezRTTI* GetComponentType() const override;
144 
146  static ezUInt16 TypeId();
147 
148 protected:
149  friend ComponentType;
150 
151  virtual void DeleteDeadComponent(ComponentStorageEntry storageEntry, ezComponent*& out_pMovedComponent) override;
152 
153  void RegisterUpdateFunction(UpdateFunctionDesc& desc);
154 
156 };
157 
159 template <typename ComponentType>
160 class ezComponentManagerSimple : public ezComponentManager<ComponentType>
161 {
162 public:
164 
165  virtual ezResult Initialize() override;
166 
168  void SimpleUpdate(ezUInt32 uiStartIndex, ezUInt32 uiCount);
169 };
170 
172 #define EZ_CREATE_COMPONENT_UPDATE_FUNCTION_DESC(func, instance) \
173  ezComponentManagerBase::UpdateFunctionDesc(ezComponentManagerBase::UpdateFunction(&func, instance), #func)
174 
175 #include <Core/World/Implementation/ComponentManager_inl.h>
176