Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
FileWatcher.h
Go to the documentation of this file.
1/**
2 * @file FileWatcher.h
3 * @brief Poll-based file change detection (Phase 7).
4 * @author Olympe Engine
5 * @date 2026-03-10
6 *
7 * @details
8 * FileWatcher uses stat() / _stat() to sample a file's modification timestamp.
9 * HasChanged() returns true the first time it detects a newer timestamp than
10 * the one recorded at Watch() / Reset() time.
11 *
12 * No filesystem-watcher OS dependency — pure polling via <sys/stat.h>.
13 *
14 * C++14 compliant — no std::optional, structured bindings, std::filesystem.
15 */
16
17#pragma once
18
19#include <string>
20#include <ctime>
21
22namespace Olympe {
23
24// ============================================================================
25// FileWatcher
26// ============================================================================
27
28/**
29 * @class FileWatcher
30 * @brief Polls a single file for modification-time changes.
31 *
32 * Typical usage:
33 * @code
34 * FileWatcher watcher("data/graph.json");
35 * // ... later, every frame or on timer ...
36 * if (watcher.HasChanged())
37 * {
38 * ReloadGraph();
39 * watcher.Reset();
40 * }
41 * @endcode
42 */
44public:
45
46 /**
47 * @brief Constructs a FileWatcher that is not yet watching any file.
48 */
50
51 /**
52 * @brief Constructs a FileWatcher and immediately begins watching @p path.
53 */
54 explicit FileWatcher(const std::string& path);
55
56 // -----------------------------------------------------------------------
57 // Control
58 // -----------------------------------------------------------------------
59
60 /**
61 * @brief Starts watching @p path, recording the current modification time.
62 */
63 void Watch(const std::string& path);
64
65 /**
66 * @brief Returns true if the file's modification time has changed since
67 * the last Watch() or Reset() call.
68 */
69 bool HasChanged();
70
71 /**
72 * @brief Resets the baseline modification timestamp to the current file time.
73 */
74 void Reset();
75
76 // -----------------------------------------------------------------------
77 // Accessors
78 // -----------------------------------------------------------------------
79
80 /**
81 * @brief Returns the path currently being watched.
82 */
83 const std::string& GetPath() const;
84
85 /**
86 * @brief Returns true if a path has been set via Watch() or the path constructor.
87 */
88 bool IsWatching() const;
89
90private:
91
92 /**
93 * @brief Platform-portable wrapper around stat() / _stat().
94 * @return Modification time, or 0 on error.
95 */
96 static time_t GetFileModTime(const std::string& path);
97
98 std::string m_Path;
101};
102
103} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Polls a single file for modification-time changes.
Definition FileWatcher.h:43
void Reset()
Resets the baseline modification timestamp to the current file time.
std::string m_Path
Definition FileWatcher.h:98
bool HasChanged()
Returns true if the file's modification time has changed since the last Watch() or Reset() call.
void Watch(const std::string &path)
Starts watching path, recording the current modification time.
FileWatcher()
Constructs a FileWatcher that is not yet watching any file.
bool IsWatching() const
Returns true if a path has been set via Watch() or the path constructor.
const std::string & GetPath() const
Returns the path currently being watched.
static time_t GetFileModTime(const std::string &path)
Platform-portable wrapper around stat() / _stat().
< Provides AssetID and INVALID_ASSET_ID