Skip to content

Commit bfad92e

Browse files
committed
Create olp::porting::any to use either boost::any or std::any for different compilers
Relates-To: NLAM-157
1 parent 6b902ab commit bfad92e

File tree

19 files changed

+209
-84
lines changed

19 files changed

+209
-84
lines changed

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

Lines changed: 2 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,7 @@ 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, const Decoder& decoder) override;
185185

186186
/**
187187
* @brief Gets the key and binary data from the cache.

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

Lines changed: 4 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,7 @@ 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, const Decoder& encoder) = 0;
101101

102102
/**
103103
* @brief Gets the key and binary data from the cache.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 {
108+
return !operand.empty();
109+
}
110+
111+
inline void reset(any& operand) noexcept { operand = boost::any(); }
112+
113+
template <typename T, typename... Args>
114+
inline any make_any(Args&&... args) {
115+
return any(T(std::forward<Args>(args)...));
116+
}
117+
118+
template <typename T, typename U, typename... Args>
119+
inline any make_any(std::initializer_list<U> il, Args&&... args) {
120+
return any(T(il, std::forward<Args>(args)...));
121+
}
122+
123+
} // namespace porting
124+
} // namespace olp
125+
126+
#endif

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

Lines changed: 2 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,7 @@ 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, const Decoder& decoder) {
5656
return impl_->Get(key, decoder);
5757
}
5858

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ 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, const olp::porting::any& value,
277277
const Encoder& encoder, time_t expiry) {
278278
std::lock_guard<std::mutex> lock(cache_lock_);
279279
if (!is_open_) {
@@ -301,11 +301,11 @@ bool DefaultCacheImpl::Put(const std::string& key,
301301
return Write(key, value, expiry).IsSuccessful();
302302
}
303303

304-
boost::any DefaultCacheImpl::Get(const std::string& key,
304+
olp::porting::any DefaultCacheImpl::Get(const std::string& key,
305305
const Decoder& decoder) {
306306
std::lock_guard<std::mutex> lock(cache_lock_);
307307
if (!is_open_) {
308-
return boost::any();
308+
return olp::porting::any();
309309
}
310310

311311
if (memory_cache_) {
@@ -329,7 +329,7 @@ boost::any DefaultCacheImpl::Get(const std::string& key,
329329
return decoded_item;
330330
}
331331

332-
return boost::any();
332+
return olp::porting::any();
333333
}
334334

335335
KeyValueCache::ValueTypePtr DefaultCacheImpl::Get(const std::string& key) {
@@ -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: 8 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,7 @@ 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(_, _)).Times(1).WillOnce(Return(olp::porting::any()));
187187

188188
client::CancellationContext context;
189189
client::ApiLookupClientImpl client(catalog_hrn, settings_);
@@ -368,7 +368,7 @@ TEST_F(ApiLookupClientImplTest, LookupApi) {
368368
olp::http::HttpStatusCode::OK),
369369
kResponseLookupResource));
370370

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

373373
// Response contains three services that are cached independently.
374374
EXPECT_CALL(*cache_, Put(_, _, _, _)).Times(3);
@@ -569,7 +569,7 @@ TEST_F(ApiLookupClientImplTest, LookupApiAsync) {
569569
SCOPED_TRACE("Fetch from cache [CacheOnly] negative");
570570
EXPECT_CALL(*cache_, Get(cache_key, _))
571571
.Times(1)
572-
.WillOnce(Return(boost::any()));
572+
.WillOnce(Return(olp::porting::any()));
573573

574574
std::promise<client::ApiLookupClient::LookupApiResponse> promise;
575575
auto future = promise.get_future();
@@ -631,7 +631,7 @@ TEST_F(ApiLookupClientImplTest, LookupApiAsync) {
631631
.WillRepeatedly(Return(true));
632632
EXPECT_CALL(*cache_, Get(cache_key, _))
633633
.Times(1)
634-
.WillOnce(Return(boost::any()));
634+
.WillOnce(Return(olp::porting::any()));
635635

636636
std::promise<client::ApiLookupClient::LookupApiResponse> promise;
637637
auto future = promise.get_future();
@@ -665,7 +665,7 @@ TEST_F(ApiLookupClientImplTest, LookupApiAsync) {
665665
EXPECT_CALL(*cache_, Put(_, _, _, expiry))
666666
.Times(3)
667667
.WillRepeatedly(Return(true));
668-
EXPECT_CALL(*cache_, Get(_, _)).Times(1).WillOnce(Return(boost::any()));
668+
EXPECT_CALL(*cache_, Get(_, _)).Times(1).WillOnce(Return(olp::porting::any()));
669669

670670
std::promise<client::ApiLookupClient::LookupApiResponse> promise;
671671
auto future = promise.get_future();
@@ -803,7 +803,7 @@ TEST_F(ApiLookupClientImplTest, LookupApiAsync) {
803803
olp::http::HttpStatusCode::OK),
804804
kResponseLookupResource));
805805

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

808808
// Response contains three services that are cached independently.
809809
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)