libnick 2025.9.1
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
filesystemwatcher.h
Go to the documentation of this file.
1
22
23#ifndef FILESYSTEMWATCHER_H
24#define FILESYSTEMWATCHER_H
25
26#include <filesystem>
27#include <mutex>
28#include <thread>
29#include <vector>
31#include "watcherflags.h"
32#include "events/event.h"
33#ifdef _WIN32
34#include <windows.h>
35#elif defined(__APPLE__)
36#include <CoreServices/CoreServices.h>
37#endif
38
40{
45 {
46 public:
63 const std::filesystem::path& getPath() const noexcept;
68 WatcherFlags getWatcherFlags() const noexcept;
73 bool getIncludeSubdirectories() const noexcept;
85 bool isExtensionWatched(const std::filesystem::path& extension) const noexcept;
91 bool addExtensionFilter(const std::filesystem::path& extension) noexcept;
97 bool removeExtensionFilter(const std::filesystem::path& extension) noexcept;
102 bool clearExtensionFilters() noexcept;
103
104 private:
108 void watch() noexcept;
109 std::thread m_watchThread;
110 mutable std::mutex m_mutex;
111 std::filesystem::path m_path;
112 bool m_includeSubdirectories;
113 WatcherFlags m_watcherFlags;
114 Events::Event<FileSystemChangedEventArgs> m_changed;
115 bool m_watching;
116 std::vector<std::filesystem::path> m_extensionFilters;
117#ifdef _WIN32
118 HANDLE m_terminateEvent;
119#elif defined(__linux__)
120 int m_notify;
121#elif defined(__APPLE__)
122 static void callback(ConstFSEventStreamRef stream, void* clientCallBackInfo, size_t numEvents, void* eventPaths, const FSEventStreamEventFlags eventFlags[], const FSEventStreamEventId eventIds[]) noexcept;
123 FSEventStreamRef m_stream;
124 CFRunLoopRef m_runLoop;
125#endif
126 };
127}
128
129#endif //FILESYSTEMWATCHER_H
An event argument for when a file system object is changed.
Definition filesystemchangedeventargs.h:36
bool addExtensionFilter(const std::filesystem::path &extension) noexcept
Adds an extension of a file to watch for changes in the folder.
bool clearExtensionFilters() noexcept
Clears all watched extension filters. This will cause all extensions to be implicitly watched.
bool getIncludeSubdirectories() const noexcept
Gets whether or not subdirectories of the folder are watched.
Events::Event< FileSystemChangedEventArgs > & changed() noexcept
Gets the event for when a watched flag of the folder is changed.
WatcherFlags getWatcherFlags() const noexcept
Gets the flags of what to watch changes for.
bool removeExtensionFilter(const std::filesystem::path &extension) noexcept
Removes an extension of a file to watch for changes in the folder.
~FileSystemWatcher() noexcept
Destructs a FileSystemWatcher.
const std::filesystem::path & getPath() const noexcept
Gets the path of the folder being watched.
bool isExtensionWatched(const std::filesystem::path &extension) const noexcept
Gets whether or not a file extension is being watched.
FileSystemWatcher(const std::filesystem::path &path, bool includeSubdirectories, WatcherFlags watcherFlags=WatcherFlags::FileName|WatcherFlags::DirectoryName|WatcherFlags::Attributes|WatcherFlags::Size|WatcherFlags::LastWrite|WatcherFlags::LastAccess)
Constructs a FileSystemWatcher.
Definition event.h:33
Definition applicationuserdirectory.h:27
WatcherFlags
Flags to describe properties of a file system object that can change.
Definition watcherflags.h:34
@ DirectoryName
The name of a directory in the file system object has changed.
Definition watcherflags.h:36
@ LastWrite
The last write time of an item in the file system object has changed.
Definition watcherflags.h:39
@ FileName
The name of a file in the file system object has changed.
Definition watcherflags.h:35
@ Attributes
The attributes of an item in the file system object have changed.
Definition watcherflags.h:37
@ LastAccess
The last access time of an item in the file system object has changed.
Definition watcherflags.h:40
@ Size
The size of an item in the file system object has changed.
Definition watcherflags.h:38