summaryrefslogtreecommitdiff
path: root/lib/cache.h
blob: 2c3975bb82d48bec19e4426589aa3d7632828ba8 (plain)
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
#pragma once

#include "special_members.h"
#include <functional>
#include <map>
#include <memory>
#include <tuple>

template<typename Obj, typename... KeyParts> class Cache {
public:
	using Ptr = std::shared_ptr<Obj>;
	using Key = std::tuple<KeyParts...>;

	Cache() = default;
	virtual ~Cache() = default;
	DEFAULT_MOVE(Cache);
	NO_COPY(Cache);

	[[nodiscard]] Ptr
	get(const KeyParts &... keyparts)
	{
		auto key = std::tie(keyparts...);
		if (auto e = cached.find(key); e != cached.end()) {
			return e->second;
		}
		return cached.emplace(key, construct(keyparts...)).first->second;
	}

	[[nodiscard]] virtual Ptr
	construct(const KeyParts &... keyparts) const
	{
		return std::make_shared<Obj>(keyparts...);
	}

private:
	std::map<Key, Ptr, std::less<>> cached;
};
// IWYU pragma: no_forward_declare Cache