• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef API_CORE_ECS_ICOMPONENT_MANAGER_H
17 #define API_CORE_ECS_ICOMPONENT_MANAGER_H
18 
19 #include <base/containers/array_view.h>
20 #include <base/containers/string_view.h>
21 #include <base/containers/vector.h>
22 #include <base/util/uid.h>
23 #include <core/ecs/entity.h>
24 #include <core/namespace.h>
25 
26 CORE_BEGIN_NAMESPACE()
27 class IPropertyApi;
28 class IPropertyHandle;
29 class IEcs;
30 
31 /** \addtogroup group_ecs_icomponentmanager
32  *  @{
33  */
34 /** Component manager modified flag bits */
35 enum ComponentManagerModifiedFlagBits {
36     /** Component added bit */
37     CORE_COMPONENT_MANAGER_COMPONENT_ADDED_BIT = 0x00000001,
38     /** Component removed bit */
39     CORE_COMPONENT_MANAGER_COMPONENT_REMOVED_BIT = 0x00000002,
40     /** Component updated bit */
41     CORE_COMPONENT_MANAGER_COMPONENT_UPDATED_BIT = 0x00000004,
42     /** Modified flag bits max enumeration */
43     CORE_COMPONENT_MANAGER_MODIFIED_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF
44 };
45 /** Container for component manager modified flag bits */
46 using ComponentManagerModifiedFlags = uint32_t;
47 
48 /**
49 Component Manager.
50 
51 */
52 class IComponentManager {
53 public:
54     using ComponentId = uint32_t;
55     static constexpr ComponentId INVALID_COMPONENT_ID = 0xFFFFFFFF;
56 
57     /** Returns the name of the component type.
58      */
59     virtual BASE_NS::string_view GetName() const = 0;
60 
61     /** Returns the UID of the component type.
62      */
63     virtual BASE_NS::Uid GetUid() const = 0;
64 
65     /** Returns the total number of components. Includes components that are being destroyed but are not yet garbage
66      *  collected.
67      */
68     virtual size_t GetComponentCount() const = 0;
69 
70     /** Access to metadata defining the contents of the component.
71      */
72     virtual const IPropertyApi& GetPropertyApi() const = 0;
73 
74     /** Returns entity for component index. May return invalid entity if the entity is being destroyed.
75      */
76     virtual Entity GetEntity(IComponentManager::ComponentId index) const = 0;
77 
78     /** Get component generation id, which is incremented every time the component data is changed.
79      */
80     virtual uint32_t GetComponentGeneration(IComponentManager::ComponentId index) const = 0;
81 
82     // Entity api.
83     /** Checks if Entity has component.
84      *  @param entity Entity to be checked.
85      */
86     virtual bool HasComponent(Entity entity) const = 0;
87 
88     /** Retrieves component id for a given entity.
89      *  @param entity Entity to be used as a search key.
90      */
91     virtual IComponentManager::ComponentId GetComponentId(Entity entity) const = 0;
92 
93     /** Creates a default component for entity.
94      *  @param entity Entity which for component is created.
95      */
96     virtual void Create(Entity entity) = 0;
97 
98     /** Removes component from entity.
99      *  @param entity Entity where component is removed.
100      */
101     virtual bool Destroy(Entity entity) = 0;
102 
103     /** Garbage collect components.
104      *  This is called automatically by the ECS, but can be called by the user too to force cleanup.
105      */
106     virtual void Gc() = 0;
107 
108     /** Remove components from entities.
109      *  @param gcList List of entities to have their components removed
110      */
111     virtual void Destroy(BASE_NS::array_view<const Entity> gcList) = 0;
112 
113     /** Get list of entities that have new components of this type (since last call).
114      */
115     virtual BASE_NS::vector<Entity> GetAddedComponents() = 0;
116 
117     /** Get list of entities that no longer have components of this type (since last call).
118      */
119     virtual BASE_NS::vector<Entity> GetRemovedComponents() = 0;
120 
121     /** Get list of entities that have been modified (since last call).
122      */
123     virtual BASE_NS::vector<Entity> GetUpdatedComponents() = 0;
124 
125     /** Returns flags if component is added, removed or updated.
126      */
127     virtual ComponentManagerModifiedFlags GetModifiedFlags() const = 0;
128 
129     /** Clears all flags of component (Application developer should not call this since its been automatically called
130      * after every frame).
131      */
132     virtual void ClearModifiedFlags() = 0;
133 
134     /** Number of changes occured in this component manager since start of its life.
135      */
136     virtual uint32_t GetGenerationCounter() const = 0;
137 
138     /** Set data for entity. Copies the data from "data" handle to the component for entity.
139      *  @param entity Entity which is set-up.
140      *  @param data property handle for entity.
141      */
142     virtual void SetData(Entity entity, const IPropertyHandle& data) = 0;
143 
144     /** Get data for entity. This handle can be used to directly read the component for entity.
145      *  @param entity Entity where we get our property handle.
146      */
147     virtual const IPropertyHandle* GetData(Entity entity) const = 0;
148 
149     /** Get data for entity. This handle can be used to directly modify the component for entity.
150      *  @param entity Entity where we get our property handle.
151      */
152     virtual IPropertyHandle* GetData(Entity entity) = 0;
153 
154     /** Set data for entity. Copies the data from "data" handle to the component.
155      *  @param index Index what is used to add or update the component if index is same as before.
156      *  @param data Data which is set to component.
157      */
158     virtual void SetData(ComponentId index, const IPropertyHandle& data) = 0;
159 
160     /** Get data for entity. This handle can be used to directly read the component for entity.
161      *  @param index Index to get data from
162      */
163     virtual const IPropertyHandle* GetData(ComponentId index) const = 0;
164 
165     /** Get data for entity. This handle can be used to directly modify the component for entity.
166      *  @param index Index to get data from
167      */
168     virtual IPropertyHandle* GetData(ComponentId index) = 0;
169 
170     /** Get the ECS instance using this manager.
171      * @return Reference to owning ECS instance.
172      */
173     virtual IEcs& GetEcs() const = 0;
174 
175 protected:
176     IComponentManager() = default;
177     IComponentManager(const IComponentManager&) = delete;
178     IComponentManager(IComponentManager&&) = delete;
179     IComponentManager& operator=(const IComponentManager&) = delete;
180     virtual ~IComponentManager() = default;
181 };
182 
183 /** Get name */
184 template<class T>
GetName()185 inline constexpr BASE_NS::string_view GetName()
186 {
187     return GetName((const T*)nullptr);
188 }
189 
190 /** Get UID */
191 template<class T>
GetUid()192 inline constexpr BASE_NS::Uid GetUid()
193 {
194     return GetUid((const T*)nullptr);
195 }
196 
197 /** Create component */
198 template<class T>
CreateComponent(T & componentManager,const Entity & entity)199 inline auto CreateComponent(T& componentManager, const Entity& entity)
200 {
201     componentManager.Create(entity);
202     return componentManager.Get(entity);
203 }
204 
205 /** @} */
206 CORE_END_NAMESPACE()
207 
208 #endif // API_CORE_ECS_ICOMPONENT_MANAGER_H
209