libnick 2024.12.0
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
filesystemwatcher.h
Go to the documentation of this file.
1
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;
85 bool isExtensionWatched(const std::filesystem::path& extension);
91 bool addExtensionFilter(const std::filesystem::path& extension);
97 bool removeExtensionFilter(const std::filesystem::path& extension);
103
104 private:
108 void watch();
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;
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[]);
123 FSEventStreamRef m_stream;
124 CFRunLoopRef m_runLoop;
125#endif
126 };
127}
128
129#endif //FILESYSTEMWATCHER_H
An event that can have handlers subscribe to it, which in turn will be called when the event is invok...
Definition event.h:49
A watcher of a file system folder.
Definition filesystemwatcher.h:45
WatcherFlags getWatcherFlags() const
Gets the flags of what to watch changes for.
bool addExtensionFilter(const std::filesystem::path &extension)
Adds an extension of a file to watch for changes in the folder.
bool getIncludeSubdirectories() const
Gets whether or not subdirectories of the folder are watched.
const std::filesystem::path & getPath() const
Gets the path of the folder being watched.
Events::Event< FileSystemChangedEventArgs > & changed()
Gets the event for when a watched flag of the folder is changed.
bool clearExtensionFilters()
Clears all watched extension filters. This will cause all extensions to be implicitly watched.
~FileSystemWatcher()
Destructs a FileSystemWatcher.
bool removeExtensionFilter(const std::filesystem::path &extension)
Removes an extension of a file to watch for changes in the folder.
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.
bool isExtensionWatched(const std::filesystem::path &extension)
Gets whether or not a file extension is being watched.
Definition applicationuserdirectory.h:27
WatcherFlags
Flags to describe properties of a file system object that can change.
Definition watcherflags.h:32
@ DirectoryName
The name of a directory in the file system object has changed.
@ LastWrite
The last write time of an item in the file system object has changed.
@ FileName
The name of a file in the file system object has changed.
@ Attributes
The attributes of an item in the file system object have changed.
@ LastAccess
The last access time of an item in the file system object has changed.
@ Size
The size of an item in the file system object has changed.