Skip to content

Commit

Permalink
Add getv function to hot reloader
Browse files Browse the repository at this point in the history
  • Loading branch information
bastikr committed Oct 8, 2024
1 parent e95f490 commit b6191b0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
22 changes: 14 additions & 8 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,23 @@ static void print_result(std::optional<std::span<const std::byte>> f) {
}

int main() {
print_result(assets::get_file("logs/source/t.txt"));
constexpr std::string_view path = "logs/source/t.txt";
std::array<std::string_view, 4> pathv{"logs", "/", "source/t", ".txt"};

std::array<std::string_view, 4> b{"logs", "/", "source/t", ".txt"};
print_result(assets::get_filev(b));
// Compile time access
print_result(assets::get<"logs/source/t.txt">());

print_result(assets::get_file_ph("logs/source/t.txt"));
// Virtual directory based
print_result(assets::get_file(path));
print_result(assets::get_filev(pathv));

print_result(assets::get<"logs/source/t.txt">());
// gperf perfect hashing access
print_result(assets::get_file_ph(path));

std::filesystem::path p(__FILE__);
std::string asset_dir = (p.parent_path() / "assets").string();
// Hot reloader (for development)
std::filesystem::path f(__FILE__);
std::string asset_dir = (f.parent_path() / "assets").string();
crl::HotReloader hot_reloader(asset_dir);
print_result(hot_reloader.get("logs/source/t.txt"));
print_result(hot_reloader.get(path));
print_result(hot_reloader.getv(pathv));
}
3 changes: 2 additions & 1 deletion lib/crl_hot_reload/include/crl_hot_reload.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class HotReloader {
public:
HotReloader(std::filesystem::path base_dir);

std::optional<std::span<std::byte>> get(std::string_view relative_path);
std::optional<std::span<std::byte>> get(std::string_view path);
std::optional<std::span<std::byte>> getv(std::span<std::string_view> path);

private:
std::filesystem::path m_base_dir;
Expand Down
22 changes: 15 additions & 7 deletions lib/crl_hot_reload/src/crl_hot_reload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ namespace crl {
HotReloader::HotReloader(std::filesystem::path base_dir)
: m_base_dir(base_dir) {}

std::optional<std::span<std::byte>>
HotReloader::get(std::string_view relative_path) {
auto path = m_base_dir / relative_path;
if (std::filesystem::is_regular_file(path)) {
std::ifstream file(path, std::ios::binary);
std::optional<std::span<std::byte>> HotReloader::get(std::string_view path) {
auto p = m_base_dir / path;
if (std::filesystem::is_regular_file(p)) {
std::ifstream file(p, std::ios::binary);
if (file.is_open()) {
file.seekg(0, std::ios::end);
std::size_t file_size = file.tellg();
Expand All @@ -25,11 +24,20 @@ HotReloader::get(std::string_view relative_path) {
file.read(reinterpret_cast<char *>(data.data()),
static_cast<std::streamsize>(file_size));

m_data[path] = std::move(data);
return m_data[path];
m_data[p] = std::move(data);
return m_data[p];
}
}
return {};
}

std::optional<std::span<std::byte>>
HotReloader::getv(std::span<std::string_view> path) {
std::string combined_path;
for (auto p : path) {
combined_path += p;
}
return get(combined_path);
}

} // namespace crl

0 comments on commit b6191b0

Please sign in to comment.