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 #include <core/property/property_types.h>
17 
18 #include "ComponentTools/base_manager.h"
19 #include "ComponentTools/base_manager.inl"
20 #include "ecs/components/initial_transform_component.h"
21 
22 #define IMPLEMENT_MANAGER
23 #include "PropertyTools/property_macros.h"
24 
25 CORE_BEGIN_NAMESPACE()
26 DECLARE_PROPERTY_TYPE(CORE3D_NS::InitialTransformComponent::Data);
27 CORE_END_NAMESPACE()
28 
29 CORE3D_BEGIN_NAMESPACE()
30 using BASE_NS::array_view;
31 using BASE_NS::countof;
32 
33 using CORE_NS::BaseManager;
34 using CORE_NS::IComponentManager;
35 using CORE_NS::IEcs;
36 using CORE_NS::Property;
37 
38 class InitialTransformComponentManager final
39     : public BaseManager<InitialTransformComponent, IInitialTransformComponentManager> {
40     BEGIN_PROPERTY(InitialTransformComponent, ComponentMetadata)
41 #include "ecs/components/initial_transform_component.h"
42     END_PROPERTY();
43     const array_view<const Property> componentMetaData_ { ComponentMetadata, countof(ComponentMetadata) };
44 
45 public:
InitialTransformComponentManager(IEcs & ecs)46     explicit InitialTransformComponentManager(IEcs& ecs)
47         : BaseManager<InitialTransformComponent, IInitialTransformComponentManager>(
48               ecs, CORE_NS::GetName<InitialTransformComponent>())
49     {}
50 
51     ~InitialTransformComponentManager() = default;
52 
PropertyCount() const53     size_t PropertyCount() const override
54     {
55         return componentMetaData_.size();
56     }
57 
MetaData(size_t index) const58     const Property* MetaData(size_t index) const override
59     {
60         if (index < componentMetaData_.size()) {
61             return &componentMetaData_[index];
62         }
63         return nullptr;
64     }
65 
MetaData() const66     array_view<const Property> MetaData() const override
67     {
68         return componentMetaData_;
69     }
70 };
71 
IInitialTransformComponentManagerInstance(IEcs & ecs)72 IComponentManager* IInitialTransformComponentManagerInstance(IEcs& ecs)
73 {
74     return new InitialTransformComponentManager(ecs);
75 }
76 
IInitialTransformComponentManagerDestroy(IComponentManager * instance)77 void IInitialTransformComponentManagerDestroy(IComponentManager* instance)
78 {
79     delete static_cast<InitialTransformComponentManager*>(instance);
80 }
81 
~InitialTransformComponent()82 InitialTransformComponent::~InitialTransformComponent()
83 {
84     if (type == CORE_NS::PropertyType::FLOAT_VECTOR_T) {
85         initialData.floatVectorValue.~vector();
86     }
87 }
88 
InitialTransformComponent()89 InitialTransformComponent::InitialTransformComponent() : type(CORE_NS::PropertyType::FLOAT_T)
90 {
91     initialData.floatValue = 0.f;
92 }
93 
InitialTransformComponent(float value)94 InitialTransformComponent::InitialTransformComponent(float value) : type(CORE_NS::PropertyType::FLOAT_T)
95 {
96     initialData.floatValue = value;
97 }
98 
InitialTransformComponent(BASE_NS::Math::Vec2 value)99 InitialTransformComponent::InitialTransformComponent(BASE_NS::Math::Vec2 value) : type(CORE_NS::PropertyType::VEC2_T)
100 {
101     initialData.vec2Value = value;
102 }
103 
InitialTransformComponent(BASE_NS::Math::Vec3 value)104 InitialTransformComponent::InitialTransformComponent(BASE_NS::Math::Vec3 value) : type(CORE_NS::PropertyType::VEC3_T)
105 {
106     initialData.vec3Value = value;
107 }
108 
InitialTransformComponent(BASE_NS::Math::Vec4 value)109 InitialTransformComponent::InitialTransformComponent(BASE_NS::Math::Vec4 value) : type(CORE_NS::PropertyType::VEC4_T)
110 {
111     initialData.vec4Value = value;
112 }
113 
InitialTransformComponent(BASE_NS::Math::Quat value)114 InitialTransformComponent::InitialTransformComponent(BASE_NS::Math::Quat value) : type(CORE_NS::PropertyType::QUAT_T)
115 {
116     initialData.quatValue = value;
117 }
118 
InitialTransformComponent(BASE_NS::array_view<const float> value)119 InitialTransformComponent::InitialTransformComponent(BASE_NS::array_view<const float> value)
120     : type(CORE_NS::PropertyType::FLOAT_VECTOR_T)
121 {
122     new (&initialData.floatVectorValue) BASE_NS::vector<float>(value.cbegin(), value.cend());
123 }
124 
InitialTransformComponent(const InitialTransformComponent & other)125 InitialTransformComponent::InitialTransformComponent(const InitialTransformComponent& other) noexcept : type(other.type)
126 {
127     if (type != CORE_NS::PropertyType::FLOAT_VECTOR_T) {
128         BASE_NS::CloneData(&initialData, sizeof(initialData), &other.initialData, sizeof(initialData));
129     } else {
130         new (&initialData.floatVectorValue) BASE_NS::vector<float>(other.initialData.floatVectorValue);
131     }
132 }
133 
InitialTransformComponent(InitialTransformComponent && other)134 InitialTransformComponent::InitialTransformComponent(InitialTransformComponent&& other) noexcept : type(other.type)
135 {
136     if (type != CORE_NS::PropertyType::FLOAT_VECTOR_T) {
137         BASE_NS::CloneData(&initialData, sizeof(initialData), &other.initialData, sizeof(initialData));
138     } else {
139         new (&initialData.floatVectorValue) BASE_NS::vector<float>(BASE_NS::move(other.initialData.floatVectorValue));
140         other.type = CORE_NS::PropertyType::FLOAT_T;
141     }
142 }
143 
operator =(const InitialTransformComponent & other)144 InitialTransformComponent& InitialTransformComponent::operator=(const InitialTransformComponent& other) noexcept
145 {
146     if (&other != this) {
147         if (other.type != CORE_NS::PropertyType::FLOAT_VECTOR_T) {
148             if (type == CORE_NS::PropertyType::FLOAT_VECTOR_T) {
149                 initialData.floatVectorValue.~vector();
150             }
151             BASE_NS::CloneData(&initialData, sizeof(initialData), &other.initialData, sizeof(initialData));
152         } else if (type != CORE_NS::PropertyType::FLOAT_VECTOR_T) {
153             new (&initialData.floatVectorValue) BASE_NS::vector<float>(other.initialData.floatVectorValue);
154         } else {
155             initialData.floatVectorValue = other.initialData.floatVectorValue;
156         }
157         type = other.type;
158     }
159     return *this;
160 }
161 
operator =(InitialTransformComponent && other)162 InitialTransformComponent& InitialTransformComponent::operator=(InitialTransformComponent&& other) noexcept
163 {
164     if (&other != this) {
165         if (other.type != CORE_NS::PropertyType::FLOAT_VECTOR_T) {
166             if (type == CORE_NS::PropertyType::FLOAT_VECTOR_T) {
167                 initialData.floatVectorValue.~vector();
168             }
169             BASE_NS::CloneData(&initialData, sizeof(initialData), &other.initialData, sizeof(initialData));
170         } else if (type != CORE_NS::PropertyType::FLOAT_VECTOR_T) {
171             new (&initialData.floatVectorValue)
172                 BASE_NS::vector<float>(BASE_NS::move(other.initialData.floatVectorValue));
173         } else {
174             initialData.floatVectorValue = other.initialData.floatVectorValue;
175         }
176         type = BASE_NS::exchange(other.type, CORE_NS::PropertyType::FLOAT_T);
177     }
178     return *this;
179 }
180 
operator =(float value)181 InitialTransformComponent& InitialTransformComponent::operator=(float value) noexcept
182 {
183     if (type == CORE_NS::PropertyType::FLOAT_VECTOR_T) {
184         initialData.floatVectorValue.~vector();
185     }
186 
187     type = CORE_NS::PropertyType::FLOAT_T;
188     initialData.floatValue = value;
189     return *this;
190 }
191 
operator =(BASE_NS::Math::Vec2 value)192 InitialTransformComponent& InitialTransformComponent::operator=(BASE_NS::Math::Vec2 value) noexcept
193 {
194     if (type == CORE_NS::PropertyType::FLOAT_VECTOR_T) {
195         initialData.floatVectorValue.~vector();
196     }
197     type = CORE_NS::PropertyType::VEC2_T;
198     initialData.vec2Value = value;
199     return *this;
200 }
201 
operator =(BASE_NS::Math::Vec3 value)202 InitialTransformComponent& InitialTransformComponent::operator=(BASE_NS::Math::Vec3 value) noexcept
203 {
204     if (type == CORE_NS::PropertyType::FLOAT_VECTOR_T) {
205         initialData.floatVectorValue.~vector();
206     }
207     type = CORE_NS::PropertyType::VEC3_T;
208     initialData.vec3Value = value;
209     return *this;
210 }
211 
operator =(BASE_NS::Math::Vec4 value)212 InitialTransformComponent& InitialTransformComponent::operator=(BASE_NS::Math::Vec4 value) noexcept
213 {
214     if (type == CORE_NS::PropertyType::FLOAT_VECTOR_T) {
215         initialData.floatVectorValue.~vector();
216     }
217     type = CORE_NS::PropertyType::VEC4_T;
218     initialData.vec4Value = value;
219     return *this;
220 }
221 
operator =(BASE_NS::Math::Quat value)222 InitialTransformComponent& InitialTransformComponent::operator=(BASE_NS::Math::Quat value) noexcept
223 {
224     if (type == CORE_NS::PropertyType::FLOAT_VECTOR_T) {
225         initialData.floatVectorValue.~vector();
226     }
227     type = CORE_NS::PropertyType::QUAT_T;
228     initialData.quatValue = value;
229     return *this;
230 }
231 
operator =(BASE_NS::array_view<const float> value)232 InitialTransformComponent& InitialTransformComponent::operator=(BASE_NS::array_view<const float> value) noexcept
233 {
234     if (type != CORE_NS::PropertyType::FLOAT_VECTOR_T) {
235         type = CORE_NS::PropertyType::FLOAT_VECTOR_T;
236         new (&initialData.floatVectorValue) BASE_NS::vector<float>(value.cbegin(), value.cend());
237     } else {
238         initialData.floatVectorValue.clear();
239         initialData.floatVectorValue.insert(initialData.floatVectorValue.cbegin(), value.cbegin(), value.cend());
240     }
241     return *this;
242 }
243 CORE3D_END_NAMESPACE()
244