olp-cpp-sdk  1.19.0
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 
29 namespace olp {
30 namespace client {
31 
40 class 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:
85  friend struct CancellationContextHash;
86 
90  struct CancellationContextImpl {
95  mutable std::recursive_mutex mutex_;
102  CancellationToken sub_operation_cancel_token_{};
106  bool is_cancelled_{false};
107  };
108 
112  std::shared_ptr<CancellationContextImpl> impl_;
113 };
114 
118 struct CORE_API CancellationContextHash {
126  size_t operator()(const CancellationContext& context) const;
127 };
128 
129 } // namespace client
130 } // namespace olp
131 
132 #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.
Definition: CancellationContext.h:118