olp-cpp-sdk 1.24.0
Loading...
Searching...
No Matches
Continuation.h
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 <deque>
23#include <functional>
24#include <memory>
25
26#include <olp/core/client/ApiError.h>
27#include <olp/core/client/ApiResponse.h>
28#include <olp/core/client/TaskContext.h>
29#include <olp/core/thread/ExecutionContext.h>
30#include <olp/core/thread/TypeHelpers.h>
31
32namespace olp {
33namespace thread {
34
35class TaskScheduler;
36
37namespace internal {
38
46class CORE_API ContinuationImpl final {
47 public:
49 using FailedCallback = std::function<void(client::ApiError)>;
51 using OutResultType = std::unique_ptr<UntypedSmartPointer>;
53 using TaskType = std::function<OutResultType(void*)>;
55 using CallbackType = std::function<void(void*)>;
57 using AsyncTaskType = std::function<void(void*, CallbackType)>;
59 using FinalCallbackType = std::function<void(void*, bool)>;
61 using ContinuationTask = std::pair<AsyncTaskType, TaskType>;
62
64 ContinuationImpl() = default;
65
74 ContinuationImpl(std::shared_ptr<TaskScheduler> task_scheduler,
76
87
93 void Run(FinalCallbackType callback);
94
101
108 bool Cancelled() const;
109
117
121 void Clear();
122
123 private:
124 std::shared_ptr<thread::TaskScheduler> task_scheduler_;
125 std::deque<ContinuationTask> tasks_;
126 ExecutionContext execution_context_;
127
133 bool change_allowed{true};
134};
135
136} // namespace internal
137
139template <typename ResultType>
140class CORE_API Continuation final {
144 using FinallyCallbackType = std::function<void(Response)>;
148 using OutResultType = ContinuationImplType::OutResultType;
150 using TaskType = ContinuationImplType::TaskType;
152 using CallbackType = ContinuationImplType::CallbackType;
154 using ContinuationTaskType =
155 std::function<void(ExecutionContext, std::function<void(ResultType)>)>;
156
157 public:
159 Continuation() = default;
160
169 Continuation(std::shared_ptr<thread::TaskScheduler> scheduler,
170 ExecutionContext context, ContinuationTaskType task);
171
178
186 template <typename Callable>
188
190 void Run();
191
197 client::CancellationToken CancelToken();
198
206 template <typename NewType>
208 std::function<void(ExecutionContext, ResultType,
209 std::function<void(NewType)>)>
210 execution_func);
211
221 Continuation& Finally(FinallyCallbackType finally_callback);
222
223 private:
224 FinallyCallbackType finally_callback_;
226};
227
233template <>
234class CORE_API Continuation<void> final {
235 public:
237 Continuation() = delete;
238
248 template <typename Callable>
250
251 private:
253 using CallbackType = internal::ContinuationImpl::CallbackType;
257 using OutResultType = ContinuationImplType::OutResultType;
259 using TaskType = ContinuationImplType::TaskType;
260
261 template <typename ResultType>
262 friend class Continuation;
263
271 template <typename NewType>
273 ExecutionContext context,
274 std::function<void(ExecutionContext, std::function<void(NewType)>)> task);
275
277};
278
279} // namespace thread
280} // namespace olp
281
282#include "Continuation.inl"
A wrapper around an internal error or HTTP status code.
Definition ApiError.h:37
Represents a request outcome.
Definition ApiResponse.h:65
Cancels service requests.
Definition CancellationToken.h:33
Continuation()=delete
Do not create the deleted Continuation constructor as an instance.
A generic template for Continuation.
Definition Continuation.h:140
Continuation(std::shared_ptr< thread::TaskScheduler > scheduler, ExecutionContext context, ContinuationTaskType task)
Creates the Continuation instance.
Continuation()=default
The default constructor of Continuation<ResultType>.
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::function< OutResultType(void *)> TaskType
The type of ContinuationType.
Definition Continuation.h:53
void Clear()
Clears the continuation chain tasks.
ContinuationImpl(std::shared_ptr< TaskScheduler > task_scheduler, ExecutionContext context, ContinuationTask task)
Creates the ContinuationImpl instance.
std::pair< AsyncTaskType, TaskType > ContinuationTask
An alias for a pair of task continuation chain types.
Definition Continuation.h:61
void SetFailedCallback(FailedCallback callback)
Sets a callback on calling SetError.
std::function< void(client::ApiError)> FailedCallback
An alias for the function that returns an error as a callback.
Definition Continuation.h:49
std::function< void(void *, bool)> FinalCallbackType
An alias for the processing tasks finalization type.
Definition Continuation.h:59
std::unique_ptr< UntypedSmartPointer > OutResultType
The return value type of the Continuation task.
Definition Continuation.h:51
const ExecutionContext & GetExecutionContext() const
Gets the ExecutionContext object.
std::function< void(void *)> CallbackType
The generic callback type.
Definition Continuation.h:55
bool Cancelled() const
Checks whether the CancellationContext instance is cancelled.
ContinuationImpl()=default
The default constructor of ContinuationImpl.
ContinuationImpl Then(ContinuationTask task)
Adds the next asynchronous task to the ContinuationImpl instance.
std::function< void(void *, CallbackType)> AsyncTaskType
An internal type of tasks in Continuation.
Definition Continuation.h:57
void Run(FinalCallbackType callback)
Starts the execution of the task continuation chain.
Rules all the other namespaces.
Definition AppleSignInProperties.h:24