Skip to content

Commit b02f596

Browse files
hzhdlrpapolukhin
authored andcommitted
feat multi-index-lru: removed mpl usage
No description --- Pull Request resolved: #1069 Co-authored-by: antoshkka <[email protected]> commit_hash:62af210ffa16694ba9fbe7b5ed74a6ea1503c308
1 parent c1919ad commit b02f596

File tree

3 files changed

+74
-28
lines changed

3 files changed

+74
-28
lines changed

libraries/multi-index-lru/include/userver/multi-index-lru/container.hpp

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,60 @@
88
#include <boost/multi_index/sequenced_index.hpp>
99
#include <boost/multi_index_container.hpp>
1010

11-
#include <boost/mpl/joint_view.hpp>
12-
#include <boost/mpl/list.hpp>
13-
1411
#include <cstddef>
12+
#include <tuple>
1513
#include <utility>
1614

1715
USERVER_NAMESPACE_BEGIN
1816

1917
namespace multi_index_lru {
2018

19+
namespace impl {
20+
template <typename T, typename = std::void_t<>>
21+
inline constexpr bool is_mpl_na = false;
22+
23+
template <typename T>
24+
inline constexpr bool is_mpl_na<T, std::void_t<decltype(std::declval<T>().~na())>> = true;
25+
26+
template <typename... Indices>
27+
struct lazy_add_seq {
28+
using type = boost::multi_index::indexed_by<boost::multi_index::sequenced<>, Indices...>;
29+
};
30+
31+
template <typename... Indices>
32+
struct lazy_add_seq_no_last {
33+
private:
34+
template <std::size_t... I>
35+
static auto makeWithoutLast(std::index_sequence<I...>) {
36+
using Tuple = std::tuple<Indices...>;
37+
return boost::multi_index::indexed_by<boost::multi_index::sequenced<>, std::tuple_element_t<I, Tuple>...>{};
38+
}
39+
40+
public:
41+
using type = decltype(makeWithoutLast(std::make_index_sequence<sizeof...(Indices) - 1>{}));
42+
};
43+
44+
template <typename IndexList>
45+
struct add_seq_index {};
46+
47+
template <typename... Indices>
48+
struct add_seq_index<boost::multi_index::indexed_by<Indices...>> {
49+
using LastType = decltype((Indices{}, ...));
50+
51+
using type = typename std::conditional_t<
52+
is_mpl_na<LastType>,
53+
lazy_add_seq_no_last<Indices...>,
54+
lazy_add_seq<Indices...>>::type;
55+
};
56+
57+
template <typename IndexList>
58+
using add_seq_index_t = typename add_seq_index<IndexList>::type;
59+
} // namespace impl
60+
2161
/// @ingroup userver_containers
2262
///
2363
/// @brief MultiIndex LRU container
24-
template <typename Value, typename IndexSpecifierList, typename Allocator = std::allocator<Value> >
64+
template <typename Value, typename IndexSpecifierList, typename Allocator = std::allocator<Value>>
2565
class Container {
2666
public:
2767
explicit Container(size_t max_size)
@@ -69,11 +109,11 @@ class Container {
69109
return container_.template get<Tag>().erase(key) > 0;
70110
}
71111

72-
size_t size() const { return container_.size(); }
112+
std::size_t size() const { return container_.size(); }
73113
bool empty() const { return container_.empty(); }
74-
size_t capacity() const { return max_size_; }
114+
std::size_t capacity() const { return max_size_; }
75115

76-
void set_capacity(size_t new_capacity) {
116+
void set_capacity(std::size_t new_capacity) {
77117
max_size_ = new_capacity;
78118
auto& seq_index = container_.template get<0>();
79119
while (container_.size() > max_size_) {
@@ -89,14 +129,12 @@ class Container {
89129
}
90130

91131
private:
92-
using AdditionalIndices = boost::mpl::list<boost::multi_index::sequenced<> >;
93-
94-
using ExtendedIndexSpecifierList = boost::mpl::joint_view<AdditionalIndices, IndexSpecifierList>;
132+
using ExtendedIndexSpecifierList = impl::add_seq_index_t<IndexSpecifierList>;
95133

96134
using BoostContainer = boost::multi_index::multi_index_container<Value, ExtendedIndexSpecifierList, Allocator>;
97135

98136
BoostContainer container_;
99-
size_t max_size_;
137+
std::size_t max_size_;
100138
};
101139
} // namespace multi_index_lru
102140

libraries/multi-index-lru/src/main_test.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string>
44

55
#include <gtest/gtest.h>
6+
#include <boost/multi_index/hashed_index.hpp>
67
#include <boost/multi_index/member.hpp>
78

89
USERVER_NAMESPACE_BEGIN
@@ -146,6 +147,29 @@ TEST_F(ProductsTest, ProductEviction) {
146147
EXPECT_EQ(cache.find<NameTag>("Mouse"), cache.end<NameTag>());
147148
}
148149

150+
TEST(Snippet, SimpleUsage) {
151+
struct MyValueT {
152+
std::string key;
153+
int val;
154+
};
155+
156+
struct MyTag {};
157+
158+
MyValueT my_value{"some_key", 1};
159+
/// [Usage]
160+
using MyLruCache = multi_index_lru::Container<
161+
MyValueT,
162+
boost::multi_index::indexed_by<boost::multi_index::hashed_unique<
163+
boost::multi_index::tag<MyTag>,
164+
boost::multi_index::member<MyValueT, std::string, &MyValueT::key>>>>;
165+
166+
MyLruCache cache(1000); // Capacity of 1000 items
167+
cache.insert(my_value);
168+
auto it = cache.find<MyTag>("some_key");
169+
EXPECT_NE(it, cache.end<MyTag>());
170+
/// [Usage]
171+
}
172+
149173
} // namespace
150174

151175
USERVER_NAMESPACE_END

scripts/docs/en/userver/libraries/multi_index_lru.md

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,5 @@ The container maintains elements in access order while supporting multiple index
88

99
## Usage
1010

11-
```cpp
12-
#include <userver/utils/multi_index_lru.hpp>
13-
14-
using MyLruCache = multi_index_lru::LRUCacheContainer<
15-
MyValue,
16-
boost::multi_index::indexed_by<
17-
boost::multi_index::hashed_unique<
18-
boost::multi_index::tag<MyTag>,
19-
boost::multi_index::member<MyValue, std::string, &MyValue::key>
20-
>
21-
>
22-
>;
23-
24-
MyLruCache cache(1000); // Capacity of 1000 items
25-
cache.insert(my_value);
26-
auto it = cache.find<MyTag>("some_key");
27-
```
11+
@snippet libraries/multi-index-lru/src/main_test.cpp Usage
2812

0 commit comments

Comments
 (0)