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_SCENE_PROPERTY_H
17 #define SCENE_EXT_SCENE_PROPERTY_H
18
19 #include <scene/base/namespace.h>
20 #include <scene/ext/intf_engine_property_init.h>
21
22 #include <meta/ext/implementation_macros.h>
23
SCENE_BEGIN_NAMESPACE()24 SCENE_BEGIN_NAMESPACE()
25
26 template<typename Type, bool IsDynamic>
27 META_NS::Internal::MetadataCtor* CreateScenePropertyCtor()
28 {
29 return [](const BASE_NS::shared_ptr<META_NS::IOwner>& owner, const META_NS::StaticMetadata& d) {
30 auto engOwner = interface_cast<IEnginePropertyInit>(owner);
31 if (!engOwner) {
32 CORE_ASSERT("Invalid use of scene property without IEnginePropertyInit");
33 return META_NS::SharedPtrIInterface {};
34 }
35 if (auto prop = META_NS::CreatePropertyImpl<Type>(
36 d.name, d.runtimeValue, META_NS::ObjectFlagBitsValue(META_NS::ObjectFlagBits::NATIVE))) {
37 const char* component = static_cast<const char*>(d.data);
38 if constexpr (IsDynamic) {
39 if (engOwner->InitDynamicProperty(prop, component)) {
40 return interface_pointer_cast<CORE_NS::IInterface>(prop);
41 }
42 } else {
43 if (engOwner->AttachEngineProperty(prop, component)) {
44 return interface_pointer_cast<CORE_NS::IInterface>(prop);
45 }
46 }
47 CORE_LOG_E("Failed to attach to engine property %s [%s]", d.name, component);
48 }
49 return META_NS::SharedPtrIInterface {};
50 };
51 }
52
53 //--- PROPERTY DATA
54 #define SCENE_IMPL_PROPERTY_DATA(intf, type, name, ppath, dynamic) \
55 { META_NS::MetadataType::PROPERTY, intf::INTERFACE_INFO, #name, \
56 SCENE_NS::CreateScenePropertyCtor<type, dynamic>(), [] { return META_NS::ConstructAnyHelper<type>({}); }, \
57 ppath },
58
59 #define SCENE_STATIC_PROPERTY_DATA(intf, type, name, ppath) SCENE_IMPL_PROPERTY_DATA(intf, type, name, ppath, false)
60 #define SCENE_STATIC_DYNINIT_PROPERTY_DATA(intf, type, name, ppath) \
61 SCENE_IMPL_PROPERTY_DATA(intf, type, name, ppath, true)
62 //---
63
64 //--- ARRAY PROPERTY DATA
65 #define SCENE_IMPL_ARRAY_PROPERTY_DATA(intf, type, name, ppath, dynamic) \
66 { META_NS::MetadataType::PROPERTY, intf::INTERFACE_INFO, #name, \
67 SCENE_NS::CreateScenePropertyCtor<type[], dynamic>(), \
68 [] { return META_NS::IAny::Ptr(META_NS::ConstructArrayAnyHelper<type>({})); }, ppath },
69
70 #define SCENE_STATIC_ARRAY_PROPERTY_DATA(intf, type, name, ppath) \
71 SCENE_IMPL_ARRAY_PROPERTY_DATA(intf, type, name, ppath, false)
72 #define SCENE_STATIC_DYNINIT_ARRAY_PROPERTY_DATA(intf, type, name, ppath) \
73 SCENE_IMPL_ARRAY_PROPERTY_DATA(intf, type, name, ppath, true)
74 //---
75
76 SCENE_END_NAMESPACE()
77
78 #endif