• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025-2025 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 #ifndef MOCK_FFRT_API_CPP_FUTURE_H
16 #define MOCK_FFRT_API_CPP_FUTURE_H
17 #include <memory>
18 #include <optional>
19 #include <chrono>
20 #include "cpp/condition_variable.h"
21 #include "thread.h"
22 
23 namespace ffrt {
24 struct non_copyable {
25 protected:
26     non_copyable() = default;
27     ~non_copyable() = default;
28     non_copyable(const non_copyable &) = delete;
29     non_copyable &operator=(const non_copyable &) = delete;
30 };
31 enum class future_status { ready, timeout, deferred };
32 
33 namespace detail {
34 template <typename Derived>
35 struct shared_state_base : private non_copyable {
waitshared_state_base36     void wait() const noexcept
37     {}
38 
39     template <typename Rep, typename Period>
wait_forshared_state_base40     future_status wait_for(const std::chrono::duration<Rep, Period> &waitTime) const noexcept
41     {
42         return future_status::timeout;
43     }
44 
45     template <typename Clock, typename Duration>
wait_untilshared_state_base46     future_status wait_until(const std::chrono::time_point<Clock, Duration> &tp) const noexcept
47     {
48         return future_status::timeout;
49     }
50 };
51 
52 template <typename R>
53 struct shared_state : public shared_state_base<shared_state<R>> {
set_valueshared_state54     void set_value(const R &value) noexcept
55     {}
56 
set_valueshared_state57     void set_value(R &&value) noexcept
58     {}
59 
getshared_state60     R &get() noexcept
61     {
62         return m_res.value();
63     }
64 
has_valueshared_state65     bool has_value() const noexcept
66     {
67         return m_res.has_value();
68     }
69 
70 private:
71     std::optional<R> m_res;
72 };
73 
74 template <>
75 struct shared_state<void> : public shared_state_base<shared_state<void>> {
76     void set_value() noexcept
77     {}
78 
79     void get() noexcept
80     {}
81 
82     bool has_value() const noexcept
83     {
84         return true;
85     }
86 };
87 };  // namespace detail
88 
89 template <typename R>
90 class future : private non_copyable {
91     template <typename>
92     friend struct promise;
93 
94     template <typename>
95     friend struct packaged_task;
96 
97 public:
98     explicit future(const std::shared_ptr<detail::shared_state<R>> &state) noexcept
99     {}
100 
101     future() noexcept = default;
102 
103     future(future &&fut) noexcept
104     {}
105     future &operator=(future &&fut) noexcept
106     {
107         return *this;
108     }
109 
110     bool valid() const noexcept
111     {
112         return true;
113     }
114 
115     R get() noexcept
116     {
117         return {};
118     }
119 
120     template <typename Rep, typename Period>
121     future_status wait_for(const std::chrono::duration<Rep, Period> &waitTime) const noexcept
122     {}
123 
124     template <typename Clock, typename Duration>
125     future_status wait_until(const std::chrono::time_point<Clock, Duration> &tp) const noexcept
126     {
127         return future_status::timeout;
128     }
129 
130     void wait() const noexcept
131     {}
132 
133     void swap(future<R> &rhs) noexcept
134     {}
135 };
136 
137 template <typename R>
138 struct promise : private non_copyable {
139     promise() noexcept : m_state{std::make_shared<detail::shared_state<R>>()}
140     {}
141     promise(promise &&p) noexcept
142     {}
143     promise &operator=(promise &&p) noexcept
144     {
145         return *this;
146     }
147 
148     void set_value(const R &value) noexcept
149     {}
150 
151     void set_value(R &&value) noexcept
152     {}
153 
154     future<R> get_future() noexcept
155     {
156         return future<R>{m_state};
157     }
158 
159     void swap(promise<R> &rhs) noexcept
160     {}
161 
162 private:
163     std::shared_ptr<detail::shared_state<R>> m_state;
164 };
165 
166 template <>
167 struct promise<void> : private non_copyable {
168     promise() noexcept
169     {}
170     promise(promise &&p) noexcept
171     {}
172     promise &operator=(promise &&p) noexcept
173     {
174         return *this;
175     }
176 
177     void set_value() noexcept
178     {}
179 
180     future<void> get_future() noexcept
181     {
182         return {};
183     }
184 
185     void swap(promise<void> &rhs) noexcept
186     {}
187 };
188 
189 template <typename F>
190 struct packaged_task;
191 
192 template <typename R, typename... Args>
193 struct packaged_task<R(Args...)> {
194     packaged_task() noexcept = default;
195 
196     packaged_task(const packaged_task &pt) noexcept
197     {}
198 
199     packaged_task(packaged_task &&pt) noexcept
200     {}
201 
202     packaged_task &operator=(packaged_task &&pt) noexcept
203     {
204         return *this;
205     }
206 
207     template <typename F>
208     explicit packaged_task(F &&f) noexcept
209     {}
210 
211     bool valid() const noexcept
212     {
213         return true;
214     }
215 
216     future<R> get_future() noexcept
217     {
218         return {};
219     }
220 
221     void operator()(Args... args)
222     {}
223 
224     void swap(packaged_task &pt) noexcept
225     {}
226 };
227 
228 template <typename F, typename... Args>
229 future<std::invoke_result_t<std::decay_t<F>, std::decay_t<Args>...>> async(F &&f, Args &&...args)
230 {
231     return {};
232 }
233 }  // namespace ffrt
234 #endif