Corrade::Utility::FileWatcher class

File watcher.

Provides a non-blocking interface to watch a single file for changes. Example usage:

Utility::FileWatcher watcher{"settings.conf"};

// in the main application loop
if(watcher.hasChanged()) {
    // reload the settings
}

Behavior

The generic implementation (currently used on all supported systems) checks for file modification time and reports a change if the modification time changes. Deleting a file and immediately recreating it with the same name will behave the same as simply updating that file, unless the file status is checked during the short time when it was deleted — in that case isValid() will return false and monitoring is stopped.

Different OSes and filesystems have different granularity of filesystem modification time:

  • Most native Linux filesystems (such as ext4) will report file modification time in millisecond precision (usually tens of milliseconds)
  • Windows, macOS and Emscripten file modification time APIs return the value in seconds, FAT filesystems have two-second precision

Constructors, destructors, conversion operators

FileWatcher(const std::string& filename) explicit
Constructor.
FileWatcher(const FileWatcher&) deleted
Copying is not allowed.
FileWatcher(FileWatcher&&) noexcept
Move constructor.

Public functions

auto operator=(const FileWatcher&) -> FileWatcher& deleted
Copying is not allowed.
auto operator=(FileWatcher&&) -> FileWatcher& noexcept
Move assignment.
auto isValid() const -> bool
Whether the file watcher is valid.
auto hasChanged() -> bool
Whether the file has changed.

Function documentation

bool Corrade::Utility::FileWatcher::isValid() const

Whether the file watcher is valid.

Returns true if the watcher was valid the last time hasChanged() was called (or, if not called yet, on construction). For example, a file could get deleted in the meantime or a filesystem unmounted. Note that it's also possible for an invalid watch to become valid later, for example if the file under watch gets recreated again.

bool Corrade::Utility::FileWatcher::hasChanged()

Whether the file has changed.

Returns true if the file modification time was updated since the previous call, false otherwise.