• 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 #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 /**
24  * @brief Populate engine values recursively from IEngineValue
25  * @param m Engine value manager to populate
26  * @param value Start populating from this engine value
27  * @param options Options for engine values and optional output vector for populated values
28  */
29 inline void AddEngineValuesRecursively(
30     const IEngineValueManager::Ptr& m, const IEngineValue::Ptr& value, EngineValueOptions options = {})
31 {
32     BASE_NS::vector<IEngineValue::Ptr> vec;
33     auto evo = options;
34     evo.values = &vec;
35     if (m->ConstructValues(value, evo)) {
36         if (options.values) {
37             options.values->insert(options.values->end(), vec.begin(), vec.end());
38         }
39         if (evo.recurseKnownStructs) {
40             for (auto&& v : vec) {
41                 AddEngineValuesRecursively(m, v, options);
42             }
43         }
44     }
45 }
46 
47 /**
48  * @brief Populate engine values recursively from EnginePropertyHandle
49  * @param m Engine value manager to populate
50  * @param handle Start populating from this engine property handle
51  * @param options Options for engine values and optional output vector for populated values
52  */
53 inline void AddEngineValuesRecursively(
54     const IEngineValueManager::Ptr& m, EnginePropertyHandle handle, EngineValueOptions options = {})
55 {
56     BASE_NS::vector<IEngineValue::Ptr> vec;
57     auto evo = options;
58     evo.values = &vec;
59     if (m->ConstructValues(handle, evo)) {
60         if (options.values) {
61             options.values->insert(options.values->end(), vec.begin(), vec.end());
62         }
63         if (evo.recurseKnownStructs) {
64             options.namePrefix = "";
65             for (auto&& v : vec) {
66                 AddEngineValuesRecursively(m, v, options);
67             }
68         }
69     }
70 }
71 
72 /**
73  * @brief Add engine value to stack property
74  * @param p Property where the value is added
75  * @param value The engine value to be added
76  * @return True on success
77  */
78 inline bool SetEngineValueToProperty(const IProperty::Ptr& p, const IEngineValue::Ptr& value, bool setAsDefault = true)
79 {
80     if (p && value && value->IsCompatible(p->GetTypeId())) {
81         if (auto i = interface_cast<IStackProperty>(p)) {
82             PropertyLock lock { p };
83             TypeId ids[] = { IEngineValue::UID };
84             for (auto&& v : i->GetValues(ids, false)) {
85                 i->RemoveValue(v);
86             }
87             i->PushValue(value);
88             if (setAsDefault) {
89                 i->SetDefaultValue(value->GetValue());
90             }
91             return true;
92         }
93     } else {
94         CORE_LOG_W("Trying to attach incompatible engine value to property (%s)", p->GetName().c_str());
95     }
96     return false;
97 }
98 
99 /// Get Engine value from property if it contains one, otherwise nullptr
GetEngineValueFromProperty(const IProperty::ConstPtr & p)100 inline IEngineValue::Ptr GetEngineValueFromProperty(const IProperty::ConstPtr& p)
101 {
102     return GetFirstValueFromProperty<IEngineValue>(p);
103 }
104 
105 /**
106  * @brief Create property from engine value with matching type
107  * @param name Name of the property
108  * @param value Engine value where the type is derived and what is added to the property
109  * @return Property on success, otherwise nullptr
110  */
PropertyFromEngineValue(BASE_NS::string_view name,const IEngineValue::Ptr & value)111 inline IProperty::Ptr PropertyFromEngineValue(BASE_NS::string_view name, const IEngineValue::Ptr& value)
112 {
113     IProperty::Ptr ret;
114     if (auto internal = interface_cast<IEngineValueInternal>(value)) {
115         ret = GetObjectRegistry().GetPropertyRegister().Create(META_NS::ClassId::StackProperty, name);
116         if (auto i = interface_cast<IPropertyInternalAny>(ret)) {
117             i->SetInternalAny(internal->CreateAny());
118         }
119         if (auto i = interface_cast<IStackProperty>(ret)) {
120             i->PushValue(value);
121             i->SetDefaultValue(value->GetValue());
122         }
123     }
124     return ret;
125 }
126 
127 META_END_NAMESPACE()
128 
129 #endif
130