52 lines
1.5 KiB
C++
52 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#include <chrono>
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
namespace izo {
|
|
|
|
enum class FileChange { Added, Removed, Modified, Renamed };
|
|
|
|
struct FileEvent {
|
|
FileChange change;
|
|
std::filesystem::path path;
|
|
std::filesystem::path previousPath;
|
|
};
|
|
|
|
struct DirectoryWatchOptions {
|
|
std::filesystem::path path;
|
|
bool recursive = true;
|
|
std::chrono::milliseconds pollInterval{250};
|
|
};
|
|
|
|
using FileEventCallback = std::function<void(const FileEvent&)>;
|
|
|
|
class DirectoryWatcher {
|
|
public:
|
|
DirectoryWatcher() noexcept = default;
|
|
~DirectoryWatcher();
|
|
DirectoryWatcher(DirectoryWatcher&&) noexcept;
|
|
DirectoryWatcher& operator=(DirectoryWatcher&&) noexcept;
|
|
DirectoryWatcher(const DirectoryWatcher&) = delete;
|
|
DirectoryWatcher& operator=(const DirectoryWatcher&) = delete;
|
|
|
|
explicit operator bool() const noexcept { return implementation_ != nullptr; }
|
|
void Stop() noexcept;
|
|
|
|
private:
|
|
struct implementation;
|
|
friend DirectoryWatcher WatchDirectory(const DirectoryWatchOptions&,
|
|
FileEventCallback, std::string*);
|
|
explicit DirectoryWatcher(std::unique_ptr<implementation> implementation) noexcept;
|
|
std::unique_ptr<implementation> implementation_;
|
|
};
|
|
|
|
DirectoryWatcher WatchDirectory(const DirectoryWatchOptions& options,
|
|
FileEventCallback callback,
|
|
std::string* errorMessage = nullptr);
|
|
|
|
} // namespace izo
|