18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci
168bf80f4bSopenharmony_ci#ifndef CORE_ECS_ENTITY_MANAGER_H
178bf80f4bSopenharmony_ci#define CORE_ECS_ENTITY_MANAGER_H
188bf80f4bSopenharmony_ci
198bf80f4bSopenharmony_ci#include <cstddef>
208bf80f4bSopenharmony_ci#include <cstdint>
218bf80f4bSopenharmony_ci
228bf80f4bSopenharmony_ci#include <base/containers/generic_iterator.h>
238bf80f4bSopenharmony_ci#include <base/containers/vector.h>
248bf80f4bSopenharmony_ci#include <base/namespace.h>
258bf80f4bSopenharmony_ci#include <core/ecs/entity.h>
268bf80f4bSopenharmony_ci#include <core/ecs/entity_reference.h>
278bf80f4bSopenharmony_ci#include <core/ecs/intf_entity_manager.h>
288bf80f4bSopenharmony_ci#include <core/namespace.h>
298bf80f4bSopenharmony_ci
308bf80f4bSopenharmony_ciBASE_BEGIN_NAMESPACE()
318bf80f4bSopenharmony_citemplate<class T1, class T2>
328bf80f4bSopenharmony_cistruct pair;
338bf80f4bSopenharmony_ciBASE_END_NAMESPACE()
348bf80f4bSopenharmony_ci
358bf80f4bSopenharmony_ciCORE_BEGIN_NAMESPACE() class EntityManager final : public IEntityManager {
368bf80f4bSopenharmony_cipublic:
378bf80f4bSopenharmony_ci    EntityManager();
388bf80f4bSopenharmony_ci    explicit EntityManager(const size_t entityCount);
398bf80f4bSopenharmony_ci    ~EntityManager() override;
408bf80f4bSopenharmony_ci
418bf80f4bSopenharmony_ci    Entity Create() override;
428bf80f4bSopenharmony_ci    EntityReference CreateReferenceCounted() override;
438bf80f4bSopenharmony_ci    EntityReference GetReferenceCounted(Entity entity) override;
448bf80f4bSopenharmony_ci    void Destroy(const Entity entity) override;
458bf80f4bSopenharmony_ci    void DestroyAllEntities() override;
468bf80f4bSopenharmony_ci
478bf80f4bSopenharmony_ci    bool IsAlive(const Entity entity) const override;
488bf80f4bSopenharmony_ci
498bf80f4bSopenharmony_ci    uint32_t GetGenerationCounter() const override;
508bf80f4bSopenharmony_ci
518bf80f4bSopenharmony_ci    Iterator::Ptr Begin(IteratorType type) const override;
528bf80f4bSopenharmony_ci    Iterator::Ptr End(IteratorType type) const override;
538bf80f4bSopenharmony_ci
548bf80f4bSopenharmony_ci    void SetActive(const Entity entity, bool state) override;
558bf80f4bSopenharmony_ci
568bf80f4bSopenharmony_ci    BASE_NS::vector<Entity> GetRemovedEntities();
578bf80f4bSopenharmony_ci    BASE_NS::vector<BASE_NS::pair<Entity, EventType>> GetEvents();
588bf80f4bSopenharmony_ci
598bf80f4bSopenharmony_ci    // Marks all entities with zero refcnt as DEAD.
608bf80f4bSopenharmony_ci    void UpdateDeadEntities();
618bf80f4bSopenharmony_ci
628bf80f4bSopenharmony_ciprivate:
638bf80f4bSopenharmony_ci    struct EntityState {
648bf80f4bSopenharmony_ci        enum class State : uint32_t {
658bf80f4bSopenharmony_ci            /* Entity is ready for re-use.
668bf80f4bSopenharmony_ci             */
678bf80f4bSopenharmony_ci            FREE = 0,
688bf80f4bSopenharmony_ci            /* Entity is alive and active.
698bf80f4bSopenharmony_ci             */
708bf80f4bSopenharmony_ci            ALIVE = 1,
718bf80f4bSopenharmony_ci            /* Entity is alive and de-activated.
728bf80f4bSopenharmony_ci             */
738bf80f4bSopenharmony_ci            INACTIVE = 2,
748bf80f4bSopenharmony_ci            /* Entity is destroyed and waiting for GC
758bf80f4bSopenharmony_ci             */
768bf80f4bSopenharmony_ci            DEAD = 3,
778bf80f4bSopenharmony_ci        } state { 0 };
788bf80f4bSopenharmony_ci        uint32_t generation { 0 };
798bf80f4bSopenharmony_ci        IEntityReferenceCounter::Ptr counter;
808bf80f4bSopenharmony_ci    };
818bf80f4bSopenharmony_ci    BASE_NS::vector<EntityState> entities_;
828bf80f4bSopenharmony_ci    BASE_NS::vector<Entity> removedList_;
838bf80f4bSopenharmony_ci    BASE_NS::vector<uint32_t> freeList_;
848bf80f4bSopenharmony_ci    BASE_NS::vector<BASE_NS::pair<Entity, IEntityManager::EventType>> eventList_;
858bf80f4bSopenharmony_ci    uint32_t generationCounter_ { 0 };
868bf80f4bSopenharmony_ci
878bf80f4bSopenharmony_ci    class IteratorImpl final : public Iterator {
888bf80f4bSopenharmony_ci        const EntityManager* owner_ { nullptr };
898bf80f4bSopenharmony_ci        uint32_t index_ { 0 };
908bf80f4bSopenharmony_ci        IteratorType type_;
918bf80f4bSopenharmony_ci
928bf80f4bSopenharmony_ci    public:
938bf80f4bSopenharmony_ci        IteratorImpl(const EntityManager&, size_t, IteratorType type);
948bf80f4bSopenharmony_ci        const class IEntityManager* GetOwner() const override;
958bf80f4bSopenharmony_ci        bool Compare(const Iterator::Ptr&) const override;
968bf80f4bSopenharmony_ci        bool Next() override;
978bf80f4bSopenharmony_ci        Entity Get() const override;
988bf80f4bSopenharmony_ci        Iterator::Ptr Clone() const override;
998bf80f4bSopenharmony_ci    };
1008bf80f4bSopenharmony_ci    Iterator::Ptr MakeIterator(uint32_t index, IteratorType type) const;
1018bf80f4bSopenharmony_ci};
1028bf80f4bSopenharmony_ciCORE_END_NAMESPACE()
1038bf80f4bSopenharmony_ci
1048bf80f4bSopenharmony_ci#endif // CORE_ECS_ENTITY_MANAGER_H
105