• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CORE__ECS_HELPER__COMPONENT_TOOLS__BASE_MANAGER_H
16 #define CORE__ECS_HELPER__COMPONENT_TOOLS__BASE_MANAGER_H
17 
18 #ifndef NDEBUG
19 #include <atomic>
20 #endif
21 #include <cstddef>
22 #include <cstdint>
23 
24 #include <base/containers/array_view.h>
25 #include <base/containers/string_view.h>
26 #include <base/containers/unordered_map.h>
27 #include <base/containers/vector.h>
28 #include <base/namespace.h>
29 #include <base/util/uid.h>
30 #include <core/ecs/entity.h>
31 #include <core/ecs/intf_component_manager.h>
32 #include <core/namespace.h>
33 #include <core/property/intf_property_api.h>
34 #include <core/property/intf_property_handle.h>
35 #include <core/property/scoped_handle.h>
36 
37 CORE_BEGIN_NAMESPACE()
38 class IEcs;
39 struct Property;
40 
41 // BaseClass should inherit IComponentManager AND should follow the basic form of component managers
42 // (this will be enforced later)
43 template<typename ComponentType, typename BaseClass>
44 class BaseManager : public BaseClass, public IPropertyApi {
45     using ComponentId = IComponentManager::ComponentId;
46 
47 public:
48     // IPropertyApi
49     size_t PropertyCount() const override = 0;
50     const Property* MetaData(size_t index) const override = 0;
51     BASE_NS::array_view<const Property> MetaData() const override = 0;
52     IPropertyHandle* Create() const override;
53     IPropertyHandle* Clone(const IPropertyHandle*) const override;
54     void Release(IPropertyHandle*) const override;
55     uint64_t Type() const override;
56 
57     // IComponentManager
58     BASE_NS::string_view GetName() const override;
59     BASE_NS::Uid GetUid() const override;
60     size_t GetComponentCount() const override;
61     const IPropertyApi& GetPropertyApi() const override;
62     Entity GetEntity(ComponentId index) const override;
63     uint32_t GetComponentGeneration(ComponentId index) const override;
64     bool HasComponent(Entity entity) const override;
65     IComponentManager::ComponentId GetComponentId(Entity entity) const override;
66     void Create(Entity entity) override;
67     bool Destroy(Entity entity) override;
68     void Gc() override;
69     void Destroy(BASE_NS::array_view<const Entity> gcList) override;
70     BASE_NS::vector<Entity> GetAddedComponents() override;
71     BASE_NS::vector<Entity> GetRemovedComponents() override;
72     BASE_NS::vector<Entity> GetUpdatedComponents() override;
73     BASE_NS::vector<Entity> GetMovedComponents() override;
74     CORE_NS::ComponentManagerModifiedFlags GetModifiedFlags() const override;
75     void ClearModifiedFlags() override;
76     uint32_t GetGenerationCounter() const override;
77     void SetData(Entity entity, const IPropertyHandle& dataHandle) override;
78     const IPropertyHandle* GetData(Entity entity) const override;
79     IPropertyHandle* GetData(Entity entity) override;
80     void SetData(ComponentId index, const IPropertyHandle& dataHandle) override;
81     const IPropertyHandle* GetData(ComponentId index) const override;
82     IPropertyHandle* GetData(ComponentId index) override;
83     IEcs& GetEcs() const override;
84 
85     // "base class"
86     ComponentType Get(ComponentId index) const override;
87     ComponentType Get(Entity entity) const override;
88     void Set(ComponentId index, const ComponentType& aData) override;
89     void Set(Entity entity, const ComponentType& aData) override;
90     ScopedHandle<const ComponentType> Read(ComponentId index) const override;
91     ScopedHandle<const ComponentType> Read(Entity entity) const override;
92     ScopedHandle<ComponentType> Write(ComponentId index) override;
93     ScopedHandle<ComponentType> Write(Entity entity) override;
94 
95     // internal, non-public
96     void Updated(Entity entity);
97 
98     BaseManager(const BaseManager&) = delete;
99     BaseManager(BaseManager&&) = delete;
100     BaseManager& operator=(const BaseManager&) = delete;
101     BaseManager& operator=(BaseManager&&) = delete;
102 
103 protected:
104     BaseManager(IEcs& ecs, BASE_NS::string_view) noexcept;
105     BaseManager(IEcs& ecs, BASE_NS::string_view, size_t preallocate) noexcept;
106     virtual ~BaseManager();
107     IEcs& ecs_;
108     BASE_NS::string_view name_;
109 
110     bool IsMatchingHandle(const IPropertyHandle& handle);
111 
112     class BaseComponentHandle : public IPropertyHandle {
113     public:
114         BaseComponentHandle() = delete;
115         BaseComponentHandle(BaseManager* owner, Entity entity) noexcept;
116         BaseComponentHandle(BaseManager* owner, Entity entity, const ComponentType& data) noexcept;
117         ~BaseComponentHandle() override = default;
118         BaseComponentHandle(const BaseComponentHandle& other) = delete;
119         BaseComponentHandle(BaseComponentHandle&& other) noexcept;
120         BaseComponentHandle& operator=(const BaseComponentHandle& other) = delete;
121         BaseComponentHandle& operator=(BaseComponentHandle&& other) noexcept;
122         const IPropertyApi* Owner() const override;
123         size_t Size() const override;
124         const void* RLock() const override;
125         void RUnlock() const override;
126         void* WLock() override;
127         void WUnlock() override;
128 #ifndef NDEBUG
129         mutable std::atomic_uint32_t rLocked_ { 0 };
130         mutable bool wLocked_ { false };
131 #endif
132         bool dirty_ { false };
133         BaseManager* manager_ { nullptr };
134         uint32_t generation_ { 0 };
135         Entity entity_;
136         ComponentType data_;
137     };
138     uint32_t generationCounter_ { 0 };
139     uint32_t modifiedFlags_ { 0 };
140     BASE_NS::unordered_map<Entity, ComponentId> entityComponent_;
141     BASE_NS::vector<BaseComponentHandle> components_;
142     BASE_NS::vector<Entity> added_;
143     BASE_NS::vector<Entity> removed_;
144     BASE_NS::vector<Entity> updated_;
145     BASE_NS::vector<Entity> moved_;
146     uint64_t typeHash_;
147 };
148 CORE_END_NAMESPACE()
149 #endif
150