Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
hot_reload.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include <aegis2d/core/export.hpp>
6
7#include <chrono>
8#include <cstddef>
9#include <cstdint>
10#include <filesystem>
11#include <functional>
12#include <optional>
13#include <vector>
14
15namespace aegis2d::core {
16
20#ifdef NDEBUG
21constexpr bool hot_reload_by_default = false;
22#else
23constexpr bool hot_reload_by_default = true;
24#endif
25
40class AEGIS2D_CORE_EXPORT HotReload {
41public:
43 using Reload = std::function<void(const std::filesystem::path& path)>;
44
49 explicit HotReload(bool enabled,
50 std::chrono::milliseconds interval = std::chrono::milliseconds{250});
51
54 [[nodiscard]] bool enabled() const noexcept;
55
62 void watch(const std::filesystem::path& path, Reload reload);
63
66 [[nodiscard]] std::size_t size() const noexcept;
67
70 std::size_t update();
71
72private:
73 // What the file looked like: its time and size, or nothing while it is missing.
74 struct Stamp {
75 std::filesystem::file_time_type time{};
76 std::uintmax_t size = 0;
77 bool operator==(const Stamp&) const = default;
78 };
79 struct File {
80 std::filesystem::path path{};
81 Reload reload{};
82 std::optional<Stamp> seen{};
83 };
84
85 [[nodiscard]] static std::optional<Stamp> stamp(const std::filesystem::path& path) noexcept;
86
87 std::vector<File> files_;
88 std::chrono::milliseconds interval_;
89 std::chrono::steady_clock::time_point last_{};
90 bool enabled_;
91 bool looked_ = false;
92};
93
94} // namespace aegis2d::core
Watches files and reloads each one with its own function when it changes.
Definition hot_reload.hpp:40
HotReload(bool enabled, std::chrono::milliseconds interval=std::chrono::milliseconds{250})
Creates a watcher with no file.
bool enabled() const noexcept
Tells whether files are watched.
std::function< void(const std::filesystem::path &path)> Reload
What to do with a changed file.
Definition hot_reload.hpp:43
Logging, time, main loop, events and configuration shared by client and server.
Definition clock.hpp:11
constexpr bool hot_reload_by_default
Whether to turn hot reload on: in debug builds (NDEBUG not defined).
Definition hot_reload.hpp:23
Definition endpoint.hpp:85