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