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_FUTURE_H
17 #define META_API_FUTURE_H
18 
19 #include <base/containers/type_traits.h>
20 
21 #include <meta/interface/detail/any.h>
22 #include <meta/interface/interface_helpers.h>
23 #include <meta/interface/intf_future.h>
24 
META_BEGIN_NAMESPACE()25 META_BEGIN_NAMESPACE()
26 
27 /**
28  * @brief Callable implementation for continuation functions used with futures.
29  */
30 template<typename Func>
31 class ContinuationFunction : public IntroduceInterfaces<IFutureContinuation> {
32     META_INTERFACE(
33         IntroduceInterfaces<IFutureContinuation>, ContinuationFunction, "f4736552-7365-4c8f-bbe9-a065e2c30382");
34 
35 public:
36     ContinuationFunction(Func func) : func_(BASE_NS::move(func)) {}
37 
38     IAny::Ptr Invoke(const IAny::Ptr& value) override
39     {
40         using Result = BASE_NS::remove_reference_t<decltype(func_(value))>;
41         if constexpr (!BASE_NS::is_same_v<Result, void>) {
42             return IAny::Ptr(new Any<Result>(func_(value)));
43         } else {
44             func_(value);
45             return nullptr;
46         }
47     }
48 
49 private:
50     Func func_;
51 };
52 
53 /**
54  * @brief Create continuation function from callable entity (e.g. lambda).
55  * The callable entity has to take IAny::Ptr as parameter which is the value from the future.
56  */
57 template<typename Func>
CreateContinuation(Func func)58 IFutureContinuation::Ptr CreateContinuation(Func func)
59 {
60     return IFutureContinuation::Ptr(new ContinuationFunction(BASE_NS::move(func)));
61 }
62 
63 template<typename Param>
64 struct ContinuationTypedFuntionTypeImpl {
65     using Type = void(Param);
66 };
67 
68 template<>
69 struct ContinuationTypedFuntionTypeImpl<void> {
70     using Type = void();
71 };
72 
73 template<typename Param>
74 using ContinuationTypedFuntionType = typename ContinuationTypedFuntionTypeImpl<Param>::Type;
75 
76 template<typename Type>
77 class Future {
78 public:
79     using StateType = IFuture::StateType;
80 
81     Future(IFuture::Ptr fut) : fut_(BASE_NS::move(fut)) {}
82 
83     StateType GetState() const
84     {
85         return fut_ ? fut_->GetState() : IFuture::ABANDONED;
86     }
87     StateType Wait() const
88     {
89         return fut_ ? fut_->Wait() : IFuture::ABANDONED;
90     }
91     StateType WaitFor(const TimeSpan& time) const
92     {
93         return fut_ ? fut_->WaitFor(time) : IFuture::ABANDONED;
94     }
95     IFuture::Ptr Then(const IFutureContinuation::Ptr& func, const BASE_NS::shared_ptr<ITaskQueue>& queue)
96     {
97         return fut_ ? fut_->Then(func, queue) : nullptr;
98     }
99     template<typename Func, typename = EnableIfCanInvokeWithArguments<Func, IFutureContinuation::FunctionType>>
100     auto Then(Func func, const BASE_NS::shared_ptr<ITaskQueue>& queue)
101     {
102         return Future<decltype(func(nullptr))>(fut_->Then(CreateContinuation(func), queue));
103     }
104     template<typename Func, typename = EnableIfCanInvokeWithArguments<Func, ContinuationTypedFuntionType<Type>>>
105     auto Then(Func func, const BASE_NS::shared_ptr<ITaskQueue>& queue, int = 0)
106     {
107         using ReturnType = decltype(func(Type {}));
108         return Future<ReturnType>(fut_->Then(CreateContinuation([f = BASE_NS::move(func)](const IAny::Ptr& v) {
109             if (v) {
110                 Type value {};
111                 if (v->GetValue(value)) {
112                     return f(value);
113                 }
114                 CORE_LOG_W("Type mismatch for future then");
115             }
116             if constexpr (!BASE_NS::is_same_v<ReturnType, void>) {
117                 return ReturnType {};
118             }
119         }),
120             queue));
121     }
122 
123     Type GetResult() const
124     {
125         if (fut_) {
126             return fut_->GetResultOr<Type>(Type {});
127         }
128         return Type {};
129     }
130     IFuture::Ptr GetFuture() const
131     {
132         return fut_;
133     }
134 
135     operator IFuture::Ptr() const
136     {
137         return fut_;
138     }
139 
140     explicit operator bool() const
141     {
142         return fut_ != nullptr;
143     }
144 
145 private:
146     IFuture::Ptr fut_;
147 };
148 
149 template<typename Type>
150 Type GetResultOr(const Future<Type>& f, NonDeduced_t<Type> def)
151 {
152     auto fut = f.GetFuture();
153     if (fut) {
154         if (auto p = fut->GetResult()) {
155             return GetValue<Type>(*p, BASE_NS::move(def));
156         }
157     }
158     return BASE_NS::move(def);
159 }
160 
161 META_END_NAMESPACE()
162 
163 #endif
164