Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::FileSystemWatcherAsync Struct Reference

FileSystemWatcherAsync is an implementation of SC::FileSystemWatcher that uses SC::Async. More...

#include <FileSystemWatcherAsync.h>

Inheritance diagram for SC::FileSystemWatcherAsync:
SC::FileSystemWatcher::EventLoopRunner

Public Member Functions

void init (AsyncEventLoop &loop)
 
- Public Member Functions inherited from SC::FileSystemWatcher::EventLoopRunner

Protected Types

using FolderWatcher = FileSystemWatcher::FolderWatcher
 

Protected Member Functions

virtual Result windowsStartFolderFilePoll (FolderWatcher &watcher, void *handle) override
 
virtual Result windowsStopFolderFilePoll (FolderWatcher &watcher) override
 
virtual void * windowsGetOverlapped (FolderWatcher &watcher) override
 
void onEventLoopNotification (AsyncFilePoll::Result &result)
 
- Protected Member Functions inherited from SC::FileSystemWatcher::EventLoopRunner
void internalInit (FileSystemWatcher &fsWatcher, int handle)
 

Protected Attributes

AsyncEventLoopeventLoop = nullptr
 
Function< void(AsyncResult &)> onClose
 
- Protected Attributes inherited from SC::FileSystemWatcher::EventLoopRunner
FileSystemWatcherfileSystemWatcher = nullptr
 

Detailed Description

FileSystemWatcherAsync is an implementation of SC::FileSystemWatcher that uses SC::Async.

The main reason for this class to exist in a dedicated library is to break the dependency of SC::FileSystemWatcher from SC::AsyncEventLoop.

Example:

// Initialize the FileSystemWatcher
FileSystemWatcher fileSystemWatcher;
FileSystemWatcherAsync eventLoopRunner;
eventLoopRunner.init(eventLoop);
SC_TRY(fileSystemWatcher.init(eventLoopRunner));
// Setup notification callback
auto onFileModified = [&](const FileSystemWatcher::Notification& notification)
{
// This callback will be called from the thread calling AsyncEventLoop::run
StringPath fullPath;
if (notification.getFullPath(fullPath))
{
switch (notification.operation)
{
case FileSystemWatcher::Operation::Modified: // File has been modified
console.print("Modified {} {}\n", notification.relativePath, fullPath.path.view());
break;
case FileSystemWatcher::Operation::AddRemoveRename: // File was added / removed
console.print("AddRemoveRename {} {}\n", notification.relativePath, fullPath.path.view());
break;
}
}
};
// Start watching a specific folder
folderWatcher.notifyCallback = onFileModified;
SC_TRY(fileSystemWatcher.watch(folderWatcher, "/path/to/dir"));
// ...
// At a later point when there is no more need of watching the folder
SC_TRY(folderWatcher.stopWatching());
// ...
// When all watchers have been unwatched and to dispose all system resources
SC_TRY(fileSystemWatcher.close());
Note
This class has been designed to be implemented with SC::AsyncEventLoop but it's probably possible implementing another backend using a different event loop library that is capable of providing similar abstractions for file polling and event loop wake-up from a foreign thread.

Member Function Documentation

◆ windowsGetOverlapped()

virtual void * SC::FileSystemWatcherAsync::windowsGetOverlapped ( FolderWatcher & watcher)
overrideprotectedvirtual

◆ windowsStartFolderFilePoll()

virtual Result SC::FileSystemWatcherAsync::windowsStartFolderFilePoll ( FolderWatcher & watcher,
void * handle )
overrideprotectedvirtual

◆ windowsStopFolderFilePoll()

virtual Result SC::FileSystemWatcherAsync::windowsStopFolderFilePoll ( FolderWatcher & watcher)
overrideprotectedvirtual

The documentation for this struct was generated from the following file: