-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInMemoryFileSystem.h
62 lines (52 loc) · 1.51 KB
/
InMemoryFileSystem.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
54
55
56
57
58
59
60
61
62
#pragma once
#include <variant>
#include <vector>
#include <string>
#include <span>
#include <algorithm>
#include <array>
#include <chrono>
#include <unordered_map>
class InMemoryFileSystem;
class InMemoryFile {
friend class InMemoryFileSystem;
using PathType = std::u8string;
public:
using PathView = std::u8string_view;
using HashType = std::array<std::byte, 32>;
PathView getPath() const {
return Path;
}
PathView getFileName() const {
const auto LastSeparator = Path.find_last_of(u8'/');
return PathView(Path).substr(LastSeparator != PathType::npos ? LastSeparator : 0);
}
PathType Path;
HashType Hash;
};
class InMemoryFileSystem
{
friend class InMemoryFile;
using FileType = std::vector<std::byte>;
public:
using PathType = InMemoryFile::PathType;
using PathView = InMemoryFile::PathView;
using HashType = InMemoryFile::HashType;
using FileView = std::span<const std::byte>;
using TimePointType = std::chrono::time_point<std::chrono::utc_clock>;
InMemoryFile createFile(PathType Path, FileView BinaryContent);
std::uint64_t getFileSize(size_t Index) const noexcept;
auto getModifiedTime(size_t Index) const noexcept -> TimePointType;
auto getFileView(size_t Index) const noexcept -> FileView;
std::vector<InMemoryFile> Files;
private:
struct GetTruncatedHash
{
std::size_t operator()(const HashType& HashValue) const
{
return *reinterpret_cast<const std::size_t*>(HashValue.data());
}
};
std::unordered_map<HashType, FileType, GetTruncatedHash> BlobStorage;
TimePointType ModifiedTime;
};