Skip to content

Commit 6379075

Browse files
committed
Use std::optional if available.
Currently, SDK requires C++11 minimum. So, boost::optional type is used for optional values. For C++17 and above more convenient is to use std::optional instead. The task NLAM-23 is about making this type configurable. This commit is a second part of the task: olp-cpp-sdk-authentication. Relates-To: NLAM-23 Signed-off-by: sopov <[email protected]>
1 parent a387a38 commit 6379075

16 files changed

+76
-78
lines changed

olp-cpp-sdk-authentication/include/olp/authentication/AuthenticationClient.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include <olp/authentication/Types.h>
3737
#include <olp/core/client/ApiResponse.h>
3838
#include <olp/core/client/CancellationToken.h>
39-
#include <boost/optional.hpp>
39+
#include <olp/core/porting/optional.hpp>
4040

4141
/**
4242
* @brief Rules all the other namespaces.
@@ -65,15 +65,15 @@ class AUTHENTICATION_API AuthenticationClient {
6565
/**
6666
* @brief (Optional) The scope assigned to the access token.
6767
*/
68-
boost::optional<std::string> scope{boost::none};
68+
porting::optional<std::string> scope{porting::none};
6969

7070
/**
7171
* @brief (Optional) The device ID assigned to the access token.
7272
*
7373
* @note This field is only necessary if you want to apply a oauth rate
7474
* limit on a particular device.
7575
*/
76-
boost::optional<std::string> device_id{boost::none};
76+
porting::optional<std::string> device_id{porting::none};
7777

7878
/**
7979
* @brief (Optional) The number of seconds left before the access
@@ -89,7 +89,7 @@ class AUTHENTICATION_API AuthenticationClient {
8989
* requires it. Fully overrides default body and resets the request content
9090
* type.
9191
*/
92-
boost::optional<std::string> custom_body{boost::none};
92+
porting::optional<std::string> custom_body{olp::porting::none};
9393
};
9494

9595
/**

olp-cpp-sdk-authentication/include/olp/authentication/AuthenticationCredentials.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include <string>
2323

24-
#include <boost/optional/optional.hpp>
24+
#include <olp/core/porting/optional.hpp>
2525
#include "AuthenticationApi.h"
2626

2727
namespace olp {
@@ -58,7 +58,7 @@ class AUTHENTICATION_API AuthenticationCredentials {
5858
* @return An optional value with your credentials if the credentials were
5959
* read successfully, or no value in case of failure.
6060
*/
61-
static boost::optional<AuthenticationCredentials> ReadFromStream(
61+
static porting::optional<AuthenticationCredentials> ReadFromStream(
6262
std::istream& stream);
6363

6464
/**
@@ -81,7 +81,7 @@ class AUTHENTICATION_API AuthenticationCredentials {
8181
* @return The optional value with your credentials if the credentials were
8282
* read successfully, or no value in case of failure.
8383
*/
84-
static boost::optional<AuthenticationCredentials> ReadFromFile(
84+
static porting::optional<AuthenticationCredentials> ReadFromFile(
8585
std::string filename = {});
8686
AuthenticationCredentials() = delete;
8787

olp-cpp-sdk-authentication/include/olp/authentication/AuthenticationSettings.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include <olp/authentication/AuthenticationApi.h>
2626
#include <olp/core/client/RetrySettings.h>
2727
#include <olp/core/http/NetworkProxySettings.h>
28-
#include <boost/optional.hpp>
28+
#include <olp/core/porting/optional.hpp>
2929

3030
namespace olp {
3131
namespace http {
@@ -50,9 +50,9 @@ struct AUTHENTICATION_API AuthenticationSettings {
5050
/**
5151
* @brief The configuration settings for the network layer.
5252
*
53-
* To remove any existing proxy settings, set to boost::none.
53+
* To remove any existing proxy settings, set to olp::porting::none.
5454
*/
55-
boost::optional<http::NetworkProxySettings> network_proxy_settings{};
55+
porting::optional<http::NetworkProxySettings> network_proxy_settings{};
5656

5757
/**
5858
* @brief The network instance that is used to internally operate with the

olp-cpp-sdk-authentication/include/olp/authentication/AuthorizeRequest.h

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
#pragma once
2121

22-
#include <boost/optional.hpp>
2322
#include <string>
2423
#include <vector>
2524

2625
#include <olp/authentication/AuthenticationApi.h>
26+
#include <olp/core/porting/optional.hpp>
2727

2828
namespace olp {
2929
namespace authentication {
@@ -83,7 +83,7 @@ class AUTHENTICATION_API AuthorizeRequest final {
8383
*
8484
* @return The service ID.
8585
*/
86-
inline const std::string& GetServiceId() const { return service_id_; }
86+
const std::string& GetServiceId() const { return service_id_; }
8787

8888
/**
8989
* @brief Sets the service ID.
@@ -92,7 +92,7 @@ class AUTHENTICATION_API AuthorizeRequest final {
9292
*
9393
* @return A reference to the updated `DecisionRequest` instance.
9494
*/
95-
inline AuthorizeRequest& WithServiceId(std::string service_id) {
95+
AuthorizeRequest& WithServiceId(std::string service_id) {
9696
service_id_ = std::move(service_id);
9797
return *this;
9898
}
@@ -109,7 +109,7 @@ class AUTHENTICATION_API AuthorizeRequest final {
109109
*
110110
* @return The contract ID or an error.
111111
*/
112-
inline const boost::optional<std::string>& GetContractId() const {
112+
const porting::optional<std::string>& GetContractId() const {
113113
return contract_id_;
114114
}
115115

@@ -122,9 +122,9 @@ class AUTHENTICATION_API AuthorizeRequest final {
122122
*
123123
* @return A reference to the updated `DecisionRequest` instance.
124124
*/
125-
inline AuthorizeRequest& WithContractId(
126-
boost::optional<std::string> contract_id) {
127-
contract_id_ = std::move(contract_id);
125+
template <class T = porting::optional<std::string>>
126+
AuthorizeRequest& WithContractId(T&& contract_id) {
127+
contract_id_ = std::forward<T>(contract_id);
128128
return *this;
129129
}
130130

@@ -137,7 +137,7 @@ class AUTHENTICATION_API AuthorizeRequest final {
137137
*
138138
* @return A reference to the updated `DecisionRequest` instance.
139139
*/
140-
inline AuthorizeRequest& WithContractId(std::string contract_id) {
140+
AuthorizeRequest& WithContractId(std::string contract_id) {
141141
contract_id_ = std::move(contract_id);
142142
return *this;
143143
}
@@ -159,8 +159,7 @@ class AUTHENTICATION_API AuthorizeRequest final {
159159
*
160160
* @return A reference to the updated `DecisionRequest` instance.
161161
*/
162-
inline AuthorizeRequest& WithAction(std::string action,
163-
std::string resource = "") {
162+
AuthorizeRequest& WithAction(std::string action, std::string resource = "") {
164163
actions_.emplace_back(std::move(action), std::move(resource));
165164
return *this;
166165
}
@@ -174,7 +173,7 @@ class AUTHENTICATION_API AuthorizeRequest final {
174173
*
175174
* @return The operator type.
176175
*/
177-
inline DecisionOperatorType GetOperatorType() const { return operator_type_; }
176+
DecisionOperatorType GetOperatorType() const { return operator_type_; }
178177

179178
/**
180179
* @brief Sets the operator type for the request.
@@ -185,8 +184,7 @@ class AUTHENTICATION_API AuthorizeRequest final {
185184
*
186185
* @return A reference to the updated `DecisionRequest` instance.
187186
*/
188-
inline AuthorizeRequest& WithOperatorType(
189-
DecisionOperatorType operator_type) {
187+
AuthorizeRequest& WithOperatorType(DecisionOperatorType operator_type) {
190188
operator_type_ = operator_type;
191189
return *this;
192190
}
@@ -196,7 +194,7 @@ class AUTHENTICATION_API AuthorizeRequest final {
196194
*
197195
* @return The diagnostics flag.
198196
*/
199-
inline bool GetDiagnostics() const { return diagnostics_; }
197+
bool GetDiagnostics() const { return diagnostics_; }
200198

201199
/**
202200
* @brief Sets the diagnostics flag for the request.
@@ -206,7 +204,7 @@ class AUTHENTICATION_API AuthorizeRequest final {
206204
*
207205
* @return A reference to the updated `DecisionRequest` instance.
208206
*/
209-
inline AuthorizeRequest& WithDiagnostics(bool diagnostics) {
207+
AuthorizeRequest& WithDiagnostics(bool diagnostics) {
210208
diagnostics_ = diagnostics;
211209
return *this;
212210
}
@@ -220,7 +218,7 @@ class AUTHENTICATION_API AuthorizeRequest final {
220218

221219
private:
222220
std::string service_id_;
223-
boost::optional<std::string> contract_id_;
221+
porting::optional<std::string> contract_id_;
224222
Actions actions_;
225223
DecisionOperatorType operator_type_{DecisionOperatorType::kAnd};
226224
bool diagnostics_{false};

olp-cpp-sdk-authentication/include/olp/authentication/Settings.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
#pragma once
2121

22-
#include <boost/optional.hpp>
2322
#include <memory>
2423
#include <string>
2524

2625
#include <olp/core/client/RetrySettings.h>
2726
#include <olp/core/http/NetworkProxySettings.h>
27+
#include <olp/core/porting/optional.hpp>
2828

2929
#include "AuthenticationApi.h"
3030
#include "AuthenticationCredentials.h"
@@ -77,7 +77,7 @@ struct AUTHENTICATION_API Settings {
7777
/**
7878
* @brief (Optional) The configuration settings for the network layer.
7979
*/
80-
boost::optional<http::NetworkProxySettings> network_proxy_settings;
80+
porting::optional<http::NetworkProxySettings> network_proxy_settings;
8181

8282
/**
8383
* @brief (Optional) The server URL of the token endpoint.
@@ -108,7 +108,7 @@ struct AUTHENTICATION_API Settings {
108108
/**
109109
* @brief (Optional) The scope to be assigned to an access token requests.
110110
*/
111-
boost::optional<std::string> scope;
111+
porting::optional<std::string> scope;
112112
};
113113

114114
} // namespace authentication

olp-cpp-sdk-authentication/include/olp/authentication/TokenResult.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#pragma once
2121

22-
#include <boost/optional.hpp>
22+
#include <olp/core/porting/optional.hpp>
2323
#include <chrono>
2424
#include <ctime>
2525
#include <string>
@@ -47,7 +47,7 @@ class AUTHENTICATION_API TokenResult {
4747
* @param scope The scope assigned to the access token.
4848
*/
4949
TokenResult(std::string access_token, time_t expiry_time,
50-
boost::optional<std::string> scope);
50+
porting::optional<std::string> scope);
5151

5252
/**
5353
* @brief Creates the `TokenResult` instance.
@@ -57,7 +57,7 @@ class AUTHENTICATION_API TokenResult {
5757
* @param scope The scope assigned to the access token.
5858
*/
5959
TokenResult(std::string access_token, std::chrono::seconds expires_in,
60-
boost::optional<std::string> scope);
60+
porting::optional<std::string> scope);
6161
/**
6262
* @brief Creates the default `TokenResult` instance.
6363
*/
@@ -92,13 +92,13 @@ class AUTHENTICATION_API TokenResult {
9292
* @return The optional string that contains the scope assigned to the access
9393
* token.
9494
*/
95-
const boost::optional<std::string>& GetScope() const;
95+
const porting::optional<std::string>& GetScope() const;
9696

9797
private:
9898
std::string access_token_;
9999
time_t expiry_time_{};
100100
std::chrono::seconds expires_in_{};
101-
boost::optional<std::string> scope_;
101+
porting::optional<std::string> scope_;
102102
};
103103

104104
} // namespace authentication

olp-cpp-sdk-authentication/src/AuthenticationClientImpl.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -265,24 +265,24 @@ Response<SignInResponseType> AuthenticationClientImpl::GetSignInResponse(
265265
}
266266

267267
template <>
268-
boost::optional<SignInResult> AuthenticationClientImpl::FindInCache(
268+
porting::optional<SignInResult> AuthenticationClientImpl::FindInCache(
269269
const std::string& key) {
270270
return client_token_cache_->locked(
271271
[&](utils::LruCache<std::string, SignInResult>& cache) {
272272
auto it = cache.Find(key);
273-
return it != cache.end() ? boost::make_optional(it->value())
274-
: boost::none;
273+
return it != cache.end() ? porting::make_optional(it->value())
274+
: porting::none;
275275
});
276276
}
277277

278278
template <>
279-
boost::optional<SignInUserResult> AuthenticationClientImpl::FindInCache(
279+
porting::optional<SignInUserResult> AuthenticationClientImpl::FindInCache(
280280
const std::string& key) {
281281
return user_token_cache_->locked(
282282
[&](utils::LruCache<std::string, SignInUserResult>& cache) {
283283
auto it = cache.Find(key);
284-
return it != cache.end() ? boost::make_optional(it->value())
285-
: boost::none;
284+
return it != cache.end() ? porting::make_optional(it->value())
285+
: porting::none;
286286
});
287287
}
288288

@@ -325,7 +325,7 @@ client::CancellationToken AuthenticationClientImpl::SignInClient(
325325
// endpoint with it. Construction of the `OlpClient` requires the host part
326326
// of URL, while `CallAuth` method - the rest of URL, hence we need to split
327327
// URL passed in the Credentials object.
328-
const auto credentials_endpoint = credentials.GetEndpointUrl();
328+
const auto& credentials_endpoint = credentials.GetEndpointUrl();
329329
const auto maybe_host_and_rest =
330330
olp::utils::Url::ParseHostAndRest(credentials_endpoint);
331331
if (maybe_host_and_rest) {
@@ -338,7 +338,7 @@ client::CancellationToken AuthenticationClientImpl::SignInClient(
338338
// settings object.
339339
auto settings = settings_;
340340
settings.token_endpoint_url = olp_client_host;
341-
auto client = CreateOlpClient(settings, boost::none, false);
341+
auto client = CreateOlpClient(settings, porting::none, false);
342342

343343
RequestTimer timer = CreateRequestTimer(client, context);
344344

@@ -472,7 +472,7 @@ client::CancellationToken AuthenticationClientImpl::SignInApple(
472472
return client::ApiError::Cancelled();
473473
}
474474

475-
auto client = CreateOlpClient(settings_, boost::none);
475+
auto client = CreateOlpClient(settings_, porting::none);
476476

477477
auto auth_response = CallApi(client, kOauthEndpoint, context,
478478
properties.GetAccessToken(), request_body);
@@ -534,7 +534,7 @@ client::CancellationToken AuthenticationClientImpl::HandleUserRequest(
534534
return client::ApiError::Cancelled();
535535
}
536536

537-
auto client = CreateOlpClient(settings_, boost::none, false);
537+
auto client = CreateOlpClient(settings_, porting::none, false);
538538

539539
RequestTimer timer = CreateRequestTimer(client, context);
540540

@@ -834,12 +834,12 @@ client::OlpClient::RequestBodyType AuthenticationClientImpl::GenerateClientBody(
834834

835835
if (properties.scope) {
836836
writer.Key(kScope);
837-
writer.String(properties.scope.get().c_str());
837+
writer.String(properties.scope->c_str());
838838
}
839839

840840
if (properties.device_id) {
841841
writer.Key(kDeviceId);
842-
writer.String(properties.device_id.get().c_str());
842+
writer.String(properties.device_id->c_str());
843843
}
844844

845845
writer.EndObject();

olp-cpp-sdk-authentication/src/AuthenticationClientImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class AuthenticationClientImpl {
178178
const client::CancellationContext& context, const std::string& key);
179179

180180
template <typename SignInResponseType>
181-
boost::optional<SignInResponseType> FindInCache(const std::string& key);
181+
porting::optional<SignInResponseType> FindInCache(const std::string& key);
182182

183183
template <typename SignInResponseType>
184184
void StoreInCache(const std::string& key, SignInResponseType);

0 commit comments

Comments
 (0)