Skip to content

Commit a387a38

Browse files
authored
Use std::optional if available. (#1623)
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 first part of the task: olp-cpp-sdk-core. Relates-To: NLAM-23 Signed-off-by: sopov <[email protected]>
1 parent 7d33c0b commit a387a38

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+283
-183
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ endif()
3737
option(OLP_SDK_ENABLE_TESTING "Flag to enable/disable building unit and integration tests" ON)
3838
option(OLP_SDK_BUILD_DOC "Build SDK documentation" OFF)
3939
option(OLP_SDK_NO_EXCEPTION "Disable exception handling" OFF)
40+
option(OLP_SDK_FORCE_BOOST_OPTIONAL "Use boost::optional instead of std::optional with C++17 and above" OFF)
4041
option(OLP_SDK_BOOST_THROW_EXCEPTION_EXTERNAL "The boost::throw_exception() is defined externally" OFF)
4142
option(OLP_SDK_BUILD_EXTERNAL_DEPS "Download and build external dependencies" ON)
4243
option(OLP_SDK_BUILD_EXAMPLES "Enable examples targets" OFF)

CONTRIBUTING.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,13 @@ inline PublishDataRequest& WithLayerId(std::string&& layer_id) {
257257

258258
#### API optional parameters
259259

260-
The SDK uses `boost::optional` for optional members, parameters, or return types. Also, function comments should indicate whether the parameter is optional or required.
260+
The SDK uses `porting::optional` mapped to either `boost::optional` or `std::optional` for optional members,
261+
parameters, or return types. Also, function comments should indicate whether the parameter is optional or required.
261262

262263
Example:
263264

264265
```cpp
265-
inline const boost::optional<std::string>& GetBillingTag() const {
266+
inline const porting::optional<std::string>& GetBillingTag() const {
266267
return billing_tag_;
267268
}
268269

docs/get-started.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ cmake --build . --target docs
130130
| `OLP_SDK_ENABLE_TESTING` | Defaults to `ON`. If enabled, unit tests are built for each library. |
131131
| `OLP_SDK_BUILD_EXTERNAL_DEPS` | Defaults to `ON`. If enabled, CMake downloads and compiles dependencies. |
132132
| `OLP_SDK_NO_EXCEPTION` | Defaults to `OFF`. If enabled, all libraries are built without exceptions. |
133+
| `OLP_SDK_FORCE_BOOST_OPTIONAL` | Defaults to `OFF`. If enabled, all libraries are built with boost::optional type even when std::optional is available. |
133134
| `OLP_SDK_BOOST_THROW_EXCEPTION_EXTERNAL` | Defaults to `OFF`. When `OLP_SDK_NO_EXCEPTION` is `ON`, `boost` requires `boost::throw_exception()` to be defined. If enabled, the external definition of `boost::throw_exception()` is used. Otherwise, the library uses own definition. |
134135
| `OLP_SDK_MSVC_PARALLEL_BUILD_ENABLE` (Windows Only) | Defaults to `ON`. If enabled, the `/MP` compilation flag is added to build the Data SDK using multiple cores. |
135136
| `OLP_SDK_DISABLE_DEBUG_LOGGING`| Defaults to `OFF`. If enabled, the debug and trace level log messages are not printed. |

external/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ if(OLP_SDK_ENABLE_DEFAULT_CACHE_LMDB)
9797
endif()
9898
endif()
9999

100-
find_package(Boost QUIET)
100+
find_package(Boost 1.82.0 QUIET)
101101
if(NOT TARGET Boost AND NOT Boost_FOUND)
102102
add_subdirectory(boost)
103103
set(BOOST_ROOT ${EXTERNAL_BOOST_ROOT} PARENT_SCOPE)

olp-cpp-sdk-core/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ set(OLP_SDK_PORTING_HEADERS
131131
./include/olp/core/porting/deprecated.h
132132
./include/olp/core/porting/export.h
133133
./include/olp/core/porting/make_unique.h
134+
./include/olp/core/porting/optional.hpp
134135
./include/olp/core/porting/platform.h
135136
./include/olp/core/porting/shared_mutex.h
136137
./include/olp/core/porting/try_emplace.h
@@ -439,6 +440,11 @@ if (OLP_SDK_NO_EXCEPTION AND NOT OLP_SDK_BOOST_THROW_EXCEPTION_EXTERNAL)
439440
PRIVATE OLP_SDK_BOOST_THROW_EXCEPTION=1)
440441
endif()
441442

443+
if (OLP_SDK_FORCE_BOOST_OPTIONAL)
444+
target_compile_definitions(${PROJECT_NAME}
445+
PUBLIC OLP_CPP_SDK_USE_BOOST_OPTIONAL)
446+
endif()
447+
442448
target_include_directories(${PROJECT_NAME} PUBLIC
443449
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
444450
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>

olp-cpp-sdk-core/include/olp/core/cache/CacheSettings.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424

2525
#include <olp/core/Config.h>
2626
#include <olp/core/CoreApi.h>
27-
28-
#include <olp/core/porting/deprecated.h>
29-
#include <boost/optional.hpp>
27+
#include <olp/core/porting/optional.hpp>
3028

3129
namespace olp {
3230
namespace cache {
@@ -72,7 +70,7 @@ struct CORE_API CacheSettings {
7270
* If this parameter is not set, the downloaded data is stored
7371
* only in the memory cache that is limited by `#max_memory_cache_size`.
7472
*/
75-
boost::optional<std::string> disk_path_mutable = boost::none;
73+
porting::optional<std::string> disk_path_mutable = porting::none;
7674

7775
/**
7876
* @brief Sets the upper limit (in bytes) of the disk space that is used for
@@ -161,7 +159,7 @@ struct CORE_API CacheSettings {
161159
* have a stable fallback state or offline data that you can always access
162160
* regardless of the network state.
163161
*/
164-
boost::optional<std::string> disk_path_protected = boost::none;
162+
porting::optional<std::string> disk_path_protected = porting::none;
165163

166164
/**
167165
* @brief The extend permissions flag (applicable for Unix systems).

olp-cpp-sdk-core/include/olp/core/cache/KeyGenerator.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
#include <string>
2323

2424
#include <olp/core/CoreApi.h>
25-
#include <olp/core/client/HRN.h>
2625
#include <olp/core/geo/tiling/TileKey.h>
27-
#include <boost/optional.hpp>
26+
#include <olp/core/porting/optional.hpp>
2827

2928
namespace olp {
3029
namespace cache {
@@ -77,7 +76,7 @@ class CORE_API KeyGenerator {
7776
*/
7877
static std::string CreatePartitionKey(
7978
const std::string& hrn, const std::string& layer_id,
80-
const std::string& partition_id, const boost::optional<int64_t>& version);
79+
const std::string& partition_id, const porting::optional<int64_t>& version);
8180

8281
/**
8382
* @brief Generates cache key for storing list of partitions.
@@ -90,7 +89,7 @@ class CORE_API KeyGenerator {
9089
*/
9190
static std::string CreatePartitionsKey(
9291
const std::string& hrn, const std::string& layer_id,
93-
const boost::optional<int64_t>& version);
92+
const porting::optional<int64_t>& version);
9493

9594
/**
9695
* @brief Generates cache key for storing list of available layer versions.
@@ -117,7 +116,7 @@ class CORE_API KeyGenerator {
117116
static std::string CreateQuadTreeKey(const std::string& hrn,
118117
const std::string& layer_id,
119118
olp::geo::TileKey root,
120-
const boost::optional<int64_t>& version,
119+
const porting::optional<int64_t>& version,
121120
int32_t depth);
122121

123122
/**

olp-cpp-sdk-core/include/olp/core/client/OlpClientSettings.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,14 @@
2424
#include <memory>
2525
#include <string>
2626

27-
#include <boost/optional.hpp>
28-
2927
#include <olp/core/client/CancellationContext.h>
30-
#include <olp/core/client/CancellationToken.h>
3128
#include <olp/core/client/DefaultLookupEndpointProvider.h>
3229
#include <olp/core/client/HRN.h>
3330
#include <olp/core/client/HttpResponse.h>
3431
#include <olp/core/client/OauthToken.h>
3532
#include <olp/core/client/RetrySettings.h>
3633
#include <olp/core/http/Network.h>
34+
#include <olp/core/porting/optional.hpp>
3735

3836
namespace olp {
3937
namespace cache {
@@ -191,16 +189,18 @@ struct CORE_API OlpClientSettings {
191189
/**
192190
* @brief The network proxy settings.
193191
*
194-
* To remove any existing proxy settings, set to `boost::none`.
192+
* To remove any existing proxy settings, set to `olp::porting::none`.
195193
*/
196-
boost::optional<http::NetworkProxySettings> proxy_settings = boost::none;
194+
porting::optional<http::NetworkProxySettings> proxy_settings = porting::none;
197195

198196
/**
199197
* @brief The authentication settings.
200198
*
201-
* To remove any existing authentication settings, set to `boost::none`.
199+
* To remove any existing authentication settings, set to
200+
* `olp::porting::none`.
202201
*/
203-
boost::optional<AuthenticationSettings> authentication_settings = boost::none;
202+
porting::optional<AuthenticationSettings> authentication_settings =
203+
porting::none;
204204

205205
/**
206206
* @brief The `TaskScheduler` instance.

olp-cpp-sdk-core/include/olp/core/client/OlpClientSettingsFactory.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
#include <memory>
2323
#include <string>
2424

25-
#include <boost/optional.hpp>
26-
2725
#include "olp/core/CoreApi.h"
2826
#include "olp/core/http/Network.h"
2927
#include "olp/core/http/NetworkInitializationSettings.h"

olp-cpp-sdk-core/include/olp/core/generated/parser/ParserWrapper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
#include <string>
2525
#include <vector>
2626

27+
#include <olp/core/porting/optional.hpp>
2728
#include <rapidjson/rapidjson.h>
28-
#include <boost/optional.hpp>
2929

3030
namespace olp {
3131
namespace parser {
@@ -57,7 +57,7 @@ inline void from_json(const rapidjson::Value& value,
5757
}
5858

5959
template <typename T>
60-
inline void from_json(const rapidjson::Value& value, boost::optional<T>& x) {
60+
inline void from_json(const rapidjson::Value& value, porting::optional<T>& x) {
6161
T result = T();
6262
from_json(value, result);
6363
x = result;

0 commit comments

Comments
 (0)