Skip to content

Commit e3a0e86

Browse files
committed
Create any wrapper to use either boost or std any
Relates-To: NLAM-157 Signed-off-by: Khomenko Denys <[email protected]>
1 parent 6b902ab commit e3a0e86

File tree

19 files changed

+234
-92
lines changed

19 files changed

+234
-92
lines changed

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

olp-cpp-sdk-core/tests/client/ApiLookupClientImplTest.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ TEST_F(ApiLookupClientImplTest, LookupApi) {
104104
SCOPED_TRACE("Fetch from cache [CacheOnly] negative");
105105
EXPECT_CALL(*cache_, Get(cache_key, _))
106106
.Times(1)
107-
.WillOnce(Return(boost::any()));
107+
.WillOnce(Return(olp::porting::any()));
108108

109109
client::CancellationContext context;
110110
client::ApiLookupClientImpl client(catalog_hrn, settings_);
@@ -154,7 +154,7 @@ TEST_F(ApiLookupClientImplTest, LookupApi) {
154154
.WillRepeatedly(Return(true));
155155
EXPECT_CALL(*cache_, Get(cache_key, _))
156156
.Times(1)
157-
.WillOnce(Return(boost::any()));
157+
.WillOnce(Return(olp::porting::any()));
158158

159159
client::CancellationContext context;
160160
client::ApiLookupClientImpl client(catalog_hrn, settings_);
@@ -183,7 +183,9 @@ TEST_F(ApiLookupClientImplTest, LookupApi) {
183183
EXPECT_CALL(*cache_, Put(_, _, _, expiry))
184184
.Times(3)
185185
.WillRepeatedly(Return(true));
186-
EXPECT_CALL(*cache_, Get(_, _)).Times(1).WillOnce(Return(boost::any()));
186+
EXPECT_CALL(*cache_, Get(_, _))
187+
.Times(1)
188+
.WillOnce(Return(olp::porting::any()));
187189

188190
client::CancellationContext context;
189191
client::ApiLookupClientImpl client(catalog_hrn, settings_);
@@ -368,7 +370,9 @@ TEST_F(ApiLookupClientImplTest, LookupApi) {
368370
olp::http::HttpStatusCode::OK),
369371
kResponseLookupResource));
370372

371-
EXPECT_CALL(*cache_, Get(_, _)).Times(1).WillOnce(Return(boost::any()));
373+
EXPECT_CALL(*cache_, Get(_, _))
374+
.Times(1)
375+
.WillOnce(Return(olp::porting::any()));
372376

373377
// Response contains three services that are cached independently.
374378
EXPECT_CALL(*cache_, Put(_, _, _, _)).Times(3);
@@ -569,7 +573,7 @@ TEST_F(ApiLookupClientImplTest, LookupApiAsync) {
569573
SCOPED_TRACE("Fetch from cache [CacheOnly] negative");
570574
EXPECT_CALL(*cache_, Get(cache_key, _))
571575
.Times(1)
572-
.WillOnce(Return(boost::any()));
576+
.WillOnce(Return(olp::porting::any()));
573577

574578
std::promise<client::ApiLookupClient::LookupApiResponse> promise;
575579
auto future = promise.get_future();
@@ -631,7 +635,7 @@ TEST_F(ApiLookupClientImplTest, LookupApiAsync) {
631635
.WillRepeatedly(Return(true));
632636
EXPECT_CALL(*cache_, Get(cache_key, _))
633637
.Times(1)
634-
.WillOnce(Return(boost::any()));
638+
.WillOnce(Return(olp::porting::any()));
635639

636640
std::promise<client::ApiLookupClient::LookupApiResponse> promise;
637641
auto future = promise.get_future();
@@ -665,7 +669,9 @@ TEST_F(ApiLookupClientImplTest, LookupApiAsync) {
665669
EXPECT_CALL(*cache_, Put(_, _, _, expiry))
666670
.Times(3)
667671
.WillRepeatedly(Return(true));
668-
EXPECT_CALL(*cache_, Get(_, _)).Times(1).WillOnce(Return(boost::any()));
672+
EXPECT_CALL(*cache_, Get(_, _))
673+
.Times(1)
674+
.WillOnce(Return(olp::porting::any()));
669675

670676
std::promise<client::ApiLookupClient::LookupApiResponse> promise;
671677
auto future = promise.get_future();
@@ -803,7 +809,9 @@ TEST_F(ApiLookupClientImplTest, LookupApiAsync) {
803809
olp::http::HttpStatusCode::OK),
804810
kResponseLookupResource));
805811

806-
EXPECT_CALL(*cache_, Get(_, _)).Times(1).WillOnce(Return(boost::any()));
812+
EXPECT_CALL(*cache_, Get(_, _))
813+
.Times(1)
814+
.WillOnce(Return(olp::porting::any()));
807815

808816
// Response contains three services that are cached independently.
809817
EXPECT_CALL(*cache_, Put(_, _, _, _)).Times(3);

olp-cpp-sdk-dataservice-read/tests/ApiClientLookupTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ TEST(ApiClientLookupTest, LookupApi) {
7777
SCOPED_TRACE("Fetch from cache [CacheOnly] negative");
7878
EXPECT_CALL(*cache, Get(cache_key, _))
7979
.Times(1)
80-
.WillOnce(Return(boost::any()));
80+
.WillOnce(Return(olp::porting::any()));
8181

8282
client::CancellationContext context;
8383
auto response = read::ApiClientLookup::LookupApi(
@@ -251,7 +251,7 @@ TEST(ApiClientLookupTest, LookupApiConcurrent) {
251251

252252
testing::InSequence s;
253253

254-
EXPECT_CALL(*cache, Get(cache_key, _)).WillOnce(Return(boost::any()));
254+
EXPECT_CALL(*cache, Get(cache_key, _)).WillOnce(Return(olp::porting::any()));
255255
EXPECT_CALL(*network, Send(IsGetRequest(lookup_url), _, _, _, _))
256256
.Times(1)
257257
.WillOnce(ReturnHttpResponse(olp::http::NetworkResponse().WithStatus(

0 commit comments

Comments
 (0)