Skip to content

Commit 10b63ea

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. This PR makes the optional type used configurable. Fix clang builds. Relates-To: NLAM-23 Signed-off-by: sopov <ext-alexander.sopov@here.com>
1 parent e2e5a13 commit 10b63ea

4 files changed

Lines changed: 36 additions & 41 deletions

File tree

examples/ProtectedCacheExample.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,16 @@ bool HandleDataResponse(
6262
} // namespace
6363

6464
int RunExampleReadWithCache(const AccessKey& access_key,
65-
const olp::cache::CacheSettings& cache_settings, const std::string& catalog) {
65+
const olp::cache::CacheSettings& cache_settings,
66+
const std::string& catalog) {
6667
OLP_SDK_LOG_INFO_F(
6768
kLogTag, "Mutable cache path is \"%s\"",
68-
cache_settings.disk_path_mutable.get_value_or("none").c_str());
69+
olp::porting::get_value_or(cache_settings.disk_path_mutable, "none")
70+
.c_str());
6971
OLP_SDK_LOG_INFO_F(
7072
kLogTag, "Protected cache path is \"%s\"",
71-
cache_settings.disk_path_protected.get_value_or("none").c_str());
73+
olp::porting::get_value_or(cache_settings.disk_path_protected, "none")
74+
.c_str());
7275
// Create a task scheduler instance
7376
std::shared_ptr<olp::thread::TaskScheduler> task_scheduler =
7477
olp::client::OlpClientSettingsFactory::CreateDefaultTaskScheduler(1u);
@@ -84,8 +87,8 @@ int RunExampleReadWithCache(const AccessKey& access_key,
8487
olp::authentication::AuthenticationCredentials::ReadFromFile();
8588

8689
// Initialize authentication settings.
87-
olp::authentication::Settings settings{
88-
read_credentials_result.get_value_or({access_key.id, access_key.secret})};
90+
olp::authentication::Settings settings{olp::porting::get_value_or(
91+
read_credentials_result, {access_key.id, access_key.secret})};
8992
settings.task_scheduler = task_scheduler;
9093
settings.network_request_handler = http_client;
9194

@@ -108,23 +111,22 @@ int RunExampleReadWithCache(const AccessKey& access_key,
108111

109112
// Create appropriate layer client with HRN, layer name and settings.
110113
olp::dataservice::read::VersionedLayerClient layer_client(
111-
olp::client::HRN(catalog), first_layer_id, olp::porting::none, client_settings);
114+
olp::client::HRN(catalog), first_layer_id, olp::porting::none,
115+
client_settings);
112116

113117
// Retrieve the partition data
114118
// Create a DataRequest with appropriate LayerId and PartitionId
115-
auto request = olp::dataservice::read::DataRequest()
116-
.WithPartitionId(first_partition_id)
117-
.WithBillingTag(olp::porting::none);
118-
if (cache_settings.disk_path_protected.is_initialized()) {
119+
auto request =
120+
olp::dataservice::read::DataRequest().WithPartitionId(first_partition_id);
121+
if (cache_settings.disk_path_protected.has_value()) {
119122
request.WithFetchOption(olp::dataservice::read::FetchOptions::CacheOnly);
120123
}
121124

122125
// Run the DataRequest
123126
auto future = layer_client.GetData(request);
124127

125128
// Wait for DataResponse
126-
olp::dataservice::read::DataResponse data_response =
127-
future.GetFuture().get();
129+
olp::dataservice::read::DataResponse data_response = future.GetFuture().get();
128130

129131
// Compact mutable cache, so it can be used as protected cache
130132
cache->Compact();
@@ -133,8 +135,8 @@ int RunExampleReadWithCache(const AccessKey& access_key,
133135
return (HandleDataResponse(data_response) ? 0 : -1);
134136
}
135137

136-
int RunExampleProtectedCache(const AccessKey& access_key, const std::string& catalog)
137-
{
138+
int RunExampleProtectedCache(const AccessKey& access_key,
139+
const std::string& catalog) {
138140
// Read data with mutable cache.
139141
olp::cache::CacheSettings cache_settings;
140142
cache_settings.disk_path_mutable =

examples/ReadExample.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ int RunExampleRead(const AccessKey& access_key, const std::string& catalog,
135135
olp::authentication::AuthenticationCredentials::ReadFromFile();
136136

137137
// Initialize authentication settings.
138-
olp::authentication::Settings settings{
139-
read_credentials_result.get_value_or({access_key.id, access_key.secret})};
138+
olp::authentication::Settings settings{olp::porting::get_value_or(
139+
read_credentials_result, {access_key.id, access_key.secret})};
140140
settings.task_scheduler = task_scheduler;
141141
settings.network_request_handler = http_client;
142142

@@ -161,8 +161,8 @@ int RunExampleRead(const AccessKey& access_key, const std::string& catalog,
161161
olp::client::HRN(catalog), client_settings);
162162

163163
// Create CatalogRequest
164-
auto request =
165-
olp::dataservice::read::CatalogRequest().WithBillingTag(olp::porting::none);
164+
auto request = olp::dataservice::read::CatalogRequest().WithBillingTag(
165+
olp::porting::none);
166166

167167
// Run the CatalogRequest
168168
auto future = catalog_client.GetCatalog(request);
@@ -184,8 +184,8 @@ int RunExampleRead(const AccessKey& access_key, const std::string& catalog,
184184
if (!first_layer_id.empty()) {
185185
// Retrieve the partitions metadata
186186
// Create a PartitionsRequest with appropriate LayerId
187-
auto request =
188-
olp::dataservice::read::PartitionsRequest().WithBillingTag(olp::porting::none);
187+
auto request = olp::dataservice::read::PartitionsRequest().WithBillingTag(
188+
olp::porting::none);
189189

190190
// Run the PartitionsRequest
191191
auto future = layer_client.GetPartitions(request);
@@ -205,8 +205,7 @@ int RunExampleRead(const AccessKey& access_key, const std::string& catalog,
205205
// Retrieve the partition data
206206
// Create a DataRequest with appropriate LayerId and PartitionId
207207
auto request = olp::dataservice::read::DataRequest()
208-
.WithPartitionId(first_partition_id)
209-
.WithBillingTag(olp::porting::none);
208+
.WithPartitionId(first_partition_id);
210209

211210
// Run the DataRequest
212211
auto future = layer_client.GetData(request);

examples/StreamLayerReadExample.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ int GetDataFromMessages(dataread::StreamLayerClient& client,
6363
auto handle = message.GetMetaData().GetDataHandle();
6464
if (handle) {
6565
OLP_SDK_LOG_INFO_F(kLogTag, "Message data: handle - %s, size - %lu",
66-
handle.get().c_str(),
67-
message.GetMetaData().GetDataSize().get());
66+
handle->c_str(),
67+
*message.GetMetaData().GetDataSize());
6868
// use GetData(const model::Message& message) with message instance to
6969
// request actual data with data handle.
7070
auto message_future = client.GetData(message);
@@ -73,14 +73,14 @@ int GetDataFromMessages(dataread::StreamLayerClient& client,
7373
OLP_SDK_LOG_WARNING_F(kLogTag,
7474
"Failed to get data for data handle %s - HTTP "
7575
"Status: %d Message: %s",
76-
handle.get().c_str(),
76+
handle->c_str(),
7777
message_result.GetError().GetHttpStatusCode(),
7878
message_result.GetError().GetMessage().c_str());
7979
continue;
8080
}
8181
auto message_data = message_result.MoveResult();
8282
OLP_SDK_LOG_INFO_F(kLogTag, "GetData for %s successful: size - %lu",
83-
handle.get().c_str(), message_data->size());
83+
handle->c_str(), message_data->size());
8484
} else {
8585
// If data is less than 1 MB, the data content published directly in the
8686
// metadata and encoded in Base64.

olp-cpp-sdk-dataservice-read/include/olp/dataservice/read/DataRequest.h

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,9 @@ class DATASERVICE_READ_API DataRequest final {
6262
*
6363
* @return A reference to the updated `DataRequest` instance.
6464
*/
65-
template <class T>
66-
typename std::enable_if<
67-
std::is_same<T, boost::optional<std::string>>::value &&
68-
std::is_same<T, porting::optional<std::string>>::value,
69-
DataRequest&>::type
65+
template <class T = porting::optional<std::string>>
66+
typename std::enable_if<std::is_same<T, boost::optional<std::string>>::value,
67+
DataRequest&>::type
7068
WithPartitionId(porting::optional<std::string> partition_id) {
7169
partition_id_ = std::move(partition_id);
7270
return *this;
@@ -114,11 +112,9 @@ class DATASERVICE_READ_API DataRequest final {
114112
*
115113
* @return A reference to the updated `DataRequest` instance.
116114
*/
117-
template <class T>
118-
typename std::enable_if<
119-
std::is_same<T, boost::optional<std::string>>::value &&
120-
std::is_same<T, porting::optional<std::string>>::value,
121-
DataRequest&>::type
115+
template <class T = porting::optional<std::string>>
116+
typename std::enable_if<std::is_same<T, boost::optional<std::string>>::value,
117+
DataRequest&>::type
122118
WithDataHandle(porting::optional<std::string> data_handle) {
123119
data_handle_ = std::move(data_handle);
124120
return *this;
@@ -164,11 +160,9 @@ class DATASERVICE_READ_API DataRequest final {
164160
*
165161
* @return A reference to the updated `DataRequest` instance.
166162
*/
167-
template <class T>
168-
typename std::enable_if<
169-
std::is_same<T, boost::optional<std::string>>::value &&
170-
std::is_constructible<porting::optional<std::string>, T>::value,
171-
DataRequest&>::type
163+
template <class T = porting::optional<std::string>>
164+
typename std::enable_if<std::is_same<T, boost::optional<std::string>>::value,
165+
DataRequest&>::type
172166
WithBillingTag(porting::optional<std::string> tag) {
173167
billing_tag_ = std::move(tag);
174168
return *this;

0 commit comments

Comments
 (0)