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 #include "engine_input_property_manager.h"
17
18 META_BEGIN_NAMESPACE()
19
20 namespace Internal {
21
Build(const IMetadata::Ptr & data)22 bool EngineInputPropertyManager::Build(const IMetadata::Ptr& data)
23 {
24 manager_ = GetObjectRegistry().Create<IEngineValueManager>(META_NS::ClassId::EngineValueManager);
25 return manager_ != nullptr;
26 }
Sync()27 bool EngineInputPropertyManager::Sync()
28 {
29 std::unique_lock lock { mutex_ };
30 for (auto&& p : props_) {
31 // evaluate property and set the value to engine value for synchronisation
32 p.second.value->SetValue(p.second.property->GetValue());
33 }
34 return manager_->Sync(EngineSyncDirection::TO_ENGINE);
35 }
ConstructFromValue(const IEngineValue::Ptr & value)36 static IProperty::Ptr ConstructFromValue(const IEngineValue::Ptr& value)
37 {
38 IProperty::Ptr property;
39 if (auto internal = interface_cast<IEngineValueInternal>(value)) {
40 property = GetObjectRegistry().GetPropertyRegister().Create(META_NS::ClassId::StackProperty, value->GetName());
41 if (auto i = interface_cast<IPropertyInternalAny>(property)) {
42 i->SetInternalAny(internal->CreateAny());
43 }
44 if (auto i = interface_cast<IStackProperty>(property)) {
45 i->SetDefaultValue(value->GetValue());
46 }
47 }
48 return property;
49 }
ConstructProperty(BASE_NS::string_view name)50 IProperty::Ptr EngineInputPropertyManager::ConstructProperty(BASE_NS::string_view name)
51 {
52 std::unique_lock lock { mutex_ };
53 auto it = props_.find(name);
54 if (it != props_.end()) {
55 return it->second.property;
56 }
57 auto value = manager_->GetEngineValue(name);
58 IProperty::Ptr property = ConstructFromValue(value);
59 if (property) {
60 props_[name] = PropInfo { property, value };
61 }
62 return property;
63 }
TieProperty(const IProperty::Ptr & p,BASE_NS::string valueName)64 IProperty::Ptr EngineInputPropertyManager::TieProperty(const IProperty::Ptr& p, BASE_NS::string valueName)
65 {
66 if (valueName.empty()) {
67 valueName = p->GetName();
68 }
69 std::unique_lock lock { mutex_ };
70 auto it = props_.find(p->GetName());
71 IEngineValue::Ptr value;
72 if (it != props_.end()) {
73 value = it->second.value;
74 }
75 if (!value) {
76 value = manager_->GetEngineValue(valueName);
77 }
78 if (value && value->IsCompatible(p->GetTypeId())) {
79 props_[p->GetName()] = PropInfo { p, value };
80 return p;
81 }
82 return nullptr;
83 }
GetAllProperties() const84 BASE_NS::vector<IProperty::Ptr> EngineInputPropertyManager::GetAllProperties() const
85 {
86 BASE_NS::vector<IProperty::Ptr> ret;
87 std::shared_lock lock { mutex_ };
88 for (auto&& p : props_) {
89 ret.push_back(p.second.property);
90 }
91 return ret;
92 }
PopulateProperties(IMetadata & data)93 bool EngineInputPropertyManager::PopulateProperties(IMetadata& data)
94 {
95 std::unique_lock lock { mutex_ };
96 props_.clear();
97 for (auto&& v : manager_->GetAllEngineValues()) {
98 if (auto p = data.GetProperty(v->GetName())) {
99 if (v->IsCompatible(p->GetTypeId())) {
100 props_[v->GetName()] = PropInfo { p, v };
101 }
102 }
103 if (auto p = ConstructFromValue(v)) {
104 props_[v->GetName()] = PropInfo { p, v };
105 data.AddProperty(p);
106 }
107 }
108 return true;
109 }
RemoveProperty(BASE_NS::string_view name)110 void EngineInputPropertyManager::RemoveProperty(BASE_NS::string_view name)
111 {
112 std::unique_lock lock { mutex_ };
113 props_.erase(name);
114 }
GetValueManager() const115 IEngineValueManager::Ptr EngineInputPropertyManager::GetValueManager() const
116 {
117 std::shared_lock lock { mutex_ };
118 return manager_;
119 }
120
121 } // namespace Internal
122
123 META_END_NAMESPACE()
124