olp-cpp-sdk 1.24.0
Loading...
Searching...
No Matches
CancellationContext.h
1/*
2 * Copyright (C) 2019-2021 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 <functional>
23#include <memory>
24#include <mutex>
25
26#include <olp/core/CoreApi.h>
27#include <olp/core/client/CancellationToken.h>
28
29namespace olp {
30namespace client {
31
40class CORE_API CancellationContext {
41 public:
45 using ExecuteFuncType = std::function<CancellationToken()>;
49 using CancelFuncType = std::function<void()>;
50
52
68 bool ExecuteOrCancelled(const ExecuteFuncType& execute_fn,
69 const CancelFuncType& cancel_fn = nullptr);
70
74 void CancelOperation();
75
81 bool IsCancelled() const;
82
83 private:
86 friend struct CancellationContextEquality;
87
91 struct CancellationContextImpl {
96 mutable std::recursive_mutex mutex_;
103 CancellationToken sub_operation_cancel_token_{};
107 bool is_cancelled_{false};
108 };
109
113 std::shared_ptr<CancellationContextImpl> impl_;
114};
115
119struct CORE_API CancellationContextHash {
127 size_t operator()(const CancellationContext& context) const;
128};
129
142 bool operator()(const CancellationContext& lhs,
143 const CancellationContext& rhs) const;
144};
145
146} // namespace client
147} // namespace olp
148
149#include "CancellationContext.inl"
A wrapper that manages the cancellation state of an asynchronous operation in a thread-safe way.
Definition CancellationContext.h:40
std::function< void()> CancelFuncType
An alias for the cancellation function.
Definition CancellationContext.h:49
std::function< CancellationToken()> ExecuteFuncType
An alias for the operation function.
Definition CancellationContext.h:45
Cancels service requests.
Definition CancellationToken.h:33
Rules all the other namespaces.
Definition AppleSignInProperties.h:24
A helper for unordered containers for equality comparison.
Definition CancellationContext.h:133
A helper for unordered containers.
Definition CancellationContext.h:119