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_EXT_TASK_QUEUE_H
17 #define META_EXT_TASK_QUEUE_H
18
19 #include <base/containers/type_traits.h>
20
21 #include <meta/ext/object_fwd.h>
22 #include <meta/interface/interface_helpers.h>
23 #include <meta/interface/intf_promise.h>
24 #include <meta/interface/intf_task_queue_extend.h>
25
META_BEGIN_NAMESPACE()26 META_BEGIN_NAMESPACE()
27
28 class PromisedQueueTask : public IntroduceInterfaces<ITaskQueueTask, ITaskScheduleInfo> {
29 public:
30 PromisedQueueTask(ITaskQueueWaitableTask::Ptr task, IPromise::Ptr p)
31 : task_(BASE_NS::move(task)), promise_(BASE_NS::move(p))
32 {}
33
34 bool Invoke() override
35 {
36 auto res = task_->Invoke();
37 // make sure the task is destroyed before future wait returns,
38 // so that any captured objects are also destroyed.
39 task_.reset();
40 promise_->Set(BASE_NS::move(res));
41 return false;
42 }
43
44 void SetQueueAndToken(const ITaskQueue::Ptr& q, ITaskQueue::Token t) override
45 {
46 promise_->SetQueueInfo(q, t);
47 }
48
49 [[nodiscard]] IFuture::Ptr GetFuture()
50 {
51 return promise_->GetFuture();
52 }
53
54 IPromise::Ptr GetPromise()
55 {
56 return promise_;
57 }
58
59 private:
60 ITaskQueueWaitableTask::Ptr task_;
61 IPromise::Ptr promise_;
62 };
63 META_END_NAMESPACE()
64
65 #endif
66