-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared_object_cache.h
More file actions
103 lines (86 loc) · 2.33 KB
/
shared_object_cache.h
File metadata and controls
103 lines (86 loc) · 2.33 KB
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
#pragma once
#include "otcv.h"
#include "global_handles.h"
#include <map>
#include <vector>
template <typename _Type, typename _Builder, typename _Key>
class GrowingCache {
public:
GrowingCache() {}
~GrowingCache() {
for (auto p : _cache) {
delete p.second;
}
_cache.clear();
}
_Key get_handle(_Builder& gpb) {
_Key key(gpb);
auto iter = _cache.find(key);
if (iter == _cache.end()) {
_cache[key] = new _Type(gpb);
}
return key;
}
_Type* get(const _Key& key) {
auto iter = _cache.find(key);
if (iter == _cache.end()) {
return nullptr;
}
else {
return iter->second;
}
}
typename std::unordered_map<_Key, _Type*>::iterator begin() {
return _cache.begin();
}
typename std::unordered_map<_Key, _Type*>::iterator end() {
return _cache.end();
}
private:
std::unordered_map<_Key, _Type*> _cache;
};
typedef GrowingCache<otcv::Sampler, otcv::SamplerBuilder, SamplerHandle> SamplerCache;
typedef GrowingCache<otcv::GraphicsPipeline, otcv::GraphicsPipelineBuilder, PipelineHandle> PipelineCache;
typedef GrowingCache<otcv::Image, otcv::ImageBuilder, ImageByNameHandle> ImageCache;
template <typename Con>
struct SequenceHash {
size_t operator()(const Con& container) const {
size_t hash = 0;
std::hash<Con::value_type> hasher;
for (const Con::value_type& item : container) {
// A common hash combining strategy (boost-like)
hash ^= hasher(item) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
}
return hash;
}
};
namespace std {
template <>
struct hash<SamplerHandle> {
std::size_t operator()(const SamplerHandle& handle) const {
SequenceHash<std::vector<uint8_t>> hasher;
return hasher(handle.serialized);
}
};
template <>
struct hash<PipelineHandle> {
std::size_t operator()(const PipelineHandle& handle) const {
SequenceHash<std::vector<uint8_t>> hasher;
return hasher(handle.serialized);
}
};
template <>
struct hash<ImageByNameHandle> {
std::size_t operator()(const ImageByNameHandle& handle) const {
SequenceHash<std::string> hasher;
return hasher(handle.name);
}
};
}
template<typename T>
void serialize_trivial(std::vector<uint8_t>& serialized, T value) {
uint8_t* start = (uint8_t*)(&value);
size_t length = sizeof(value);
serialized.insert(serialized.end(), start, start + length);
}
void serialize_string(std::vector<uint8_t>& serialized, const std::string& str);