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