• 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 
16 #ifndef SCENE_EXT_COMPONENT_H
17 #define SCENE_EXT_COMPONENT_H
18 
19 #include <scene/base/namespace.h>
20 #include <scene/ext/ecs_lazy_property.h>
21 #include <scene/ext/intf_component.h>
22 #include <scene/ext/scene_property.h>
23 
SCENE_BEGIN_NAMESPACE()24 SCENE_BEGIN_NAMESPACE()
25 
26 class Component : public META_NS::IntroduceInterfaces<EcsLazyProperty, IComponent> {
27 public:
28     bool PopulateAllProperties() override
29     {
30         if (populated_) {
31             return true;
32         }
33         populated_ =
34             this->object_->AddAllProperties(this->template GetSelf<META_NS::IMetadata>(), this->GetName()).GetResult();
35         return populated_;
36     }
37 
38 private:
39     bool populated_ {};
40 };
41 
GetComponentProperty(const META_NS::IAttach * obj,BASE_NS::string_view component,BASE_NS::string_view name)42 inline META_NS::IProperty::ConstPtr GetComponentProperty(
43     const META_NS::IAttach* obj, BASE_NS::string_view component, BASE_NS::string_view name)
44 {
45     if (auto c = obj->GetAttachmentContainer(true)->FindByName(component)) {
46         if (auto m = interface_cast<META_NS::IMetadata>(c)) {
47             return m->GetProperty(name);
48         }
49     }
50     return nullptr;
51 }
52 
GetComponentProperty(META_NS::IAttach * obj,BASE_NS::string_view component,BASE_NS::string_view name)53 inline META_NS::IProperty::Ptr GetComponentProperty(
54     META_NS::IAttach* obj, BASE_NS::string_view component, BASE_NS::string_view name)
55 {
56     if (auto c = obj->GetAttachmentContainer(true)->FindByName(component)) {
57         if (auto m = interface_cast<META_NS::IMetadata>(c)) {
58             return m->GetProperty(name);
59         }
60     }
61     return nullptr;
62 }
63 
64 #define SCENE_USE_READONLY_COMPONENT_PROPERTY(type, name, component)        \
65     ::META_NS::IProperty::ConstPtr Property##name() const noexcept override \
66     {                                                                       \
67         return GetComponentProperty(this, component, #name);                \
68     }
69 
70 #define SCENE_USE_COMPONENT_PROPERTY(type, name, component)      \
71     SCENE_USE_READONLY_COMPONENT_PROPERTY(type, name, component) \
72     ::META_NS::IProperty::Ptr Property##name() noexcept override \
73     {                                                            \
74         return GetComponentProperty(this, component, #name);     \
75     }
76 
77 // the type is not used, so just forward for now
78 #define SCENE_USE_ARRAY_COMPONENT_PROPERTY(type, name, component) SCENE_USE_COMPONENT_PROPERTY(type, name, component)
79 
80 #define SCENE_USE_READONLY_ARRAY_COMPONENT_PROPERTY(type, name, component) \
81     SCENE_USE_READONLY_COMPONENT_PROPERTY(type, name, component)
82 
83 SCENE_END_NAMESPACE()
84 
85 #endif
86