18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci#include "dependencies.h"
168bf80f4bSopenharmony_ci
178bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE()
188bf80f4bSopenharmony_cinamespace Internal {
198bf80f4bSopenharmony_ci
208bf80f4bSopenharmony_cibool Dependencies::IsActive() const
218bf80f4bSopenharmony_ci{
228bf80f4bSopenharmony_ci    return active_;
238bf80f4bSopenharmony_ci}
248bf80f4bSopenharmony_civoid Dependencies::Start()
258bf80f4bSopenharmony_ci{
268bf80f4bSopenharmony_ci    if (active_) {
278bf80f4bSopenharmony_ci        ++depth_;
288bf80f4bSopenharmony_ci    } else {
298bf80f4bSopenharmony_ci        active_ = true;
308bf80f4bSopenharmony_ci        depth_ = 1;
318bf80f4bSopenharmony_ci    }
328bf80f4bSopenharmony_ci}
338bf80f4bSopenharmony_civoid Dependencies::End()
348bf80f4bSopenharmony_ci{
358bf80f4bSopenharmony_ci    if (active_) {
368bf80f4bSopenharmony_ci        if (--depth_ == 0) {
378bf80f4bSopenharmony_ci            active_ = false;
388bf80f4bSopenharmony_ci            deps_.clear();
398bf80f4bSopenharmony_ci            state_ = GenericError::SUCCESS;
408bf80f4bSopenharmony_ci        }
418bf80f4bSopenharmony_ci    }
428bf80f4bSopenharmony_ci}
438bf80f4bSopenharmony_ciReturnError Dependencies::AddDependency(const IProperty::ConstPtr& prop)
448bf80f4bSopenharmony_ci{
458bf80f4bSopenharmony_ci    if (active_ && prop) {
468bf80f4bSopenharmony_ci        // save dependencies only once per property/depth
478bf80f4bSopenharmony_ci        for (const auto& b : deps_) {
488bf80f4bSopenharmony_ci            if (b.depth == depth_ && b.property == prop) {
498bf80f4bSopenharmony_ci                return GenericError::SUCCESS;
508bf80f4bSopenharmony_ci            }
518bf80f4bSopenharmony_ci        }
528bf80f4bSopenharmony_ci        deps_.push_back({ prop, depth_ });
538bf80f4bSopenharmony_ci    }
548bf80f4bSopenharmony_ci    return GenericError::SUCCESS;
558bf80f4bSopenharmony_ci}
568bf80f4bSopenharmony_ciReturnError Dependencies::GetImmediateDependencies(BASE_NS::vector<IProperty::ConstPtr>& deps) const
578bf80f4bSopenharmony_ci{
588bf80f4bSopenharmony_ci    if (state_) {
598bf80f4bSopenharmony_ci        BASE_NS::vector<IProperty::ConstPtr> immediate;
608bf80f4bSopenharmony_ci        for (auto&& v : deps_) {
618bf80f4bSopenharmony_ci            if (v.depth == depth_) {
628bf80f4bSopenharmony_ci                immediate.push_back(v.property);
638bf80f4bSopenharmony_ci            }
648bf80f4bSopenharmony_ci        }
658bf80f4bSopenharmony_ci        deps = BASE_NS::move(immediate);
668bf80f4bSopenharmony_ci    }
678bf80f4bSopenharmony_ci    return state_;
688bf80f4bSopenharmony_ci}
698bf80f4bSopenharmony_ci
708bf80f4bSopenharmony_cibool Dependencies::HasDependency(const IProperty* p) const
718bf80f4bSopenharmony_ci{
728bf80f4bSopenharmony_ci    for (auto&& v : deps_) {
738bf80f4bSopenharmony_ci        if (v.depth >= depth_ && v.property.get() == p) {
748bf80f4bSopenharmony_ci            return true;
758bf80f4bSopenharmony_ci        }
768bf80f4bSopenharmony_ci    }
778bf80f4bSopenharmony_ci    return false;
788bf80f4bSopenharmony_ci}
798bf80f4bSopenharmony_ci
808bf80f4bSopenharmony_ciDependencies& GetDeps()
818bf80f4bSopenharmony_ci{
828bf80f4bSopenharmony_ci    thread_local Dependencies deps;
838bf80f4bSopenharmony_ci    return deps;
848bf80f4bSopenharmony_ci}
858bf80f4bSopenharmony_ci
868bf80f4bSopenharmony_ci} // namespace Internal
878bf80f4bSopenharmony_ciMETA_END_NAMESPACE()