Refactor FileSystemWatcher interface to make it simpler

This commit is contained in:
Wojtek Figat
2021-05-30 14:12:17 +02:00
parent 2a3a15533c
commit 3c72abad37
2 changed files with 22 additions and 49 deletions

View File

@@ -22,61 +22,34 @@ enum class FileSystemAction
/// </summary>
class FLAXENGINE_API FileSystemWatcherBase : public NonCopyable
{
protected:
bool _isEnabled;
bool _withSubDirs;
String _directory;
public:
FileSystemWatcherBase(const String& directory, bool withSubDirs)
: _isEnabled(true)
, _withSubDirs(withSubDirs)
, _directory(directory)
: Directory(directory)
, WithSubDirs(withSubDirs)
, Enabled(true)
{
}
public:
/// <summary>
/// The watcher directory path.
/// </summary>
const String Directory;
/// <summary>
/// The value whenever watcher is tracking changes in subdirectories.
/// </summary>
const bool WithSubDirs;
/// <summary>
/// The current watcher enable state.
/// </summary>
bool Enabled;
/// <summary>
/// Action fired when directory or file gets changed. Can be invoked from main or other thread depending on the platform.
/// </summary>
Delegate<const String&, FileSystemAction> OnEvent;
public:
/// <summary>
/// Gets the watcher directory string.
/// </summary>
/// <returns>The target directory path.</returns>
const String& GetDirectory() const
{
return _directory;
}
/// <summary>
/// Gets the value whenever watcher is tracking changes in subdirectories.
/// </summary>
/// <returns>True if watcher is tracking changes in subdirectories, otherwise false.</returns>
bool WithSubDirs() const
{
return _withSubDirs;
}
/// <summary>
/// Gets the current watcher enable state.
/// </summary>
/// <returns>True if watcher is enabled, otherwise false.</returns>
bool GetEnabled() const
{
return _isEnabled;
}
/// <summary>
/// Sets the current enable state.
/// </summary>
/// <param name="value">A state to assign.</param>
void SetEnabled(bool value)
{
_isEnabled = value;
}
};

View File

@@ -87,7 +87,7 @@ VOID CALLBACK NotificationCompletion(DWORD dwErrorCode, DWORD dwNumberOfBytesTra
{
// Build path
String path(notify->FileName, notify->FileNameLength / sizeof(WCHAR));
path = watcher->GetDirectory() / path;
path = watcher->Directory / path;
// Send event
watcher->OnEvent(path, action);
@@ -106,7 +106,7 @@ BOOL RefreshWatch(WindowsFileSystemWatcher* watcher)
watcher->DirectoryHandle,
watcher->Buffer[watcher->CurrentBuffer],
FileSystemWatcher::BufferSize,
watcher->WithSubDirs() ? TRUE : FALSE,
watcher->WithSubDirs ? TRUE : FALSE,
FILE_NOTIFY_CHANGE_CREATION | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_FILE_NAME,
&dwBytesReturned,
(OVERLAPPED*)&watcher->Overlapped,