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