1 /*
2  * Copyright (c) 2024 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_IENTITY_MANAGER_H
17 #define API_CORE_ECS_IENTITY_MANAGER_H
18 
19 #include <cstdint>
20 
21 #include <base/containers/generic_iterator.h>
22 #include <base/containers/vector.h>
23 #include <core/ecs/entity.h>
24 #include <core/ecs/entity_reference.h>
25 #include <core/namespace.h>
26 
27 BASE_BEGIN_NAMESPACE()
28 template<class T1, class T2>
29 struct pair;
30 BASE_END_NAMESPACE()
31 
CORE_BEGIN_NAMESPACE()32 CORE_BEGIN_NAMESPACE()
33 /** @ingroup group_ecs_ientitymanager */
34 /** IEntity manager */
35 class IEntityManager {
36 public:
37     enum class EventType : uint8_t {
38         /** Entity Created
39          */
40         CREATED,
41         /** Entity was activated
42          */
43         ACTIVATED,
44         /** Entity was de-activated
45          */
46         DEACTIVATED,
47         /** Entity destroyed
48          */
49         DESTROYED
50     };
51     using IteratorValue = Entity;
52     using Iterator = BASE_NS::IGenericIterator<const IEntityManager>;
53 
54     IEntityManager(const IEntityManager&) = delete;
55     IEntityManager& operator=(const IEntityManager&) = delete;
56 
57     /** Create a new entity.
58      * @return New entity which is not reference counted. It will exist until IEntityManager::Destroy is called.
59      */
60     virtual Entity Create() = 0;
61 
62     /** Create a new reference counted entity.
63      * @return New reference counted entity. It will exist until there are no more references to it.
64      */
65     virtual EntityReference CreateReferenceCounted() = 0;
66 
67     /** Get a reference counter for the given entity. If the entity was originally create as not reference counted it
68      * will now have a reference count and be destroyed once all references are gone.
69      * @param entity Entity to reference count.
70      * @return Reference counted entity.
71      */
72     virtual EntityReference GetReferenceCounted(Entity entity) = 0;
73 
74     /** Destroy entity.
75      *  @param entity Entity what we want to destroy.
76      */
77     virtual void Destroy(const Entity entity) = 0;
78 
79     /** Destroy all entities
80      */
81     virtual void DestroyAllEntities() = 0;
82 
83     /** Checks if entity is alive.
84      *  @param entity Entity which alive status we check.
85      */
86     virtual bool IsAlive(const Entity entity) const = 0;
87 
88     /** Sets entity active state, inactive entities count as not alive. Invalid entities ignored.
89      *  @param entity Entity which active status we change
90      *  @param state New active status
91      */
92     virtual void SetActive(const Entity entity, bool state) = 0;
93 
94     /** Returns generation ID of current entity manager state, this id changes whenever entities are created or
95      * destroyed.
96      */
97     virtual uint32_t GetGenerationCounter() const = 0;
98 
99     enum class IteratorType : uint8_t {
100         ALIVE,       /* iterate entities that are alive and active */
101         DEACTIVATED, /* iterate entities that are deactivated */
102     };
103     virtual Iterator::Ptr Begin(IteratorType type = IteratorType::ALIVE) const = 0;
104     virtual Iterator::Ptr End(IteratorType type = IteratorType::ALIVE) const = 0;
105 
106 protected:
107     IEntityManager() = default;
108     virtual ~IEntityManager() = default;
109 };
110 
111 using EntityIterator = BASE_NS::IIterator<CORE_NS::IEntityManager::Iterator>;
112 
begin(const CORE_NS::IEntityManager & manager)113 inline CORE_NS::EntityIterator begin(const CORE_NS::IEntityManager& manager)
114 {
115     return CORE_NS::EntityIterator(manager.Begin(IEntityManager::IteratorType::ALIVE));
116 }
117 
end(const CORE_NS::IEntityManager & manager)118 inline CORE_NS::EntityIterator end(const CORE_NS::IEntityManager& manager)
119 {
120     return CORE_NS::EntityIterator(manager.End(IEntityManager::IteratorType::ALIVE));
121 }
122 CORE_END_NAMESPACE()
123 
124 #endif // API_CORE_ECS_IENTITY_MANAGER_H
125