Detects folder-tree changes by comparing snapshots and queues them for subscribers. poll() scans on its calling thread; dispatch() invokes handlers on its calling thread. run() is intended for a ThreadManager thread. Scan state and queue state have separate mutexes.
More...
#include <file_watcher.hpp>
|
| using | Filter = std::function<bool(const Change&)> |
| | Predicate receiving a change and returning true to deliver it; an empty filter accepts all changes. Invoked by dispatch() on its caller’s thread without the queue mutex held.
|
| using | Handler = std::function<void(const Change&)> |
| | Callback receiving a delivered change on the dispatch() caller’s thread. It runs without the queue mutex held; the change reference is valid only during the call.
|
| using | Subscription = std::uint64_t |
| | Identifier used to unregister a subscriber.
|
|
| | FileWatcher ()=default |
| | Constructs an inactive watcher.
|
| | FileWatcher (const FileWatcher &)=delete |
| | Copy construction is disabled for this synchronized watcher.
|
| FileWatcher & | operator= (const FileWatcher &)=delete |
| | Copy assignment is disabled for this synchronized watcher.
|
| bool | watch (const std::filesystem::path &root) |
| | Starts or restarts watching with a silent baseline, also acknowledging owner-made changes. Holds _scan while reading the root identity and scanning; does not clear queued changes.
|
| void | unwatch () |
| | Stops scanning and clears the snapshot under _scan; queued changes remain available.
|
| std::unique_lock< std::recursive_mutex > | hold () |
| | Returns an owning _scan lock that blocks polling until released. Use around filesystem changes and a baseline refresh to avoid reporting those changes.
|
| Subscription | subscribe (Filter filter, Handler handler) |
| | Registers callbacks under _queueMutex for future dispatch snapshots.
|
| void | unsubscribe (Subscription id) |
| | Removes a subscription under _queueMutex, if present. A dispatch already holding a copied subscriber list may still invoke it.
|
| void | setNotify (std::function< void()> notify) |
| | Replaces the run-loop notification callback under _queueMutex. run() copies and invokes it on the polling thread after a poll reports changes, without the mutex held.
|
| std::size_t | poll () |
| | Compares the watched tree with its baseline and enqueues detected changes. Holds _scan during scanning and acquires _queueMutex to enqueue; does not invoke notification. Detects root renames among siblings and stops watching if the root disappears.
|
| std::size_t | dispatch () |
| | Drains queued changes and delivers them to a snapshot of subscribers on the calling thread. Copies subscribers and swaps the queue under _queueMutex, then calls filters and handlers unlocked.
|
| void | run (const threads::StopToken &token, std::function< void()> tick=nullptr, std::chrono::milliseconds interval=std::chrono::milliseconds(1000)) |
| | Polls repeatedly on the calling thread until the stop token signals. Intended for a ThreadManager thread; tick runs after each poll and notify runs when changes were queued. Callbacks are invoked without _queueMutex held.
|
|
| using | Snapshot = std::map<std::string, Entry> |
| | Map of root-relative generic paths to scanned entry metadata.
|
|
| void | enqueue (std::vector< Change > &changes) |
| | Moves changes into the pending queue while holding _queueMutex.
|
| void | diff (const Snapshot &before, const Snapshot &after, std::vector< Change > &changes) const |
| | Appends changes between snapshots, suppressing child renames carried by a folder rename. Uses _root to construct paths; callers must hold _scan.
|
|
| static Snapshot | scanFolder (const std::filesystem::path &root) |
| | Scans a folder tree, skipping entries whose status or identity cannot be read. Iteration stops on an iterator error; permission-denied directories are skipped.
|
| static bool | inside (const std::string &path, const std::string &folder) |
| | Tests whether a generic relative path is a strict descendant of a folder path.
|
|
| std::recursive_mutex | _scan |
| | Protects watch state and snapshots; hold() exposes a scoped lock for owner operations.
|
| std::filesystem::path | _root |
| | Current watched root path, updated when the root is renamed.
|
| FileId | _rootId |
| | Identity used to detect root removal or sibling rename.
|
| bool | _watching = false |
| | Whether polling has an active baseline.
|
| Snapshot | _snapshot |
| | Most recent tree snapshot, protected by _scan.
|
| std::mutex | _queueMutex |
| | Protects the change queue, subscribers, subscription counter and notify callback.
|
| std::deque< Change > | _queue |
| | Pending changes awaiting dispatch, protected by _queueMutex.
|
| std::vector< Subscriber > | _subscribers |
| | Registered subscribers, protected by _queueMutex.
|
| Subscription | _nextSubscription = 1 |
| | Next subscription identifier, protected by _queueMutex.
|
| std::function< void()> | _notify |
| | Optional run-loop notification callback, protected by _queueMutex.
|
Detects folder-tree changes by comparing snapshots and queues them for subscribers. poll() scans on its calling thread; dispatch() invokes handlers on its calling thread. run() is intended for a ThreadManager thread. Scan state and queue state have separate mutexes.
Definition at line 159 of file file_watcher.hpp.
◆ Filter
Predicate receiving a change and returning true to deliver it; an empty filter accepts all changes. Invoked by dispatch() on its caller’s thread without the queue mutex held.
Definition at line 166 of file file_watcher.hpp.
◆ Handler
Callback receiving a delivered change on the dispatch() caller’s thread. It runs without the queue mutex held; the change reference is valid only during the call.
Definition at line 171 of file file_watcher.hpp.
◆ Snapshot
Map of root-relative generic paths to scanned entry metadata.
Definition at line 203 of file file_watcher.hpp.
◆ Subscription
◆ FileWatcher() [1/2]
| fray::watch::FileWatcher::FileWatcher |
( |
| ) |
|
|
default |
Constructs an inactive watcher.
◆ FileWatcher() [2/2]
| fray::watch::FileWatcher::FileWatcher |
( |
const FileWatcher & | | ) |
|
|
delete |
Copy construction is disabled for this synchronized watcher.
◆ diff()
| void fray::watch::FileWatcher::diff |
( |
const Snapshot & | before, |
|
|
const Snapshot & | after, |
|
|
std::vector< Change > & | changes ) const |
|
inlineprivate |
Appends changes between snapshots, suppressing child renames carried by a folder rename. Uses _root to construct paths; callers must hold _scan.
- Parameters
-
| before | Previous snapshot. |
| after | New snapshot. |
| changes | Output vector to append to. |
Definition at line 333 of file file_watcher.hpp.
◆ dispatch()
| std::size_t fray::watch::FileWatcher::dispatch |
( |
| ) |
|
|
inline |
Drains queued changes and delivers them to a snapshot of subscribers on the calling thread. Copies subscribers and swaps the queue under _queueMutex, then calls filters and handlers unlocked.
- Returns
- Number of changes drained, regardless of subscriber matches.
Definition at line 576 of file file_watcher.hpp.
◆ enqueue()
| void fray::watch::FileWatcher::enqueue |
( |
std::vector< Change > & | changes | ) |
|
|
inlineprivate |
Moves changes into the pending queue while holding _queueMutex.
- Parameters
-
| changes | Changes to enqueue; elements are moved from when nonempty. |
Definition at line 316 of file file_watcher.hpp.
◆ hold()
| std::unique_lock< std::recursive_mutex > fray::watch::FileWatcher::hold |
( |
| ) |
|
|
inline |
Returns an owning _scan lock that blocks polling until released. Use around filesystem changes and a baseline refresh to avoid reporting those changes.
Definition at line 468 of file file_watcher.hpp.
◆ inside()
| bool fray::watch::FileWatcher::inside |
( |
const std::string & | path, |
|
|
const std::string & | folder ) |
|
inlinestaticprivate |
Tests whether a generic relative path is a strict descendant of a folder path.
- Parameters
-
| path | Candidate path. |
| folder | Folder prefix. |
- Returns
- True if path is longer than folder and has folder followed by a slash.
Definition at line 304 of file file_watcher.hpp.
◆ operator=()
Copy assignment is disabled for this synchronized watcher.
◆ poll()
| std::size_t fray::watch::FileWatcher::poll |
( |
| ) |
|
|
inline |
Compares the watched tree with its baseline and enqueues detected changes. Holds _scan during scanning and acquires _queueMutex to enqueue; does not invoke notification. Detects root renames among siblings and stops watching if the root disappears.
- Returns
- Number of changes queued, or zero when inactive or unchanged.
Definition at line 521 of file file_watcher.hpp.
◆ run()
| void fray::watch::FileWatcher::run |
( |
const threads::StopToken & | token, |
|
|
std::function< void()> | tick = nullptr, |
|
|
std::chrono::milliseconds | interval = std::chrono::milliseconds(1000) ) |
|
inline |
Polls repeatedly on the calling thread until the stop token signals. Intended for a ThreadManager thread; tick runs after each poll and notify runs when changes were queued. Callbacks are invoked without _queueMutex held.
- Parameters
-
| token | Stop token used to interrupt the interval wait. |
| tick | Optional callback run after each poll. |
| interval | Delay between polls. |
Definition at line 600 of file file_watcher.hpp.
◆ scanFolder()
| Snapshot fray::watch::FileWatcher::scanFolder |
( |
const std::filesystem::path & | root | ) |
|
|
inlinestaticprivate |
Scans a folder tree, skipping entries whose status or identity cannot be read. Iteration stops on an iterator error; permission-denied directories are skipped.
- Parameters
-
- Returns
- Snapshot keyed by paths relative to root; it may be partial on errors.
Definition at line 271 of file file_watcher.hpp.
◆ setNotify()
| void fray::watch::FileWatcher::setNotify |
( |
std::function< void()> | notify | ) |
|
|
inline |
Replaces the run-loop notification callback under _queueMutex. run() copies and invokes it on the polling thread after a poll reports changes, without the mutex held.
- Parameters
-
| notify | Callback to invoke, or empty to disable notification. |
Definition at line 508 of file file_watcher.hpp.
◆ subscribe()
Registers callbacks under _queueMutex for future dispatch snapshots.
- Parameters
-
| filter | Optional predicate; an empty filter matches every change. |
| handler | Callback invoked for matching changes; must be callable. |
- Returns
- Identifier to pass to unsubscribe().
Definition at line 476 of file file_watcher.hpp.
◆ unsubscribe()
| void fray::watch::FileWatcher::unsubscribe |
( |
Subscription | id | ) |
|
|
inline |
Removes a subscription under _queueMutex, if present. A dispatch already holding a copied subscriber list may still invoke it.
- Parameters
-
Definition at line 491 of file file_watcher.hpp.
◆ unwatch()
| void fray::watch::FileWatcher::unwatch |
( |
| ) |
|
|
inline |
Stops scanning and clears the snapshot under _scan; queued changes remain available.
Definition at line 457 of file file_watcher.hpp.
◆ watch()
| bool fray::watch::FileWatcher::watch |
( |
const std::filesystem::path & | root | ) |
|
|
inline |
Starts or restarts watching with a silent baseline, also acknowledging owner-made changes. Holds _scan while reading the root identity and scanning; does not clear queued changes.
- Parameters
-
| root | Folder path to watch. |
- Returns
- True if the root identity was read; false if it could not be read.
Definition at line 440 of file file_watcher.hpp.
◆ _nextSubscription
| Subscription fray::watch::FileWatcher::_nextSubscription = 1 |
|
private |
Next subscription identifier, protected by _queueMutex.
Definition at line 259 of file file_watcher.hpp.
◆ _notify
| std::function<void()> fray::watch::FileWatcher::_notify |
|
private |
Optional run-loop notification callback, protected by _queueMutex.
Definition at line 263 of file file_watcher.hpp.
◆ _queue
| std::deque<Change> fray::watch::FileWatcher::_queue |
|
private |
Pending changes awaiting dispatch, protected by _queueMutex.
Definition at line 251 of file file_watcher.hpp.
◆ _queueMutex
| std::mutex fray::watch::FileWatcher::_queueMutex |
|
private |
Protects the change queue, subscribers, subscription counter and notify callback.
Definition at line 247 of file file_watcher.hpp.
◆ _root
| std::filesystem::path fray::watch::FileWatcher::_root |
|
private |
Current watched root path, updated when the root is renamed.
Definition at line 230 of file file_watcher.hpp.
◆ _rootId
| FileId fray::watch::FileWatcher::_rootId |
|
private |
Identity used to detect root removal or sibling rename.
Definition at line 234 of file file_watcher.hpp.
◆ _scan
| std::recursive_mutex fray::watch::FileWatcher::_scan |
|
mutableprivate |
Protects watch state and snapshots; hold() exposes a scoped lock for owner operations.
Definition at line 226 of file file_watcher.hpp.
◆ _snapshot
| Snapshot fray::watch::FileWatcher::_snapshot |
|
private |
Most recent tree snapshot, protected by _scan.
Definition at line 242 of file file_watcher.hpp.
◆ _subscribers
| std::vector<Subscriber> fray::watch::FileWatcher::_subscribers |
|
private |
Registered subscribers, protected by _queueMutex.
Definition at line 255 of file file_watcher.hpp.
◆ _watching
| bool fray::watch::FileWatcher::_watching = false |
|
private |
The documentation for this class was generated from the following file: