Skip to content

Commit a6e85ca

Browse files
authored
Create any wrapper to use either boost or std any (#1644)
Add olp::porting::any wrapper which should use std::any for C++17 or boost::any for C++14 and older. Relates-To: NLAM-157 Signed-off-by: Khomenko Denys <[email protected]>
1 parent 403f8c5 commit a6e85ca

File tree

25 files changed

+454
-107
lines changed

25 files changed

+454
-107
lines changed

CMakeLists.txt

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

docs/get-started.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ cmake --build . --target docs
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. |
133133
| `OLP_SDK_USE_STD_OPTIONAL` | Defaults to `OFF`. If enabled, all libraries are built with std::optional type instead of boost::optional for C++17 and above. |
134+
| `OLP_SDK_USE_STD_ANY` | Defaults to `OFF`. If enabled, all libraries are built with std::any type instead of boost::any for C++17 and above. |
134135
| `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. |
135136
| `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. |
136137
| `OLP_SDK_DISABLE_DEBUG_LOGGING`| Defaults to `OFF`. If enabled, the debug and trace level log messages are not printed. |

olp-cpp-sdk-core/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ set(OLP_SDK_PLATFORM_HEADERS
128128
)
129129

130130
set(OLP_SDK_PORTING_HEADERS
131+
./include/olp/core/porting/any.h
131132
./include/olp/core/porting/deprecated.h
132133
./include/olp/core/porting/export.h
133134
./include/olp/core/porting/make_unique.h
@@ -450,6 +451,11 @@ if (OLP_SDK_USE_STD_OPTIONAL)
450451
PUBLIC OLP_CPP_SDK_USE_STD_OPTIONAL)
451452
endif()
452453

454+
if (OLP_SDK_USE_STD_ANY)
455+
target_compile_definitions(${PROJECT_NAME}
456+
PUBLIC OLP_SDK_USE_STD_ANY)
457+
endif()
458+
453459
target_include_directories(${PROJECT_NAME} PUBLIC
454460
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
455461
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class CORE_API DefaultCache : public KeyValueCache {
158158
*
159159
* @return True if the operation is successful; false otherwise.
160160
*/
161-
bool Put(const std::string& key, const boost::any& value,
161+
bool Put(const std::string& key, const olp::porting::any& value,
162162
const Encoder& encoder, time_t expiry) override;
163163

164164
/**
@@ -181,7 +181,8 @@ class CORE_API DefaultCache : public KeyValueCache {
181181
*
182182
* @return The key-value pair.
183183
*/
184-
boost::any Get(const std::string& key, const Decoder& decoder) override;
184+
olp::porting::any Get(const std::string& key,
185+
const Decoder& decoder) override;
185186

186187
/**
187188
* @brief Gets the key and binary data from the cache.

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
#include <olp/core/client/ApiError.h>
3131
#include <olp/core/client/ApiNoResult.h>
3232
#include <olp/core/client/ApiResponse.h>
33+
#include <olp/core/porting/any.h>
3334
#include <olp/core/utils/WarningWorkarounds.h>
34-
#include <boost/any.hpp>
3535

3636
namespace olp {
3737
namespace cache {
3838

3939
using Encoder = std::function<std::string()>;
40-
using Decoder = std::function<boost::any(const std::string&)>;
40+
using Decoder = std::function<olp::porting::any(const std::string&)>;
4141

4242
template <typename Result>
4343
using OperationOutcome = client::ApiResponse<Result, client::ApiError>;
@@ -74,7 +74,7 @@ class CORE_API KeyValueCache {
7474
*
7575
* @return True if the operation is successful; false otherwise.
7676
*/
77-
virtual bool Put(const std::string& key, const boost::any& value,
77+
virtual bool Put(const std::string& key, const olp::porting::any& value,
7878
const Encoder& encoder, time_t expiry = kDefaultExpiry) = 0;
7979

8080
/**
@@ -97,7 +97,8 @@ class CORE_API KeyValueCache {
9797
*
9898
* @return The key-value pair.
9999
*/
100-
virtual boost::any Get(const std::string& key, const Decoder& encoder) = 0;
100+
virtual olp::porting::any Get(const std::string& key,
101+
const Decoder& encoder) = 0;
101102

102103
/**
103104
* @brief Gets the key and binary data from the cache.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (C) 2025 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+
#if (__cplusplus >= 201703L) && defined(OLP_SDK_USE_STD_ANY)
23+
#include <any>
24+
25+
namespace olp {
26+
namespace porting {
27+
28+
using any = std::any;
29+
30+
template <typename T>
31+
inline T any_cast(const any& operand) {
32+
return std::any_cast<T>(operand);
33+
}
34+
35+
template <typename T>
36+
inline T any_cast(any& operand) {
37+
return std::any_cast<T>(operand);
38+
}
39+
40+
template <typename T>
41+
inline T any_cast(any&& operand) {
42+
return std::any_cast<T>(std::move(operand));
43+
}
44+
45+
template <typename T>
46+
inline const T* any_cast(const any* operand) noexcept {
47+
return std::any_cast<T>(operand);
48+
}
49+
50+
template <typename T>
51+
inline T* any_cast(any* operand) noexcept {
52+
return std::any_cast<T>(operand);
53+
}
54+
55+
inline bool has_value(const any& operand) noexcept {
56+
return operand.has_value();
57+
}
58+
59+
inline void reset(any& operand) noexcept { operand.reset(); }
60+
61+
template <typename T, typename... Args>
62+
inline any make_any(Args&&... args) {
63+
return std::make_any<T>(std::forward<Args>(args)...);
64+
}
65+
66+
template <typename T, typename U, typename... Args>
67+
inline any make_any(std::initializer_list<U> il, Args&&... args) {
68+
return std::make_any<T>(il, std::forward<Args>(args)...);
69+
}
70+
71+
} // namespace porting
72+
} // namespace olp
73+
74+
#else
75+
#include <boost/any.hpp>
76+
77+
namespace olp {
78+
namespace porting {
79+
80+
using any = boost::any;
81+
82+
template <typename T>
83+
inline T any_cast(const any& operand) {
84+
return boost::any_cast<T>(operand);
85+
}
86+
87+
template <typename T>
88+
inline T any_cast(any& operand) {
89+
return boost::any_cast<T>(operand);
90+
}
91+
92+
template <typename T>
93+
inline T any_cast(any&& operand) {
94+
return boost::any_cast<T>(operand);
95+
}
96+
97+
template <typename T>
98+
inline const T* any_cast(const any* operand) noexcept {
99+
return boost::any_cast<T>(operand);
100+
}
101+
102+
template <typename T>
103+
inline T* any_cast(any* operand) noexcept {
104+
return boost::any_cast<T>(operand);
105+
}
106+
107+
inline bool has_value(const any& operand) noexcept { return !operand.empty(); }
108+
109+
inline void reset(any& operand) noexcept { operand = boost::any(); }
110+
111+
template <typename T, typename... Args>
112+
inline any make_any(Args&&... args) {
113+
return any(T(std::forward<Args>(args)...));
114+
}
115+
116+
template <typename T, typename U, typename... Args>
117+
inline any make_any(std::initializer_list<U> il, Args&&... args) {
118+
return any(T(il, std::forward<Args>(args)...));
119+
}
120+
121+
} // namespace porting
122+
} // namespace olp
123+
124+
#endif

olp-cpp-sdk-core/src/cache/DefaultCache.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ bool DefaultCache::Clear() { return impl_->Clear(); }
4242

4343
void DefaultCache::Compact() { return impl_->Compact(); }
4444

45-
bool DefaultCache::Put(const std::string& key, const boost::any& value,
45+
bool DefaultCache::Put(const std::string& key, const olp::porting::any& value,
4646
const Encoder& encoder, time_t expiry) {
4747
return impl_->Put(key, value, encoder, expiry);
4848
}
@@ -52,7 +52,8 @@ bool DefaultCache::Put(const std::string& key,
5252
return impl_->Put(key, value, expiry);
5353
}
5454

55-
boost::any DefaultCache::Get(const std::string& key, const Decoder& decoder) {
55+
olp::porting::any DefaultCache::Get(const std::string& key,
56+
const Decoder& decoder) {
5657
return impl_->Get(key, decoder);
5758
}
5859

olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ void DefaultCacheImpl::Compact() {
273273
}
274274
}
275275

276-
bool DefaultCacheImpl::Put(const std::string& key, const boost::any& value,
276+
bool DefaultCacheImpl::Put(const std::string& key,
277+
const olp::porting::any& value,
277278
const Encoder& encoder, time_t expiry) {
278279
std::lock_guard<std::mutex> lock(cache_lock_);
279280
if (!is_open_) {
@@ -301,11 +302,11 @@ bool DefaultCacheImpl::Put(const std::string& key,
301302
return Write(key, value, expiry).IsSuccessful();
302303
}
303304

304-
boost::any DefaultCacheImpl::Get(const std::string& key,
305-
const Decoder& decoder) {
305+
olp::porting::any DefaultCacheImpl::Get(const std::string& key,
306+
const Decoder& decoder) {
306307
std::lock_guard<std::mutex> lock(cache_lock_);
307308
if (!is_open_) {
308-
return boost::any();
309+
return olp::porting::any();
309310
}
310311

311312
if (memory_cache_) {
@@ -329,7 +330,7 @@ boost::any DefaultCacheImpl::Get(const std::string& key,
329330
return decoded_item;
330331
}
331332

332-
return boost::any();
333+
return olp::porting::any();
333334
}
334335

335336
KeyValueCache::ValueTypePtr DefaultCacheImpl::Get(const std::string& key) {
@@ -784,10 +785,9 @@ DefaultCache::StorageOpenResult DefaultCacheImpl::SetupProtectedCache() {
784785
settings_.disk_path_protected->c_str());
785786

786787
open_mode = static_cast<OpenOptions>(open_mode | OpenOptions::ReadOnly);
787-
status =
788-
protected_cache_->Open(*settings_.disk_path_protected,
789-
*settings_.disk_path_protected,
790-
protected_storage_settings, open_mode, false);
788+
status = protected_cache_->Open(
789+
*settings_.disk_path_protected, *settings_.disk_path_protected,
790+
protected_storage_settings, open_mode, false);
791791
}
792792

793793
if (status != OpenResult::Success) {
@@ -1068,7 +1068,7 @@ OperationOutcome<KeyValueCache::ValueTypePtr> DefaultCacheImpl::Read(
10681068
auto value = memory_cache_->Get(key);
10691069
if (!value.empty()) {
10701070
PromoteKeyLru(key);
1071-
return boost::any_cast<KeyValueCache::ValueTypePtr>(value);
1071+
return olp::porting::any_cast<KeyValueCache::ValueTypePtr>(value);
10721072
}
10731073
}
10741074

olp-cpp-sdk-core/src/cache/DefaultCacheImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ class DefaultCacheImpl {
5151
bool Put(const std::string& key, const KeyValueCache::ValueTypePtr value,
5252
time_t expiry);
5353

54-
bool Put(const std::string& key, const boost::any& value,
54+
bool Put(const std::string& key, const olp::porting::any& value,
5555
const Encoder& encoder, time_t expiry);
5656

57-
boost::any Get(const std::string& key, const Decoder& decoder);
57+
olp::porting::any Get(const std::string& key, const Decoder& decoder);
5858

5959
DefaultCache::ValueTypePtr Get(const std::string& key);
6060

olp-cpp-sdk-core/src/cache/InMemoryCache.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
#include <tuple>
2828
#include <vector>
2929

30+
#include <olp/core/porting/any.h>
3031
#include <olp/core/utils/LruCache.h>
31-
#include <boost/any.hpp>
3232

3333
namespace olp {
3434
namespace cache {
@@ -42,7 +42,7 @@ class InMemoryCache {
4242
static constexpr size_t kSizeMax = std::numeric_limits<std::size_t>::max();
4343
static constexpr time_t kExpiryMax = std::numeric_limits<time_t>::max();
4444

45-
using ItemTuple = std::tuple<std::string, time_t, boost::any, size_t>;
45+
using ItemTuple = std::tuple<std::string, time_t, olp::porting::any, size_t>;
4646
using ItemTuples = std::vector<ItemTuple>;
4747
using TimeProvider = std::function<time_t()>;
4848
using ModelCacheCostFunc = std::function<std::size_t(const ItemTuple&)>;
@@ -70,10 +70,10 @@ class InMemoryCache {
7070
ModelCacheCostFunc cache_cost = DefaultCacheCost(),
7171
TimeProvider time_provider = DefaultTimeProvider());
7272

73-
bool Put(const std::string& key, const boost::any& item,
73+
bool Put(const std::string& key, const olp::porting::any& item,
7474
time_t expire_seconds = kExpiryMax, size_t = 1u);
7575

76-
boost::any Get(const std::string& key);
76+
olp::porting::any Get(const std::string& key);
7777
size_t Size() const;
7878
void Clear();
7979

0 commit comments

Comments
 (0)