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 #ifndef META_API_ENGINE_UTIL_H
16 #define META_API_ENGINE_UTIL_H
17
18 #include <meta/api/util.h>
19 #include <meta/interface/engine/intf_engine_value_manager.h>
20
META_BEGIN_NAMESPACE()21 META_BEGIN_NAMESPACE()
22
23 inline void AddEngineValuesRecursively(
24 const IEngineValueManager::Ptr& m, const IEngineValue::Ptr& value, EngineValueOptions options = {})
25 {
26 BASE_NS::vector<IEngineValue::Ptr> vec;
27 if (m->ConstructValues(
28 value, EngineValueOptions { options.namePrefix, &vec, options.pushValuesDirectlyToEngine })) {
29 if (options.values) {
30 options.values->insert(options.values->end(), vec.begin(), vec.end());
31 }
32 for (auto&& v : vec) {
33 AddEngineValuesRecursively(m, v, options);
34 }
35 }
36 }
37
38 inline void AddEngineValuesRecursively(
39 const IEngineValueManager::Ptr& m, EnginePropertyHandle handle, EngineValueOptions options = {})
40 {
41 BASE_NS::vector<IEngineValue::Ptr> vec;
42 if (m->ConstructValues(
43 handle, EngineValueOptions { options.namePrefix, &vec, options.pushValuesDirectlyToEngine })) {
44 if (options.values) {
45 options.values->insert(options.values->end(), vec.begin(), vec.end());
46 }
47 for (auto&& v : vec) {
48 AddEngineValuesRecursively(
49 m, v, EngineValueOptions { "", options.values, options.pushValuesDirectlyToEngine });
50 }
51 }
52 }
53
SetEngineValueToProperty(const IProperty::Ptr & p,const IEngineValue::Ptr & value)54 inline bool SetEngineValueToProperty(const IProperty::Ptr& p, const IEngineValue::Ptr& value)
55 {
56 if (p && value && value->IsCompatible(p->GetTypeId())) {
57 if (auto i = interface_cast<IStackProperty>(p)) {
58 PropertyLock lock { p };
59 TypeId ids[] = { IEngineValue::UID };
60 for (auto&& v : i->GetValues(ids, false)) {
61 i->RemoveValue(v);
62 }
63 i->PushValue(value);
64 return true;
65 }
66 } else {
67 CORE_LOG_W("Trying to attach incompatible engine value to property (%s)", p->GetName().c_str());
68 }
69 return false;
70 }
71
GetEngineValueFromProperty(const IProperty::ConstPtr & p)72 inline IEngineValue::Ptr GetEngineValueFromProperty(const IProperty::ConstPtr& p)
73 {
74 return GetFirstValueFromProperty<IEngineValue>(p);
75 }
76
PropertyFromEngineValue(BASE_NS::string_view name,const IEngineValue::Ptr & value)77 inline IProperty::Ptr PropertyFromEngineValue(BASE_NS::string_view name, const IEngineValue::Ptr& value)
78 {
79 IProperty::Ptr ret;
80 if (auto internal = interface_cast<IEngineValueInternal>(value)) {
81 ret = GetObjectRegistry().GetPropertyRegister().Create(META_NS::ClassId::StackProperty, name);
82 if (auto i = interface_cast<IPropertyInternalAny>(ret)) {
83 i->SetInternalAny(internal->CreateAny());
84 }
85 if (auto i = interface_cast<IStackProperty>(ret)) {
86 i->PushValue(value);
87 }
88 }
89 return ret;
90 }
91
92 META_END_NAMESPACE()
93
94 #endif
95