ezEngine  Milestone 7
ComponentManager_inl.h
1 
2 EZ_FORCE_INLINE ezWorld* ezComponentManagerBase::GetWorld() const
3 {
4  return m_pWorld;
5 }
6 
7 EZ_FORCE_INLINE bool ezComponentManagerBase::IsValidComponent(const ezComponentHandle& component) const
8 {
9  return m_Components.Contains(component);
10 }
11 
12 EZ_FORCE_INLINE bool ezComponentManagerBase::TryGetComponent(const ezComponentHandle& component, ezComponent*& out_pComponent) const
13 {
14  ComponentStorageEntry storageEntry = { nullptr };
15  bool res = m_Components.TryGetValue(component, storageEntry);
16  out_pComponent = storageEntry.m_Ptr;
17  return res;
18 }
19 
20 EZ_FORCE_INLINE ezUInt32 ezComponentManagerBase::GetComponentCount() const
21 {
22  return m_Components.GetCount();
23 }
24 
25 //static
26 EZ_FORCE_INLINE ezComponentId ezComponentManagerBase::GetIdFromHandle(const ezComponentHandle& component)
27 {
28  return component;
29 }
30 
31 //static
32 EZ_FORCE_INLINE ezComponentHandle ezComponentManagerBase::GetHandle(ezGenericComponentId internalId, ezUInt16 uiTypeId)
33 {
34  return ezComponentHandle(ezComponentId(internalId, uiTypeId));
35 }
36 
38 
39 template <typename T, bool CompactStorage>
41  ezComponentManagerBase(pWorld),
42  m_ComponentStorage(GetBlockAllocator(), GetAllocator())
43 {
44  EZ_CHECK_AT_COMPILETIME_MSG(EZ_IS_DERIVED_FROM_STATIC(ezComponent, ComponentType),
45  "Not a valid component type");
46 }
47 
48 template <typename T, bool CompactStorage>
50 {
51  for (auto it = this->m_ComponentStorage.GetIterator(); it.IsValid(); ++it)
52  {
53  DeinitializeComponent(it);
54  }
55 }
56 
57 template <typename T, bool CompactStorage>
59 {
60  ComponentType* pNewComponent = nullptr;
61  return CreateComponent(pNewComponent);
62 }
63 
64 template <typename T, bool CompactStorage>
65 EZ_FORCE_INLINE ezComponentHandle ezComponentManager<T, CompactStorage>::CreateComponent(ComponentType*& out_pComponent)
66 {
67  auto storageEntry = m_ComponentStorage.Create();
68  out_pComponent = storageEntry.m_Ptr;
69 
70  return ezComponentManagerBase::CreateComponent(*reinterpret_cast<ComponentStorageEntry*>(&storageEntry), ComponentType::TypeId());
71 }
72 
73 template <typename T, bool CompactStorage>
74 EZ_FORCE_INLINE bool ezComponentManager<T, CompactStorage>::TryGetComponent(const ezComponentHandle& component, ComponentType*& out_pComponent) const
75 {
76  EZ_ASSERT_DEBUG(ComponentType::TypeId() == GetIdFromHandle(component).m_TypeId,
77  "The given component handle is not of the expected type. Expected type id %d, got type id %d",
78  ComponentType::TypeId(), GetIdFromHandle(component).m_TypeId);
79 
80  ezComponent* pComponent = nullptr;
81  bool bResult = ezComponentManagerBase::TryGetComponent(component, pComponent);
82  out_pComponent = static_cast<ComponentType*>(pComponent);
83  return bResult;
84 }
85 
86 template <typename T, bool CompactStorage>
88 {
89  return m_ComponentStorage.GetIterator();
90 }
91 
92 template <typename T, bool CompactStorage>
94 {
95  return ezGetStaticRTTI<T>();
96 }
97 
98 //static
99 template <typename T, bool CompactStorage>
101 {
102  return T::TypeId();
103 }
104 
105 template <typename T, bool CompactStorage>
106 EZ_FORCE_INLINE void ezComponentManager<T, CompactStorage>::DeleteDeadComponent(ComponentStorageEntry storageEntry, ezComponent*& out_pMovedComponent)
107 {
108  T* pMovedComponent = nullptr;
109  m_ComponentStorage.Delete(*reinterpret_cast<typename ezBlockStorage<ComponentType, ezInternal::DEFAULT_BLOCK_SIZE, CompactStorage>::Entry*>(&storageEntry), pMovedComponent);
110  out_pMovedComponent = pMovedComponent;
111 
112  ezComponentManagerBase::DeleteDeadComponent(storageEntry, out_pMovedComponent);
113 }
114 
115 template <typename T, bool CompactStorage>
116 EZ_FORCE_INLINE void ezComponentManager<T, CompactStorage>::RegisterUpdateFunction(UpdateFunctionDesc& desc)
117 {
118  // round up to multiple of data block capacity so tasks only have to deal with complete data blocks
119  if (desc.m_uiGranularity != 0)
120  desc.m_uiGranularity = ezMath::Ceil((ezInt32)desc.m_uiGranularity, ezDataBlock<ComponentType, ezInternal::DEFAULT_BLOCK_SIZE>::CAPACITY);
121 
123 }
124 
126 
127 template <typename ComponentType>
129  ezComponentManager<ComponentType>(pWorld)
130 {
131 }
132 
133 template <typename ComponentType>
135 {
136  auto desc = EZ_CREATE_COMPONENT_UPDATE_FUNCTION_DESC(ezComponentManagerSimple<ComponentType>::SimpleUpdate, this);
137 
138  this->RegisterUpdateFunction(desc);
139 
140  return EZ_SUCCESS;
141 }
142 
143 template <typename ComponentType>
144 void ezComponentManagerSimple<ComponentType>::SimpleUpdate(ezUInt32 uiStartIndex, ezUInt32 uiCount)
145 {
146  for (auto it = this->m_ComponentStorage.GetIterator(uiStartIndex, uiCount); it.IsValid(); ++it)
147  {
148  if (it->IsActive())
149  it->Update();
150  }
151 }
152