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/interface/engine/intf_engine_value_manager.h>
19 
META_BEGIN_NAMESPACE()20 META_BEGIN_NAMESPACE()
21 
22 inline void AddEngineValuesRecursively(
23     const IEngineValueManager::Ptr& m, const IEngineValue::Ptr& value, EngineValueOptions options = {})
24 {
25     BASE_NS::vector<IEngineValue::Ptr> vec;
26     if (m->ConstructValues(
27             value, EngineValueOptions { options.namePrefix, &vec, options.pushValuesDirectlyToEngine })) {
28         if (options.values) {
29             options.values->insert(options.values->end(), vec.begin(), vec.end());
30         }
31         for (auto&& v : vec) {
32             AddEngineValuesRecursively(m, v, options);
33         }
34     }
35 }
36 
37 inline void AddEngineValuesRecursively(
38     const IEngineValueManager::Ptr& m, CORE_NS::IPropertyHandle* handle, EngineValueOptions options = {})
39 {
40     BASE_NS::vector<IEngineValue::Ptr> vec;
41     if (m->ConstructValues(
42         handle, EngineValueOptions { options.namePrefix, &vec, options.pushValuesDirectlyToEngine })) {
43         if (options.values) {
44             options.values->insert(options.values->end(), vec.begin(), vec.end());
45         }
46         for (auto&& v : vec) {
47             AddEngineValuesRecursively(
48                 m, v, EngineValueOptions { "", options.values, options.pushValuesDirectlyToEngine });
49         }
50     }
51 }
52 
SetEngineValueToProperty(const IProperty::Ptr & p,const IEngineValue::Ptr & value)53 inline bool SetEngineValueToProperty(const IProperty::Ptr& p, const IEngineValue::Ptr& value)
54 {
55     if (p && value && value->IsCompatible(p->GetTypeId())) {
56         if (auto i = interface_cast<IStackProperty>(p)) {
57             PropertyLock lock { p };
58             TypeId ids[] = { IEngineValue::UID };
59             for (auto&& v : i->GetValues(ids, false)) {
60                 i->RemoveValue(v);
61             }
62             i->PushValue(value);
63             return true;
64         }
65     }
66     return false;
67 }
68 
GetEngineValueFromProperty(const IProperty::Ptr & p)69 inline IEngineValue::Ptr GetEngineValueFromProperty(const IProperty::Ptr& p)
70 {
71     if (auto i = interface_cast<IStackProperty>(p)) {
72         PropertyLock lock { p };
73         TypeId ids[] = { IEngineValue::UID };
74         auto values = i->GetValues(ids, false);
75         if (!values.empty()) {
76             return interface_pointer_cast<IEngineValue>(values.back());
77         }
78     }
79     return nullptr;
80 }
81 
82 META_END_NAMESPACE()
83 
84 #endif
85