Watches files and reloads each one with its own function when it changes.
More...
#include <aegis2d/core/hot_reload.hpp>
|
| using | Reload = std::function< void(const std::filesystem::path &path)> |
| | What to do with a changed file.
|
| |
|
| | 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.
|
| |
| void | watch (const std::filesystem::path &path, Reload reload) |
| | Starts watching a file, as it is now.
|
| |
| std::size_t | size () const noexcept |
| | Files watched.
|
| |
| std::size_t | update () |
| | Reloads the files that changed since they were last seen.
|
| |
Watches files and reloads each one with its own function when it changes.
A texture, a JSON file or anything read from disk registers how to reload it with watch(); update(), called once per frame, looks at the files at most every interval and calls the reload function of each file whose time or size changed. A reload that throws (a file caught half-written, an image that does not decode) is logged and the file stays watched, so the next save is tried again; the game keeps what it had. Off, as in release builds by default, watch() and update() do nothing.
hot.update();
static Config from_file(const std::filesystem::path &path)
Reads and parses a JSON file.
Watches files and reloads each one with its own function when it changes.
Definition hot_reload.hpp:40
void watch(const std::filesystem::path &path, Reload reload)
Starts watching a file, as it is now.
constexpr bool hot_reload_by_default
Whether to turn hot reload on: in debug builds (NDEBUG not defined).
Definition hot_reload.hpp:23
◆ Reload
What to do with a changed file.
◆ HotReload()
| aegis2d::core::HotReload::HotReload |
( |
bool |
enabled, |
|
|
std::chrono::milliseconds |
interval = std::chrono::milliseconds{250} |
|
) |
| |
|
explicit |
Creates a watcher with no file.
- Parameters
-
| enabled | false to do nothing, such as hot_reload_by_default in a release build. |
| interval | Least time between two looks at the files; zero looks at every update(). |
◆ enabled()
| bool aegis2d::core::HotReload::enabled |
( |
| ) |
const |
|
noexcept |
Tells whether files are watched.
- Returns
- The value given to the constructor.
◆ size()
| std::size_t aegis2d::core::HotReload::size |
( |
| ) |
const |
|
noexcept |
Files watched.
- Returns
- How many different files; 0 when off.
◆ update()
| std::size_t aegis2d::core::HotReload::update |
( |
| ) |
|
Reloads the files that changed since they were last seen.
- Returns
- How many reloads succeeded; failures are logged, not thrown.
◆ watch()
| void aegis2d::core::HotReload::watch |
( |
const std::filesystem::path & |
path, |
|
|
Reload |
reload |
|
) |
| |
Starts watching a file, as it is now.
Watching a file again replaces its reload function, so it is reloaded once per change whoever registers it, however often. A reload function may watch more files, as when a level file that changed brings new textures.
- Parameters
-
| path | File to watch; it may not exist yet, its first appearance is a change. |
| reload | Called with path by update() when the file changes. |
The documentation for this class was generated from the following file: