-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbeddedTextureCache.cpp
More file actions
53 lines (45 loc) · 1.5 KB
/
EmbeddedTextureCache.cpp
File metadata and controls
53 lines (45 loc) · 1.5 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
#include "EmbeddedTextureCache.h"
#include <cstddef>
#include <cstring>
#include <mutex>
#include <unordered_map>
#include <utility>
namespace EmbeddedTextureCache {
namespace {
std::mutex& m() { static std::mutex s_m; return s_m; }
std::unordered_map<std::string, std::vector<uint8_t>>& tbl() {
static std::unordered_map<std::string, std::vector<uint8_t>> s_tbl;
return s_tbl;
}
} // namespace
void store(const std::string& textureName, const std::vector<uint8_t>& bytes)
{
if (textureName.empty() || bytes.empty()) return;
std::lock_guard lock(m());
tbl()[textureName] = bytes;
}
void store(const std::string& textureName, const std::byte* data, std::size_t byteCount)
{
if (textureName.empty() || !data || byteCount == 0) return;
// `std::byte` is the C++17 type for opaque byte memory (Sonar
// S6045). Storage stays `uint8_t` so downstream consumers
// (the FBX exporter writes through m_w.writePropertyR(bytes),
// which takes std::vector<uint8_t>) don't need to change.
std::vector<uint8_t> bytes(byteCount);
std::memcpy(bytes.data(), data, byteCount);
std::lock_guard lock(m());
tbl()[textureName] = std::move(bytes);
}
std::vector<uint8_t> retrieve(const std::string& textureName)
{
if (textureName.empty()) return {};
std::lock_guard lock(m());
auto it = tbl().find(textureName);
return it == tbl().end() ? std::vector<uint8_t>{} : it->second;
}
void clear()
{
std::lock_guard lock(m());
tbl().clear();
}
} // namespace EmbeddedTextureCache