-
Notifications
You must be signed in to change notification settings - Fork 34
/
ref_cache.hpp
121 lines (98 loc) · 2.84 KB
/
ref_cache.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <map>
#include <memory>
#include <optional>
namespace kagome {
/**
* @brief Cache keeps an item inside if any reference exists. If all
* references lost, then item removes from cache. If an item exists, then it
* returns new reference on it.
*/
template <typename K, typename V>
struct RefCache : std::enable_shared_from_this<RefCache<K, V>> {
struct RefObj;
using Value = std::decay_t<V>;
using Container = std::map<K, std::weak_ptr<RefObj>>;
using Iterator = typename Container::iterator;
using SRefCache = std::shared_ptr<RefCache>;
using Key = K;
struct RefObj {
template <typename... Args>
RefObj(Args &&...args) : obj_(std::forward<Args>(args)...) {}
RefObj(RefObj &&) = default;
RefObj(const RefObj &) = delete;
RefObj &operator=(RefObj &&) = delete;
RefObj &operator=(const RefObj &) = delete;
~RefObj() {
if (keeper_) {
keeper_->remove(it_);
}
}
Value &value_mut() {
return obj_;
}
const Value &value() const {
return obj_;
}
private:
void store_internal_refs(Iterator it, SRefCache keeper) {
BOOST_ASSERT(keeper);
BOOST_ASSERT(!keeper_);
it_ = it;
keeper_ = keeper;
}
friend struct RefCache;
Value obj_;
Iterator it_;
SRefCache keeper_;
};
using SRefObj = std::shared_ptr<RefObj>;
static SRefCache create() {
return SRefCache(new RefCache());
}
/**
* @brief returns an element, if exists. Otherway `nullopt`.
* @param k is a key of an element
*/
std::optional<SRefObj> get(const Key &k) {
[[likely]] if (auto it = items_.find(k); it != items_.end()) {
auto obj = it->second.lock();
BOOST_ASSERT(obj);
return obj;
}
return std::nullopt;
}
/**
* @brief returns an existed element or creates new one and store it in
* cache.
* @param k is a key of an element
* @param f is a functor to create RefObj
*/
template <typename F>
SRefObj get_or_insert(const Key &k, F &&f) {
[[likely]] if (auto it = items_.find(k); it != items_.end()) {
auto obj = it->second.lock();
BOOST_ASSERT(obj);
return obj;
}
auto obj = std::make_shared<RefObj>(std::forward<F>(f)());
auto [it, inserted] = items_.emplace(k, obj);
BOOST_ASSERT(inserted);
BOOST_ASSERT(this->shared_from_this());
obj->store_internal_refs(it, this->shared_from_this());
return obj;
}
private:
RefCache() = default;
void remove(const Iterator &it) {
items_.erase(it);
}
friend struct RefObj;
Container items_;
};
} // namespace kagome