olp-cpp-sdk  1.21.0
ApiResponse.h
1 /*
2  * Copyright (C) 2019-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 <future>
23 #include <memory>
24 #include <string>
25 #include <type_traits>
26 #include <utility>
27 
28 #include "CancellationToken.h"
29 
30 namespace olp {
31 namespace client {
32 
35 template <typename Payload>
37  public:
38  ResponseExtension() = default;
39 
40  explicit ResponseExtension(Payload payload) : payload_(std::move(payload)) {}
41 
42  const Payload& GetPayload() const { return payload_; }
43 
44  protected:
45  Payload payload_{};
46 };
47 
49 template <>
50 class ResponseExtension<void> {
51  public:
52  ResponseExtension() = default;
53 };
54 
64 template <typename Result, typename Error, typename Payload = void>
65 class ApiResponse : public ResponseExtension<Payload> {
66  public:
68  using ResultType = Result;
69 
71  using ErrorType = Error;
72 
74  using PayloadType = Payload;
75 
76  ApiResponse() = default;
77 
89  template <
90  typename P = Payload, typename R = Result,
91  typename = typename std::enable_if<!std::is_same<P, void>::value>::type,
92  typename =
93  typename std::enable_if<std::is_copy_constructible<R>::value>::type>
94  ApiResponse(const ApiResponse<R, Error, void>& other) // NOLINT
95  : ResponseExtension<P>(),
96  result_(other.GetResult()),
97  error_(other.GetError()),
98  success_(other.IsSuccessful()) {}
99 
111  template <
112  typename P = Payload, typename R = Result,
113  typename = typename std::enable_if<!std::is_same<P, void>::value>::type,
114  typename =
115  typename std::enable_if<std::is_move_constructible<R>::value>::type>
117  : ResponseExtension<P>(),
118  result_(other.MoveResult()),
119  error_(other.GetError()),
120  success_(other.IsSuccessful()) {}
121 
134  template <typename P = Payload, typename = typename std::enable_if<
135  !std::is_same<P, void>::value>::type>
137  : ResponseExtension<P>(std::move(payload)),
138  result_(other.GetResult()),
139  error_(other.GetError()),
140  success_(other.IsSuccessful()) {}
141 
153  template <
154  typename U, typename P = Payload, typename R = Result,
155  typename = typename std::enable_if<std::is_same<P, void>::value>::type,
156  typename =
157  typename std::enable_if<std::is_copy_constructible<R>::value>::type>
159  : ResponseExtension<P>(),
160  result_(other.GetResult()),
161  error_(other.GetError()),
162  success_(other.IsSuccessful()) {}
163 
175  template <
176  typename U, typename P = Payload, typename R = Result,
177  typename = typename std::enable_if<std::is_same<P, void>::value>::type,
178  typename =
179  typename std::enable_if<std::is_move_constructible<R>::value>::type>
181  : ResponseExtension<P>(),
182  result_(other.MoveResult()),
183  error_(other.GetError()),
184  success_(other.IsSuccessful()) {}
185 
193  ApiResponse(ResultType result) // NOLINT
194  : ResponseExtension<Payload>(),
195  result_(std::move(result)),
196  success_(true) {}
197 
206  template <typename P = Payload, typename = typename std::enable_if<
207  !std::is_same<P, void>::value>::type>
208  ApiResponse(ResultType result, P payload)
209  : ResponseExtension<Payload>(std::move(payload)),
210  result_(std::move(result)),
211  success_(true) {}
212 
218  ApiResponse(const ErrorType& error) // NOLINT
219  : error_(error), success_(false) {}
220 
226  template <typename P = Payload, typename = typename std::enable_if<
227  !std::is_same<P, void>::value>::type>
228  ApiResponse(const ErrorType& error, P payload)
229  : ResponseExtension<Payload>(std::move(payload)),
230  error_(error),
231  success_(false) {}
232 
238  inline bool IsSuccessful() const { return success_; }
239 
245  inline const ResultType& GetResult() const { return result_; }
246 
252  inline ResultType&& MoveResult() { return std::move(result_); }
253 
259  inline const ErrorType& GetError() const { return error_; }
260 
266  inline explicit operator bool() const { return IsSuccessful(); }
267 
268  private:
269  ResultType result_{};
270  ErrorType error_{};
271  bool success_{false};
272 };
273 
280 template <typename T>
282  public:
286  using PromisePtr = std::shared_ptr<std::promise<T>>;
287 
295  CancellableFuture(const CancellationToken& cancel_token, PromisePtr promise)
296  : cancel_token_(cancel_token), promise_(std::move(promise)) {}
297 
304  inline const CancellationToken& GetCancellationToken() const {
305  return cancel_token_;
306  }
307 
314  inline std::future<T> GetFuture() const { return promise_->get_future(); }
315 
316  private:
317  CancellationToken cancel_token_;
318  PromisePtr promise_;
319 };
320 
321 } // namespace client
322 } // namespace olp
Represents a request outcome.
Definition: ApiResponse.h:65
const ResultType & GetResult() const
Gets the result of the successfully executed request.
Definition: ApiResponse.h:245
ApiResponse(ApiResponse< R, Error, void > &&other)
Creates the ApiResponse instance from a similar response type without payload. The payload is default...
Definition: ApiResponse.h:116
ApiResponse(const ErrorType &error, P payload)
Creates the ApiResponse instance if the request is not successful.
Definition: ApiResponse.h:228
ResultType && MoveResult()
Moves the result of the successfully executed request.
Definition: ApiResponse.h:252
Payload PayloadType
The type of additional payload.
Definition: ApiResponse.h:74
ApiResponse(ResultType result)
Creates the ApiResponse instance.
Definition: ApiResponse.h:193
ApiResponse(const ApiResponse< Result, Error, void > &other, P payload)
Creates the ApiResponse instance from a similar response type without payload, and a separate payload...
Definition: ApiResponse.h:136
bool IsSuccessful() const
Checks the status of the request attempt.
Definition: ApiResponse.h:238
Result ResultType
The type of result.
Definition: ApiResponse.h:68
Error ErrorType
The type of error.
Definition: ApiResponse.h:71
ApiResponse(ResultType result, P payload)
Creates the ApiResponse instance with payload.
Definition: ApiResponse.h:208
ApiResponse(const ErrorType &error)
Creates the ApiResponse instance if the request is not successful.
Definition: ApiResponse.h:218
const ErrorType & GetError() const
Gets the error of the unsuccessful request attempt.
Definition: ApiResponse.h:259
ApiResponse(const ApiResponse< R, Error, void > &other)
Creates the ApiResponse instance from a similar response type without payload. The payload is default...
Definition: ApiResponse.h:94
ApiResponse(ApiResponse< R, Error, U > &&other)
Creates the ApiResponse instance from a similar request with a payload.
Definition: ApiResponse.h:180
ApiResponse(const ApiResponse< R, Error, U > &other)
Creates the ApiResponse instance from a similar request with a payload.
Definition: ApiResponse.h:158
A wrapper template that you can use to cancel a request or wait for it to finalize.
Definition: ApiResponse.h:281
const CancellationToken & GetCancellationToken() const
Gets the CancellationToken reference used to cancel the ongoing operation.
Definition: ApiResponse.h:304
CancellableFuture(const CancellationToken &cancel_token, PromisePtr promise)
Creates the CancellableFuture instance with CancellationToken and std::promise.
Definition: ApiResponse.h:295
std::shared_ptr< std::promise< T > > PromisePtr
The sharable promise type.
Definition: ApiResponse.h:286
std::future< T > GetFuture() const
Gets the future associated with the std::promise that you specified during initialization.
Definition: ApiResponse.h:314
Cancels service requests.
Definition: CancellationToken.h:33
Definition: ApiResponse.h:36
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24