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
168bf80f4bSopenharmony_ci#ifndef META_API_ITERATION_H
178bf80f4bSopenharmony_ci#define META_API_ITERATION_H
188bf80f4bSopenharmony_ci
198bf80f4bSopenharmony_ci#include <meta/api/internal/iteration.h>
208bf80f4bSopenharmony_ci#include <meta/interface/intf_lockable.h>
218bf80f4bSopenharmony_ci
228bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE()
238bf80f4bSopenharmony_ci
248bf80f4bSopenharmony_ci/// Create suitable callable for iteration from lambda (or alike)
258bf80f4bSopenharmony_ciusing Internal::MakeIterationCallable;
268bf80f4bSopenharmony_ci
278bf80f4bSopenharmony_ci/// Create suitable const callable for iteration from lambda (or alike)
288bf80f4bSopenharmony_ciusing Internal::MakeIterationConstCallable;
298bf80f4bSopenharmony_ci
308bf80f4bSopenharmony_ci/**
318bf80f4bSopenharmony_ci * @brief Iterate over sequence of elements (e.g. container) and call function for each element.
328bf80f4bSopenharmony_ci *        Takes unique lock over the sequence of elements if it is ILockable
338bf80f4bSopenharmony_ci * @param c Sequence of elements, must implement IIterable
348bf80f4bSopenharmony_ci * @param func Function that is called for each element, can mutate the element value
358bf80f4bSopenharmony_ci * @param traversal Control how to iterate over elements
368bf80f4bSopenharmony_ci * @return True if the iteration did not fail
378bf80f4bSopenharmony_ci */
388bf80f4bSopenharmony_citemplate<typename Iterable, typename Func>
398bf80f4bSopenharmony_cibool ForEachUnique(
408bf80f4bSopenharmony_ci    const BASE_NS::shared_ptr<Iterable>& c, Func&& func, TraversalType traversal = TraversalType::NO_HIERARCHY)
418bf80f4bSopenharmony_ci{
428bf80f4bSopenharmony_ci    return Internal::Iterate(
438bf80f4bSopenharmony_ci        c,
448bf80f4bSopenharmony_ci        [f = BASE_NS::forward<Func>(func)](Internal::IterationArgType<Func>& arg) {
458bf80f4bSopenharmony_ci            f(arg);
468bf80f4bSopenharmony_ci            return true;
478bf80f4bSopenharmony_ci        },
488bf80f4bSopenharmony_ci        IterateStrategy { traversal, LockType::UNIQUE_LOCK });
498bf80f4bSopenharmony_ci}
508bf80f4bSopenharmony_ci
518bf80f4bSopenharmony_ci/**
528bf80f4bSopenharmony_ci * @brief Iterate over sequence of elements (e.g. container) and call function for each element.
538bf80f4bSopenharmony_ci *        Takes shared lock over the sequence of elements if it is IReadWriteLockable
548bf80f4bSopenharmony_ci * @param c Sequence of elements, must implement IIterable
558bf80f4bSopenharmony_ci * @param func Function that is called for each element, cannot mutate the element value
568bf80f4bSopenharmony_ci * @param traversal Control how to iterate over elements
578bf80f4bSopenharmony_ci * @return True if the iteration did not fail
588bf80f4bSopenharmony_ci */
598bf80f4bSopenharmony_citemplate<typename Iterable, typename Func>
608bf80f4bSopenharmony_cibool ForEachShared(
618bf80f4bSopenharmony_ci    const BASE_NS::shared_ptr<Iterable>& c, Func&& func, TraversalType traversal = TraversalType::NO_HIERARCHY)
628bf80f4bSopenharmony_ci{
638bf80f4bSopenharmony_ci    return Internal::ConstIterate(
648bf80f4bSopenharmony_ci        c,
658bf80f4bSopenharmony_ci        [f = BASE_NS::forward<Func>(func)](const Internal::IterationArgType<Func>& arg) {
668bf80f4bSopenharmony_ci            f(arg);
678bf80f4bSopenharmony_ci            return true;
688bf80f4bSopenharmony_ci        },
698bf80f4bSopenharmony_ci        IterateStrategy { traversal, LockType::SHARED_LOCK });
708bf80f4bSopenharmony_ci}
718bf80f4bSopenharmony_ci
728bf80f4bSopenharmony_ci/**
738bf80f4bSopenharmony_ci * @brief Iterate over sequence of elements (e.g. container) and call function for each element
748bf80f4bSopenharmony_ci *        as long as the function returns true. If it returns false, stop iteration.
758bf80f4bSopenharmony_ci *        Takes unique lock over the sequence of elements if it is ILockable
768bf80f4bSopenharmony_ci * @param c Sequence of elements, must implement IIterable
778bf80f4bSopenharmony_ci * @param func Function that is called for each element, can mutate the element value
788bf80f4bSopenharmony_ci * @param traversal Control how to iterate over elements
798bf80f4bSopenharmony_ci * @return True if the iteration was stopped (this usually indicates the user function returned false)
808bf80f4bSopenharmony_ci */
818bf80f4bSopenharmony_citemplate<typename Iterable, typename Func>
828bf80f4bSopenharmony_cibool IterateUnique(
838bf80f4bSopenharmony_ci    const BASE_NS::shared_ptr<Iterable>& c, Func&& func, TraversalType traversal = TraversalType::NO_HIERARCHY)
848bf80f4bSopenharmony_ci{
858bf80f4bSopenharmony_ci    return Internal::Iterate(c, BASE_NS::forward<Func>(func), IterateStrategy { traversal, LockType::UNIQUE_LOCK })
868bf80f4bSopenharmony_ci               .value == IterationResult::STOP;
878bf80f4bSopenharmony_ci}
888bf80f4bSopenharmony_ci
898bf80f4bSopenharmony_ci/**
908bf80f4bSopenharmony_ci * @brief Iterate over sequence of elements (e.g. container) and call function for each element
918bf80f4bSopenharmony_ci *        as long as the function returns true. If it returns false, stop iteration.
928bf80f4bSopenharmony_ci *        Takes shared lock over the sequence of elements if it is IReadWriteLockable
938bf80f4bSopenharmony_ci * @param c Sequence of elements, must implement IIterable
948bf80f4bSopenharmony_ci * @param func Function that is called for each element, cannot mutate the element value
958bf80f4bSopenharmony_ci * @param traversal Control how to iterate over elements
968bf80f4bSopenharmony_ci * @return True if the iteration was stopped (this usually indicates the user function returned false)
978bf80f4bSopenharmony_ci */
988bf80f4bSopenharmony_citemplate<typename Iterable, typename Func>
998bf80f4bSopenharmony_cibool IterateShared(
1008bf80f4bSopenharmony_ci    const BASE_NS::shared_ptr<Iterable>& c, Func&& func, TraversalType traversal = TraversalType::NO_HIERARCHY)
1018bf80f4bSopenharmony_ci{
1028bf80f4bSopenharmony_ci    return Internal::ConstIterate(c, BASE_NS::forward<Func>(func), IterateStrategy { traversal, LockType::SHARED_LOCK })
1038bf80f4bSopenharmony_ci               .value == IterationResult::STOP;
1048bf80f4bSopenharmony_ci}
1058bf80f4bSopenharmony_ci
1068bf80f4bSopenharmony_ci/**
1078bf80f4bSopenharmony_ci * @brief Iterate over sequence of elements (e.g. container) and call function for each element
1088bf80f4bSopenharmony_ci *        as long as the function returns true. If it returns false, stop iteration.
1098bf80f4bSopenharmony_ci *        Takes unique lock over the sequence of elements if it is ILockable
1108bf80f4bSopenharmony_ci * @param c Sequence of elements, must implement IIterable
1118bf80f4bSopenharmony_ci * @param func Function that is called for each element, can mutate the element value
1128bf80f4bSopenharmony_ci * @param is Strategy how to iterate over elements
1138bf80f4bSopenharmony_ci * @return True if the iteration was stopped (this usually indicates the user function returned false)
1148bf80f4bSopenharmony_ci */
1158bf80f4bSopenharmony_citemplate<typename Iterable, typename Func>
1168bf80f4bSopenharmony_cibool Iterate(const BASE_NS::shared_ptr<Iterable>& c, Func&& func, IterateStrategy is)
1178bf80f4bSopenharmony_ci{
1188bf80f4bSopenharmony_ci    return Internal::Iterate(c, BASE_NS::forward<Func>(func), is).value == IterationResult::STOP;
1198bf80f4bSopenharmony_ci}
1208bf80f4bSopenharmony_ci
1218bf80f4bSopenharmony_ci/**
1228bf80f4bSopenharmony_ci * @brief Iterate over sequence of elements (e.g. container) and call function for each element
1238bf80f4bSopenharmony_ci *        as long as the function returns true. If it returns false, stop iteration.
1248bf80f4bSopenharmony_ci *        Takes shared lock over the sequence of elements if it is IReadWriteLockable
1258bf80f4bSopenharmony_ci * @param c Sequence of elements, must implement IIterable
1268bf80f4bSopenharmony_ci * @param func Function that is called for each element, cannot mutate the element value
1278bf80f4bSopenharmony_ci * @param is Strategy how to iterate over elements
1288bf80f4bSopenharmony_ci * @return True if the iteration was stopped (this usually indicates the user function returned false)
1298bf80f4bSopenharmony_ci */
1308bf80f4bSopenharmony_citemplate<typename Iterable, typename Func>
1318bf80f4bSopenharmony_cibool ConstIterate(const BASE_NS::shared_ptr<Iterable>& c, Func&& func, IterateStrategy is)
1328bf80f4bSopenharmony_ci{
1338bf80f4bSopenharmony_ci    return Internal::ConstIterate(c, BASE_NS::forward<Func>(func), is).value == IterationResult::STOP;
1348bf80f4bSopenharmony_ci}
1358bf80f4bSopenharmony_ci
1368bf80f4bSopenharmony_ciMETA_END_NAMESPACE()
1378bf80f4bSopenharmony_ci
1388bf80f4bSopenharmony_ci#endif
139