Initial commit
This commit is contained in:
commit
8b8a7f4f9a
17 files changed
+4930
No files matched your search
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <filesystem>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <chrono>
|
||||
|
||||
namespace Echoion::Plugin {
|
||||
|
||||
/**
|
||||
* @brief Simple file watcher that monitors a file for changes
|
||||
* and triggers a callback when the file is modified.
|
||||
*/
|
||||
class FileWatcher {
|
||||
public:
|
||||
using ChangeCallback = std::function<void()>;
|
||||
|
||||
FileWatcher();
|
||||
~FileWatcher();
|
||||
|
||||
// Disable copy
|
||||
FileWatcher(const FileWatcher&) = delete;
|
||||
FileWatcher& operator=(const FileWatcher&) = delete;
|
||||
|
||||
/**
|
||||
* @brief Start watching a file for changes
|
||||
* @param filePath Path to the file to watch
|
||||
* @param callback Function to call when file changes
|
||||
* @param intervalMs Polling interval in milliseconds
|
||||
*/
|
||||
void startWatching(
|
||||
const std::filesystem::path& filePath,
|
||||
ChangeCallback callback,
|
||||
std::chrono::milliseconds intervalMs = std::chrono::milliseconds(1000)
|
||||
);
|
||||
|
||||
/**
|
||||
* @brief Stop watching
|
||||
*/
|
||||
void stopWatching();
|
||||
|
||||
[[nodiscard]] bool isWatching() const noexcept;
|
||||
|
||||
private:
|
||||
void watchLoop();
|
||||
|
||||
std::filesystem::path watchedPath_;
|
||||
ChangeCallback callback_;
|
||||
std::chrono::milliseconds intervalMs_{1000};
|
||||
std::atomic<bool> watching_{false};
|
||||
std::atomic<bool> stopRequested_{false};
|
||||
std::thread watchThread_;
|
||||
std::mutex mutex_;
|
||||
|
||||
// Store last modification time
|
||||
std::filesystem::file_time_type lastWriteTime_;
|
||||
std::uintmax_t lastFileSize_ = 0;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Plugin
|
||||
Reference in new issue
Block a user