olp-cpp-sdk 1.24.0
Loading...
Searching...
No Matches
Continuation.inl
1/*
2 * Copyright (C) 2022 HERE Europe B.V.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * SPDX-License-Identifier: Apache-2.0
17 * License-Filename: LICENSE
18 */
19
20#pragma once
21
22#include <memory>
23
24namespace olp {
25namespace thread {
26
27class ExecutionContext;
28class TaskScheduler;
29
30template <typename ResultType>
32 // Run should not start an execution if the `Finally` method is not called
33 // for the task continuation.
34 if (!finally_callback_) {
35 impl_.Clear();
36 return;
37 }
38
39 // In case of the continuation is cancelled before calling the `Run` method,
40 // return the `Cancelled` error.
41 if (impl_.Cancelled()) {
42 const auto finally_callback = std::move(finally_callback_);
43 if (finally_callback) {
44 finally_callback(client::ApiError::Cancelled());
45 }
46
47 impl_.Clear();
48 return;
49 }
50
51 // Use std::move to set the `finally_callback_` member to nullptr.
52 // It's needed to check if the execution is done, and prevent calling
53 // the `Cancel` method of the `Continuation` object.
54 // The `SetError` method of the `ExecutionContext` should not execute a
55 // callback as well.
56 impl_.SetFailedCallback([=](client::ApiError error) {
57 const auto finally_callback = std::move(finally_callback_);
58 if (finally_callback) {
59 finally_callback(std::move(error));
60 }
61 });
62
63 impl_.Run([=](void* input, bool cancelled) {
64 impl_.Clear();
65
66 const auto finally_callback = std::move(finally_callback_);
67 if (finally_callback) {
68 if (cancelled) {
69 finally_callback(client::ApiError::Cancelled());
70 } else {
71 finally_callback(std::move(*static_cast<ResultType*>(input)));
72 }
73 }
74 });
75}
76
77template <typename ResultType>
79 FinallyCallbackType finally_callback) {
80 finally_callback_ = std::move(finally_callback);
81 return *this;
82}
83
84template <typename NewType>
86 ExecutionContext context,
87 std::function<void(ExecutionContext, std::function<void(NewType)>)> func) {
88 using NewResultType = internal::RemoveRefAndConst<NewType>;
89 return {[=](void*, CallbackType callback) {
90 func(context, [callback](NewResultType input) {
91 callback(static_cast<NewResultType*>(&input));
92 });
93 },
94 [](void* input) {
95 auto in = *static_cast<NewResultType*>(input);
96 auto result = std::make_unique<NewResultType>(std::move(in));
97 return internal::MakeUntypedUnique(std::move(result));
98 }};
99}
100
101template <typename Callable>
103 Callable task) {
104 using NewResultType = internal::AsyncResultType<Callable>;
105 using Function = internal::TypeToFunctionInput<NewResultType>;
106 return Then(std::function<void(Function)>(std::forward<Callable>(task)));
107}
108
109template <typename ResultType>
110template <typename Callable>
112 Callable task) {
113 using NewResultType = internal::DeducedType<Callable>;
114 using Function = internal::TypeToFunctionInput<NewResultType>;
115 return Then(std::function<void(ExecutionContext, ResultType, Function)>(
116 std::move(task)));
117}
118
119template <typename ResultType>
120template <typename NewType>
123 std::function<void(NewType)>)>
124 task) {
125 using NewResultType = internal::RemoveRefAndConst<NewType>;
126 const auto context = impl_.GetExecutionContext();
127
128 return impl_.Then(
129 {[=](void* input, CallbackType callback) {
130 auto in = *static_cast<ResultType*>(input);
131 task(std::move(context), std::move(in),
132 [=](NewResultType arg) { callback(static_cast<void*>(&arg)); });
133 },
134 [](void* input) {
135 auto in = *static_cast<NewResultType*>(input);
136 auto result = std::make_unique<NewResultType>(std::move(in));
137 return internal::MakeUntypedUnique(std::move(result));
138 }});
139}
140
141template <typename ResultType>
143 if (!finally_callback_) {
145 }
146
147 auto context = impl_.GetExecutionContext();
149 [=]() mutable { context.CancelOperation(); });
150}
151
152template <typename ResultType>
154 std::shared_ptr<TaskScheduler> scheduler, ExecutionContext context,
155 std::function<void(ExecutionContext, std::function<void(ResultType)>)> task)
156 : impl_(scheduler, context,
157 Continuation<void>::ToAsyncTask(context, std::move(task))) {}
158
159template <typename ResultType>
161 : impl_(std::move(continuation)) {}
162
163} // namespace thread
164} // namespace olp
A wrapper around an internal error or HTTP status code.
Definition ApiError.h:37
static ApiError Cancelled(const char *message="Cancelled")
Creates the ApiError instance with the cancelled error code and description.
Definition ApiError.h:47
Cancels service requests.
Definition CancellationToken.h:33
A generic template for Continuation.
Definition Continuation.h:140
Continuation< internal::DeducedType< Callable > > Then(Callable task)
Adds the next asynchronous task to the ContinuationImpl instance.
Definition Continuation.inl:111
client::CancellationToken CancelToken()
Provides a token to cancel the task.
Definition Continuation.inl:142
void Run()
Starts the execution of the task continuation chain.
Definition Continuation.inl:31
Continuation()=default
The default constructor of Continuation<ResultType>.
Continuation & Finally(FinallyCallbackType finally_callback)
Handles the finalization of the Continuation instance and sets a callback for it.
Definition Continuation.inl:78
Handles the cancellation and final mechanisms.
Definition ExecutionContext.h:31
Provides mechanisms to create a chain of tasks and start, cancel, and finalize an execution.
Definition Continuation.h:46
std::pair< AsyncTaskType, TaskType > ContinuationTask
An alias for a pair of task continuation chain types.
Definition Continuation.h:61
Rules all the other namespaces.
Definition AppleSignInProperties.h:24