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 #ifndef META_API_INTERNAL_ITERATION_H
17 #define META_API_INTERNAL_ITERATION_H
18 
19 #include <meta/api/internal/breadth_first_iteration.h>
20 #include <meta/api/internal/depth_first_iteration.h>
21 #include <meta/api/internal/iteration_types.h>
22 #include <meta/base/interface_traits.h>
23 #include <meta/interface/intf_iterable.h>
24 
META_BEGIN_NAMESPACE()25 META_BEGIN_NAMESPACE()
26 namespace Internal {
27 
28 template<template<typename> class InterableCallable, typename Iterable, typename Func>
29 IterationResult IterateImpl(const Iterable& ite, Func&& func, IterateStrategy is)
30 {
31     if (!ite) {
32         return IterationResult::FAILED;
33     }
34     if (is.traversal == TraversalType::NO_HIERARCHY) {
35         return CallIterate<InterableCallable>(ite, BASE_NS::forward<Func>(func), is);
36     }
37     // map the FULL_HIERARCHY to BREADTH_FIRST_ORDER by default
38     if (is.traversal == TraversalType::FULL_HIERARCHY) {
39         is.traversal = TraversalType::BREADTH_FIRST_ORDER;
40     }
41     if (is.traversal == TraversalType::DEPTH_FIRST_PRE_ORDER || is.traversal == TraversalType::DEPTH_FIRST_POST_ORDER) {
42         return DepthFirstOrderIterate<InterableCallable>(ite, func, is);
43     }
44     if (is.traversal == TraversalType::BREADTH_FIRST_ORDER) {
45         return BreadthFirstOrderIterate<InterableCallable>(ite, func, is);
46     }
47     return IterationResult::FAILED;
48 }
49 
50 template<typename Iterable, typename Func, typename = DisableIfCallable<Func>>
51 IterationResult Iterate(const BASE_NS::shared_ptr<Iterable>& c, Func&& func, IterateStrategy is)
52 {
53     return IterateImpl<IIterableCallable>(interface_pointer_cast<IIterable>(c), BASE_NS::forward<Func>(func), is);
54 }
55 
56 template<typename Iterable>
57 IterationResult Iterate(const BASE_NS::shared_ptr<Iterable>& c, const ICallable::Ptr& func, IterateStrategy is)
58 {
59     return IterateImpl<IIterableCallable>(interface_pointer_cast<IIterable>(c), *func, is);
60 }
61 
62 template<typename Iterable, typename Func, typename = DisableIfCallable<Func>>
63 IterationResult ConstIterate(const BASE_NS::shared_ptr<Iterable>& c, Func&& func, IterateStrategy is)
64 {
65     return IterateImpl<IIterableConstCallable>(interface_pointer_cast<IIterable>(c), BASE_NS::forward<Func>(func), is);
66 }
67 
68 template<typename Iterable>
69 IterationResult ConstIterate(const BASE_NS::shared_ptr<Iterable>& c, const ICallable::Ptr& func, IterateStrategy is)
70 {
71     return IterateImpl<IIterableConstCallable>(interface_pointer_cast<IIterable>(c), *func, is);
72 }
73 
74 } // namespace Internal
75 META_END_NAMESPACE()
76 
77 #endif
78