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 "dependencies.h"
16 
17 META_BEGIN_NAMESPACE()
18 namespace Internal {
19 
IsActive() const20 bool Dependencies::IsActive() const
21 {
22     return active_;
23 }
Start()24 void Dependencies::Start()
25 {
26     if (active_) {
27         ++depth_;
28     } else {
29         active_ = true;
30         depth_ = 1;
31     }
32 }
End()33 void Dependencies::End()
34 {
35     if (active_) {
36         if (--depth_ == 0) {
37             active_ = false;
38             deps_.clear();
39             state_ = GenericError::SUCCESS;
40         }
41     }
42 }
AddDependency(const IProperty::ConstPtr & prop)43 ReturnError Dependencies::AddDependency(const IProperty::ConstPtr& prop)
44 {
45     if (active_ && prop) {
46         // save dependencies only once per property/depth
47         for (const auto& b : deps_) {
48             if (b.depth == depth_ && b.property == prop) {
49                 return GenericError::SUCCESS;
50             }
51         }
52         deps_.push_back({ prop, depth_ });
53     }
54     return GenericError::SUCCESS;
55 }
GetImmediateDependencies(BASE_NS::vector<IProperty::ConstPtr> & deps) const56 ReturnError Dependencies::GetImmediateDependencies(BASE_NS::vector<IProperty::ConstPtr>& deps) const
57 {
58     if (state_) {
59         BASE_NS::vector<IProperty::ConstPtr> immediate;
60         for (auto&& v : deps_) {
61             if (v.depth == depth_) {
62                 immediate.push_back(v.property);
63             }
64         }
65         deps = BASE_NS::move(immediate);
66     }
67     return state_;
68 }
69 
HasDependency(const IProperty * p) const70 bool Dependencies::HasDependency(const IProperty* p) const
71 {
72     for (auto&& v : deps_) {
73         if (v.depth >= depth_ && v.property.get() == p) {
74             return true;
75         }
76     }
77     return false;
78 }
79 
GetDeps()80 Dependencies& GetDeps()
81 {
82     thread_local Dependencies deps;
83     return deps;
84 }
85 
86 } // namespace Internal
87 META_END_NAMESPACE()