Skip to content

Commit ccdd33b

Browse files
authored
Use std::optional if available. (#1630)
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 last part of the task: tests and examples. Relates-To: NLAM-23 Signed-off-by: sopov <[email protected]>
1 parent 0678e1e commit ccdd33b

34 files changed

+242
-235
lines changed

docs/dataservice-cache-example.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ You can get data from a [versioned layer](https://www.here.com/docs/bundle/data-
8989
olp::dataservice::read::VersionedLayerClient layer_client (
9090
client::HRN catalog,
9191
std::string layer_id,
92-
boost::optional<int64_t> catalog_version,
92+
porting::optional<int64_t> catalog_version,
9393
client::OlpClientSettings settings);
9494
```
9595
@@ -102,7 +102,7 @@ You can get data from a [versioned layer](https://www.here.com/docs/bundle/data-
102102
```cpp
103103
auto request = olp::dataservice::read::DataRequest()
104104
.WithPartitionId(first_partition_id)
105-
.WithBillingTag(boost::none)
105+
.WithBillingTag(olp::porting::none)
106106
.WithFetchOption(olp::dataservice::read::FetchOptions::OnlineIfNotFound);
107107
```
108108

docs/dataservice-read-catalog-example.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ You can request any data version from a [versioned layer](https://www.here.com/d
359359
olp::dataservice::read::VersionedLayerClient layer_client (
360360
client::HRN catalog,
361361
std::string layer_id,
362-
boost::optional<int64_t> catalog_version,
362+
porting::optional<int64_t> catalog_version,
363363
client::OlpClientSettings settings);
364364
```
365365

examples/ProtectedCacheExample.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@ 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::value_or(cache_settings.disk_path_mutable, "none").c_str());
6970
OLP_SDK_LOG_INFO_F(
7071
kLogTag, "Protected cache path is \"%s\"",
71-
cache_settings.disk_path_protected.get_value_or("none").c_str());
72+
olp::porting::value_or(cache_settings.disk_path_protected, "none")
73+
.c_str());
7274
// Create a task scheduler instance
7375
std::shared_ptr<olp::thread::TaskScheduler> task_scheduler =
7476
olp::client::OlpClientSettingsFactory::CreateDefaultTaskScheduler(1u);
@@ -84,8 +86,8 @@ int RunExampleReadWithCache(const AccessKey& access_key,
8486
olp::authentication::AuthenticationCredentials::ReadFromFile();
8587

8688
// Initialize authentication settings.
87-
olp::authentication::Settings settings{
88-
read_credentials_result.get_value_or({access_key.id, access_key.secret})};
89+
olp::authentication::Settings settings{olp::porting::value_or(
90+
read_credentials_result, {access_key.id, access_key.secret})};
8991
settings.task_scheduler = task_scheduler;
9092
settings.network_request_handler = http_client;
9193

@@ -108,23 +110,22 @@ int RunExampleReadWithCache(const AccessKey& access_key,
108110

109111
// Create appropriate layer client with HRN, layer name and settings.
110112
olp::dataservice::read::VersionedLayerClient layer_client(
111-
olp::client::HRN(catalog), first_layer_id, boost::none, client_settings);
113+
olp::client::HRN(catalog), first_layer_id, olp::porting::none,
114+
client_settings);
112115

113116
// Retrieve the partition data
114117
// Create a DataRequest with appropriate LayerId and PartitionId
115-
auto request = olp::dataservice::read::DataRequest()
116-
.WithPartitionId(first_partition_id)
117-
.WithBillingTag(boost::none);
118-
if (cache_settings.disk_path_protected.is_initialized()) {
118+
auto request =
119+
olp::dataservice::read::DataRequest().WithPartitionId(first_partition_id);
120+
if (cache_settings.disk_path_protected.has_value()) {
119121
request.WithFetchOption(olp::dataservice::read::FetchOptions::CacheOnly);
120122
}
121123

122124
// Run the DataRequest
123125
auto future = layer_client.GetData(request);
124126

125127
// Wait for DataResponse
126-
olp::dataservice::read::DataResponse data_response =
127-
future.GetFuture().get();
128+
olp::dataservice::read::DataResponse data_response = future.GetFuture().get();
128129

129130
// Compact mutable cache, so it can be used as protected cache
130131
cache->Compact();
@@ -133,8 +134,8 @@ int RunExampleReadWithCache(const AccessKey& access_key,
133134
return (HandleDataResponse(data_response) ? 0 : -1);
134135
}
135136

136-
int RunExampleProtectedCache(const AccessKey& access_key, const std::string& catalog)
137-
{
137+
int RunExampleProtectedCache(const AccessKey& access_key,
138+
const std::string& catalog) {
138139
// Read data with mutable cache.
139140
olp::cache::CacheSettings cache_settings;
140141
cache_settings.disk_path_mutable =
@@ -145,6 +146,6 @@ int RunExampleProtectedCache(const AccessKey& access_key, const std::string& cat
145146
}
146147
// Read data with protected cache. Set mutable cache to none.
147148
cache_settings.disk_path_protected = cache_settings.disk_path_mutable;
148-
cache_settings.disk_path_mutable = boost::none;
149+
cache_settings.disk_path_mutable = olp::porting::none;
149150
return RunExampleReadWithCache(access_key, cache_settings, catalog);
150151
}

examples/ReadExample.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ bool HandleDataResponse(
119119
} // namespace
120120

121121
int RunExampleRead(const AccessKey& access_key, const std::string& catalog,
122-
const boost::optional<int64_t>& catalog_version) {
122+
const olp::porting::optional<int64_t>& catalog_version) {
123123
// Create a task scheduler instance
124124
std::shared_ptr<olp::thread::TaskScheduler> task_scheduler =
125125
olp::client::OlpClientSettingsFactory::CreateDefaultTaskScheduler(1u);
@@ -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::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(boost::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(boost::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);
@@ -204,9 +204,8 @@ int RunExampleRead(const AccessKey& access_key, const std::string& catalog,
204204
if (!first_partition_id.empty()) {
205205
// Retrieve the partition data
206206
// Create a DataRequest with appropriate LayerId and PartitionId
207-
auto request = olp::dataservice::read::DataRequest()
208-
.WithPartitionId(first_partition_id)
209-
.WithBillingTag(boost::none);
207+
auto request = olp::dataservice::read::DataRequest().WithPartitionId(
208+
first_partition_id);
210209

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

examples/ReadExample.h

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

2222
#include "Examples.h"
2323

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

2626
/**
2727
* @brief Dataservice read example. Authenticate client using access key id and
@@ -33,6 +33,6 @@
3333
* @param catalog_version The desired version of the catalog.
3434
* @return result of publish data(0 - if succeed)
3535
*/
36-
int RunExampleRead(
37-
const AccessKey& access_key, const std::string& catalog,
38-
const boost::optional<int64_t>& catalog_version = boost::none);
36+
int RunExampleRead(const AccessKey& access_key, const std::string& catalog,
37+
const olp::porting::optional<int64_t>& catalog_version =
38+
olp::porting::none);

examples/StreamLayerReadExample.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ 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(), *message.GetMetaData().GetDataSize());
6867
// use GetData(const model::Message& message) with message instance to
6968
// request actual data with data handle.
7069
auto message_future = client.GetData(message);
@@ -73,14 +72,14 @@ int GetDataFromMessages(dataread::StreamLayerClient& client,
7372
OLP_SDK_LOG_WARNING_F(kLogTag,
7473
"Failed to get data for data handle %s - HTTP "
7574
"Status: %d Message: %s",
76-
handle.get().c_str(),
75+
handle->c_str(),
7776
message_result.GetError().GetHttpStatusCode(),
7877
message_result.GetError().GetMessage().c_str());
7978
continue;
8079
}
8180
auto message_data = message_result.MoveResult();
8281
OLP_SDK_LOG_INFO_F(kLogTag, "GetData for %s successful: size - %lu",
83-
handle.get().c_str(), message_data->size());
82+
handle->c_str(), message_data->size());
8483
} else {
8584
// If data is less than 1 MB, the data content published directly in the
8685
// metadata and encoded in Base64.

examples/main.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#include "Options.h"
2121
#include "ProtectedCacheExample.h"
2222
#include "ReadExample.h"
23-
#include "WriteExample.h"
2423
#include "StreamLayerReadExample.h"
24+
#include "WriteExample.h"
2525

2626
#include <stdio.h>
2727
#include <string.h>
@@ -68,7 +68,7 @@ int RequiredArgumentError(const tools::Option& arg) {
6868
}
6969
int ParseArguments(const int argc, char** argv, AccessKey& access_key,
7070
std::string& catalog,
71-
boost::optional<int64_t>& catalog_version,
71+
olp::porting::optional<int64_t>& catalog_version,
7272
std::string& layer_id,
7373
olp::dataservice::read::SubscribeRequest::SubscriptionMode&
7474
subscription_mode) {
@@ -128,7 +128,7 @@ int ParseArguments(const int argc, char** argv, AccessKey& access_key,
128128
if (ss.fail() || !ss.eof()) {
129129
std::cout << "invalid catalog version value -- '" << *it
130130
<< "', but int64 is expected." << std::endl;
131-
catalog_version = boost::none;
131+
catalog_version = olp::porting::none;
132132
}
133133
} else if (IsMatch(*it, tools::kLayerIdOption)) {
134134
if (++it == arguments.end()) {
@@ -168,7 +168,7 @@ int ParseArguments(const int argc, char** argv, AccessKey& access_key,
168168

169169
int RunExamples(const AccessKey& access_key, int examples_to_run,
170170
const std::string& catalog,
171-
const boost::optional<int64_t>& catalog_version,
171+
const olp::porting::optional<int64_t>& catalog_version,
172172
const std::string& layer_id,
173173
olp::dataservice::read::SubscribeRequest::SubscriptionMode
174174
subscription_mode) {
@@ -210,10 +210,10 @@ int RunExamples(const AccessKey& access_key, int examples_to_run,
210210
int main(int argc, char** argv) {
211211
AccessKey access_key{}; // You can specify your here.access.key.id
212212
// and here.access.key.secret
213-
std::string catalog; // the HRN of the catalog to which you to publish data
214-
std::string layer_id; // the of the layer inside the catalog to which you
215-
// want to publish data
216-
boost::optional<int64_t> catalog_version; // version of the catalog.
213+
std::string catalog; // the HRN of the catalog to which you to publish data
214+
std::string layer_id; // the of the layer inside the catalog to which you
215+
// want to publish data
216+
olp::porting::optional<int64_t> catalog_version; // version of the catalog.
217217
auto subscription_mode =
218218
olp::dataservice::read::SubscribeRequest::SubscriptionMode::
219219
kSerial; // subscription mode for read stream layer example

tests/common/PlatformUrlsGenerator.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
#include "PlatformUrlsGenerator.h"
2121

2222
#include <algorithm>
23+
#include <cassert>
2324
#include <utility>
2425

2526
#include <olp/core/utils/Url.h>
26-
#include <olp/dataservice/read/model/Partitions.h>
27-
#include <olp/dataservice/read/model/VersionResponse.h>
2827
#include "ApiDefaultResponses.h"
2928

3029
namespace {

tests/common/ReadDefaultResponses.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ void FillSubQuads(std::int32_t depth, std::vector<std::uint16_t>& sub_quads_) {
112112
sub_quads_.insert(sub_quads_.end(), layer_ids.begin(), layer_ids.end());
113113
}
114114

115-
mockserver::TileMetadata MakePartition(std::string data_handle,
116-
boost::optional<int32_t> version) {
115+
mockserver::TileMetadata MakePartition(
116+
std::string data_handle, olp::porting::optional<int32_t> version) {
117117
mockserver::TileMetadata partition;
118118
partition.data_handle = std::move(data_handle);
119119
partition.version = version;
@@ -173,17 +173,17 @@ std::string ReadDefaultResponses::GenerateQuadTreeResponse(
173173
}
174174

175175
QuadTreeBuilder::QuadTreeBuilder(olp::geo::TileKey root_tile,
176-
boost::optional<int32_t> version)
176+
olp::porting::optional<int32_t> version)
177177
: root_tile_(root_tile), base_version_(version) {}
178178

179-
QuadTreeBuilder& QuadTreeBuilder::WithParent(olp::geo::TileKey parent,
180-
std::string data_handle,
181-
boost::optional<int32_t> version) {
179+
QuadTreeBuilder& QuadTreeBuilder::WithParent(
180+
olp::geo::TileKey parent, std::string data_handle,
181+
olp::porting::optional<int32_t> version) {
182182
assert(root_tile_.IsChildOf(parent));
183183

184184
// Make sure to set version when the base_version is set
185-
if (version != boost::none) {
186-
assert(base_version_ != boost::none);
185+
if (version != olp::porting::none) {
186+
assert(base_version_ != olp::porting::none);
187187
} else if (base_version_) {
188188
version = base_version_;
189189
}
@@ -208,11 +208,11 @@ QuadTreeBuilder& QuadTreeBuilder::FillParents() {
208208

209209
QuadTreeBuilder& QuadTreeBuilder::WithSubQuad(
210210
olp::geo::TileKey tile, std::string datahandle,
211-
boost::optional<int32_t> version) {
211+
olp::porting::optional<int32_t> version) {
212212
assert(tile.IsChildOf(root_tile_) || tile == root_tile_);
213213
assert((tile.Level() - root_tile_.Level()) <= 4);
214-
if (version != boost::none) {
215-
assert(base_version_ != boost::none);
214+
if (version != olp::porting::none) {
215+
assert(base_version_ != olp::porting::none);
216216
}
217217

218218
auto origin = root_tile_.ChangedLevelTo(tile.Level());

tests/common/ReadDefaultResponses.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace mockserver {
3434

3535
struct TileMetadata {
3636
std::string data_handle;
37-
boost::optional<int32_t> version;
37+
olp::porting::optional<int32_t> version;
3838
std::string crc;
3939
std::string checksum;
4040
int32_t data_size{0};
@@ -87,23 +87,25 @@ class QuadTreeBuilder {
8787
public:
8888
// if version is set, the quad tree is considered to be a versioned type.
8989
explicit QuadTreeBuilder(olp::geo::TileKey root_tile,
90-
boost::optional<int32_t> base_version);
90+
olp::porting::optional<int32_t> base_version);
9191

92-
QuadTreeBuilder& WithParent(olp::geo::TileKey parent, std::string data_handle,
93-
boost::optional<int32_t> version = boost::none);
92+
QuadTreeBuilder& WithParent(
93+
olp::geo::TileKey parent, std::string data_handle,
94+
olp::porting::optional<int32_t> version = olp::porting::none);
9495
QuadTreeBuilder& FillParents();
9596

9697
// tile is represented as a normal tilekey
97-
QuadTreeBuilder& WithSubQuad(olp::geo::TileKey tile, std::string datahandle,
98-
boost::optional<int32_t> version = boost::none);
98+
QuadTreeBuilder& WithSubQuad(
99+
olp::geo::TileKey tile, std::string datahandle,
100+
olp::porting::optional<int32_t> version = olp::porting::none);
99101

100102
std::string BuildJson() const;
101103

102104
olp::geo::TileKey Root() const;
103105

104106
protected:
105107
olp::geo::TileKey root_tile_;
106-
boost::optional<int32_t> base_version_;
108+
olp::porting::optional<int32_t> base_version_;
107109

108110
std::map<std::uint64_t, TileMetadata> sub_quads_;
109111
std::map<std::uint64_t, TileMetadata> parent_quads_;

0 commit comments

Comments
 (0)