Finality Engine
Loading...
Searching...
No Matches
ReflectionCache.h
Go to the documentation of this file.
1
6#pragma once
7#include <cstdint>
8#include <filesystem>
9#include <fstream>
10#include <iomanip>
11#include <optional>
12#include <stdexcept>
13#include <string>
14#include <vector>
15
18{
19 std::string Text;
20 size_t Scripts = 0;
21 bool Legacy = false;
22 std::vector<std::filesystem::path> Dependencies;
23 std::vector<std::string> Stamps;
24};
25
28{
29 public:
30 std::filesystem::path Directory;
31 std::string Context;
32
33 static std::string Key(const std::filesystem::path &path)
34 {
35 uint64_t value = 14695981039346656037ull;
36 for (unsigned char byte : path.generic_string())
37 value = (value ^ byte) * 1099511628211ull;
38 return std::to_string(value);
39 }
40
41 static std::string Stamp(const std::filesystem::path &path)
42 {
43 return (std::filesystem::is_directory(path) ? "directory"
44 : std::to_string(std::filesystem::file_size(path))) +
45 ":" + std::to_string(std::filesystem::last_write_time(path).time_since_epoch().count());
46 }
47
48 std::optional<CachedReflection> Read(const std::filesystem::path &source) const
49 {
50 try
51 {
52 std::ifstream input(Directory / Key(source), std::ios::binary);
53 std::string context, path, stamp;
54 CachedReflection result;
55 size_t count = 0;
56 if (!(input >> std::quoted(context)) || context != Context ||
57 !(input >> result.Scripts >> result.Legacy >> count) || count > 100000)
58 return {};
59 for (size_t i = 0; i < count; ++i)
60 {
61 if (!(input >> std::quoted(path) >> std::quoted(stamp)) || Stamp(path) != stamp)
62 return {};
63 result.Dependencies.emplace_back(path);
64 result.Stamps.push_back(stamp);
65 }
66 if (!(input >> std::quoted(result.Text)))
67 return {};
68 return result;
69 }
70 catch (const std::exception &)
71 {
72 return {};
73 }
74 }
75
76 void Write(const std::filesystem::path &source, const CachedReflection &value) const
77 {
78 std::filesystem::create_directories(Directory);
79 const auto path = Directory / Key(source);
80 const auto temporary = path.string() + ".tmp";
81 std::ofstream output(temporary, std::ios::binary | std::ios::trunc);
82 output << std::quoted(Context) << '\n'
83 << value.Scripts << ' ' << value.Legacy << ' ' << value.Dependencies.size() << '\n';
84 for (size_t i = 0; i < value.Dependencies.size(); ++i)
85 output << std::quoted(value.Dependencies[i].generic_string()) << ' '
86 << std::quoted(value.Stamps.at(i)) << '\n';
87 output << std::quoted(value.Text);
88 output.close();
89 if (!output)
90 throw std::runtime_error("Cannot write reflection cache");
91 std::filesystem::remove(path);
92 std::filesystem::rename(temporary, path);
93 }
94};
void Write(const std::filesystem::path &source, const CachedReflection &value) const
std::string Context
static std::string Stamp(const std::filesystem::path &path)
static std::string Key(const std::filesystem::path &path)
std::filesystem::path Directory
std::optional< CachedReflection > Read(const std::filesystem::path &source) const
Generated reflection text, script count and dependency stamps for one translation unit.
std::vector< std::string > Stamps
std::vector< std::filesystem::path > Dependencies