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