Unravel Engine C++ Reference
Loading...
Searching...
No Matches
watcher.h
Go to the documentation of this file.
1#ifndef FS_WATCHER_H
2#define FS_WATCHER_H
3
4#include <atomic>
5#include <chrono>
6#include <condition_variable>
7#include <functional>
8#include <map>
9#include <memory>
10#include <mutex>
11#include <string>
12#include <thread>
13
14#include "filesystem.h"
15#include "pattern_filter.h"
16
17namespace fs
18{
19
21{
22public:
31
32 struct entry
33 {
34 fs::path path;
35 fs::path last_path;
37 fs::file_time_type last_mod_time;
38 std::uintmax_t size = 0;
39 fs::file_type type;
40 };
41
42 using notify_callback = std::function<void(const std::vector<entry>&, bool)>;
43 using clock_t = std::chrono::steady_clock;
44 //-----------------------------------------------------------------------------
45 // Name : watch ()
52 //-----------------------------------------------------------------------------
53 static auto watch(const fs::path& path,
54 const pattern_filter& filter,
55 bool recursive,
56 bool initial_list,
57 clock_t::duration poll_interval,
58 notify_callback callback) -> std::uint64_t;
59
60 // Backward compatible overload
61 static auto watch(const fs::path& path,
62 const std::string& filter_pattern,
63 bool recursive,
64 bool initial_list,
65 clock_t::duration poll_interval,
66 notify_callback callback) -> std::uint64_t;
67
68 //-----------------------------------------------------------------------------
69 // Name : unwatch ()
73 //-----------------------------------------------------------------------------
74 static void unwatch(std::uint64_t key);
75
76 //-----------------------------------------------------------------------------
77 // Name : unwatch_all ()
81 //-----------------------------------------------------------------------------
82 static void unwatch_all();
83
84 //-----------------------------------------------------------------------------
85 // Name : touch ()
90 //-----------------------------------------------------------------------------
91 static void touch(const fs::path& path, bool recursive, fs::file_time_type time = fs::now());
92
93 //-----------------------------------------------------------------------------
94 // Name : ~watcher ()
100 //-----------------------------------------------------------------------------
101 ~watcher();
102 watcher() = default;
103
104 static void pause();
105 static void resume();
106
107protected:
108 //-----------------------------------------------------------------------------
109 // Name : close ()
115 //-----------------------------------------------------------------------------
116 void close();
117
118 //-----------------------------------------------------------------------------
119 // Name : start ()
125 //-----------------------------------------------------------------------------
126 void start();
127
128 //-----------------------------------------------------------------------------
129 // Name : watch_impl ()
135 //-----------------------------------------------------------------------------
136 static auto watch_impl(const fs::path& path,
137 const pattern_filter& filter,
138 bool recursive,
139 bool initial_list,
140 clock_t::duration poll_interval,
141 notify_callback& list_callback) -> std::uint64_t;
142
143 static void unwatch_impl(std::uint64_t key);
144
145 static void unwatch_all_impl();
146
148 std::mutex mutex_;
150 std::atomic<bool> watching_ = {false};
151
152 std::condition_variable cv_;
154 std::thread thread_;
156 class impl;
157 std::map<std::uint64_t, std::shared_ptr<impl>> watchers_;
158};
159
160auto to_string(const watcher::entry& e) -> std::string;
161
162} // namespace fs
163
164#endif
A filter that combines include and exclude patterns for file/directory filtering.
static void unwatch_impl(std::uint64_t key)
Definition watcher.cpp:596
watcher()=default
static void unwatch(std::uint64_t key)
Un-watches a previously registered file or directory.
Definition watcher.cpp:426
static void resume()
Definition watcher.cpp:491
static void unwatch_all_impl()
Definition watcher.cpp:607
std::function< void(const std::vector< entry > &, bool)> notify_callback
Definition watcher.h:42
static void pause()
Definition watcher.cpp:475
std::atomic< bool > watching_
Atomic bool sync.
Definition watcher.h:150
static void unwatch_all()
Un-watches all previously registered file or directory.
Definition watcher.cpp:431
static void touch(const fs::path &path, bool recursive, fs::file_time_type time=fs::now())
Sets the last modification time of a file or directory. by default sets the time to the current time.
Definition watcher.cpp:436
static auto watch(const fs::path &path, const std::string &filter_pattern, bool recursive, bool initial_list, clock_t::duration poll_interval, notify_callback callback) -> std::uint64_t
void start()
Definition watcher.cpp:519
std::condition_variable cv_
Definition watcher.h:152
std::map< std::uint64_t, std::shared_ptr< impl > > watchers_
Definition watcher.h:157
std::chrono::steady_clock clock_t
Definition watcher.h:43
static auto watch_impl(const fs::path &path, const pattern_filter &filter, bool recursive, bool initial_list, clock_t::duration poll_interval, notify_callback &list_callback) -> std::uint64_t
Definition watcher.cpp:565
void close()
Definition watcher.cpp:506
std::thread thread_
Thread that polls for changes.
Definition watcher.h:154
std::mutex mutex_
Mutex for the file watchers.
Definition watcher.h:148
static auto watch(const fs::path &path, const pattern_filter &filter, bool recursive, bool initial_list, clock_t::duration poll_interval, notify_callback callback) -> std::uint64_t
Watches a file or directory for modification and call back the specified std::function....
Definition watcher.cpp:416
Definition cache.hpp:11
auto to_string(const watcher::entry &e) -> std::string
Definition watcher.cpp:617
fs::path last_path
Definition watcher.h:35
fs::file_type type
Definition watcher.h:39
fs::file_time_type last_mod_time
Definition watcher.h:37
entry_status status
Definition watcher.h:36
fs::path path
Definition watcher.h:34