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_TYPES_H
17 #define META_API_INTERNAL_ITERATION_TYPES_H
18
19 #include <meta/api/locking.h>
20 #include <meta/base/interface_traits.h>
21 #include <meta/interface/interface_helpers.h>
22 #include <meta/interface/intf_iterable.h>
23
META_BEGIN_NAMESPACE()24 META_BEGIN_NAMESPACE()
25 namespace Internal {
26
27 template<typename Func>
28 struct IterationFuncType;
29
30 template<typename R, typename Type>
31 struct IterationFuncType<R(Type)> {
32 using ArgType = PlainType_t<Type>;
33 using ActualType = Type;
34 };
35
36 template<typename T>
37 using IterationArgType = typename IterationFuncType<FuncToSignature_t<T>>::ArgType;
38 template<typename T>
39 using IterationForwardArgType = typename IterationFuncType<FuncToSignature_t<T>>::ArgType;
40
41 template<template<typename> class Intf, typename Func>
42 class IterationCallable : public IntroduceInterfaces<Intf<IterationArgType<Func>>> {
43 public:
44 IterationCallable(Func f) : func_(BASE_NS::move(f)) {}
45
46 protected:
47 Func func_;
48
49 using ArgType = typename IntroduceInterfaces<Intf<IterationArgType<Func>>>::ArgType;
50 IterationResult Invoke(ArgType arg) final
51 {
52 if constexpr (BASE_NS::is_same_v<IterationResult, decltype(func_(arg))> ||
53 BASE_NS::is_same_v<IterationResult::Type, decltype(func_(arg))>) {
54 return func_(arg);
55 } else {
56 return func_(arg) ? IterationResult::CONTINUE : IterationResult::STOP;
57 }
58 }
59 };
60
61 template<typename T>
62 using DisableIfCallable = BASE_NS::enable_if_t<!BASE_NS::is_convertible_v<T, const ICallable::Ptr&>>;
63
64 template<typename Func>
65 auto MakeIterationCallable(Func f)
66 {
67 return ICallable::Ptr(new IterationCallable<IIterableCallable, Func>(BASE_NS::move(f)));
68 }
69 template<typename Func>
70 auto MakeIterationConstCallable(Func f)
71 {
72 return ICallable::Ptr(new IterationCallable<IIterableConstCallable, Func>(BASE_NS::move(f)));
73 }
74
75 template<template<typename> class InterableCallable, typename Iterable, typename Func>
76 auto CallIterate(const Iterable& i, Func&& func, IterateStrategy is)
77 {
78 IterationCallable<InterableCallable, Func> f(BASE_NS::forward<Func>(func));
79 InterfaceLock lock(is.lock, i);
80 return i->Iterate(IterationParameters { f, is });
81 }
82
83 template<template<typename> class InterableCallable, typename Iterable>
84 auto CallIterate(const Iterable& i, ICallable& func, IterateStrategy is)
85 {
86 InterfaceLock lock(is.lock, i);
87 return i->Iterate(IterationParameters { func, is });
88 }
89
90 } // namespace Internal
91 META_END_NAMESPACE()
92
93 #endif
94