lfilesystem  0.0.1
C++ filesystem library
lfilesystem_FileWatcher.h
Go to the documentation of this file.
1 /*
2  * ======================================================================================
3  * __ ____ __ __ ____ ___
4  * ( ) (_ _)( \/ )( ___)/ __)
5  * )(__ _)(_ ) ( )__) \__ \
6  * (____)(____)(_/\/\_)(____)(___/
7  *
8  * This file is part of the Limes open source library and is licensed under the terms of the GNU Public License.
9  *
10  * Commercial licenses are available; contact the maintainers at ben.the.vining@gmail.com to inquire for details.
11  *
12  * ======================================================================================
13  */
14 
15 #pragma once
16 
17 #include <memory>
18 #include "lfilesystem/lfilesystem_Export.h"
20 
27 namespace limes::files
28 {
29 
67 class LFILE_EXPORT FileWatcher
68 {
69 public:
77  explicit FileWatcher (const FilesystemEntry& fileToWatch);
78 
82  FileWatcher() noexcept;
83 
85  virtual ~FileWatcher();
86 
87  FileWatcher (const FileWatcher&) = delete;
88  FileWatcher& operator= (const FileWatcher&) = delete;
89 
90  FileWatcher (FileWatcher&&) = delete;
91  FileWatcher& operator= (FileWatcher&&) = delete;
92 
96  virtual void fileAccessed (const FilesystemEntry& /*path*/) { }
97 
102  virtual void fileMetadataChanged (const FilesystemEntry& /*path*/) { }
103 
107  virtual void fileHandleClosed (const FilesystemEntry& /*path*/) { }
108 
110  virtual void fileCreated (const FilesystemEntry& /*path*/) { }
111 
113  virtual void fileDeleted (const FilesystemEntry& /*path*/) { }
114 
122  virtual void fileModified (const FilesystemEntry& /*path*/) { }
123 
128  virtual void fileMoved (const FilesystemEntry& /*path*/) { }
129 
133  virtual void fileOpened (const FilesystemEntry& /*path*/) { }
134 
139  virtual void otherEventType (const FilesystemEntry& /*path*/) { }
140 
146  bool start();
147 
154  bool start (const FilesystemEntry& newPathToWatch);
155 
159  void stop();
160 
163  bool isRunning();
164 
166  [[nodiscard]] FilesystemEntry getWatchedPath() const noexcept;
167 
171  static bool supportedBySystem() noexcept;
172 
173 private:
174  class Impl;
175 
176  [[maybe_unused]] std::unique_ptr<Impl> pimpl;
177 
178  [[maybe_unused]] FilesystemEntry watchedPath;
179 };
180 
181 } // namespace files
This class listens for changes to or operations on a certain file, and receives callbacks to be notif...
virtual void fileMetadataChanged(const FilesystemEntry &)
Called when a file's metadata is changed, such as permissions, owner, etc.
virtual void fileCreated(const FilesystemEntry &)
Called when a file or subdirectory is created in a watched directory.
virtual void fileMoved(const FilesystemEntry &)
Called when a file is moved or renamed.
virtual void otherEventType(const FilesystemEntry &)
Called when an unspecified event has occurred, such as one of the watched file's parent directories b...
bool start()
Restarts watching the path specified at construction.
virtual void fileHandleClosed(const FilesystemEntry &)
Called when a file handle is closed (from either reading or writing).
virtual void fileDeleted(const FilesystemEntry &)
Called when a file is deleted.
FileWatcher(const FilesystemEntry &fileToWatch)
Creates a FileWatcher to watch the given file or directory.
bool isRunning()
Returns true if the FilesystemWatcher is currently monitoring events.
bool start(const FilesystemEntry &newPathToWatch)
Begins watching the new specified path.
FileWatcher() noexcept
Creates an inactive FileWatcher that does nothing.
void stop()
Stops the FilesystemWatcher's event callbacks.
virtual void fileModified(const FilesystemEntry &)
Called when a file's content is modified.
FilesystemEntry getWatchedPath() const noexcept
Returns the path that is currently being watched.
virtual void fileOpened(const FilesystemEntry &)
Called when a file handle is created (for either reading or writing).
The base class for any kind of object on the filesystem.
This file defines the FilesystemEntry class.
Filesystem utilities.