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