-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoClearingContainer.h
53 lines (42 loc) · 1.24 KB
/
AutoClearingContainer.h
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
#ifndef AUTOCLEARINGCONTAINER_H
#define AUTOCLEARINGCONTAINER_H
#include <memory>
#include <map>
#include <functional>
namespace util {
/**
* A container that provides a handle per item contained, and lazily removes the
* item once no remaining instances of the handle exist.
*/
using Handle = std::shared_ptr<int>;
template <typename ValueType>
class AutoClearingContainer {
public:
using value_type = ValueType;
[[nodiscard]] Handle PushBack(ValueType&& value)
{
auto lifetime = std::make_shared<int>(0);
values_.insert({lifetime, std::move(value)});
return lifetime;
}
void ForEach(const std::function<void(ValueType&)>& action)
{
std::erase_if(values_, [&](const auto& iter)
{
auto& [handle, value] = iter;
auto handleValid = handle.lock();
return !handleValid;
});
for (auto& pair : values_) {
auto& [handle, value] = pair;
auto handleValid = handle.lock();
if (handleValid) {
action(value);
}
}
}
private:
std::map<std::weak_ptr<int>, ValueType, std::owner_less<std::weak_ptr<int>>> values_;
};
} // end namespace util
#endif // AUTOCLEARINGCONTAINER_H