uvw  1.3.0
fs_event.hpp
1 #pragma once
2 
3 
4 #include <utility>
5 #include <string>
6 #include <memory>
7 #include <uv.h>
8 #include "handle.hpp"
9 #include "util.hpp"
10 #include "loop.hpp"
11 
12 
13 namespace uvw {
14 
15 
16 namespace details {
17 
18 
19 enum class UVFsEventFlags: std::underlying_type_t<uv_fs_event_flags> {
20  WATCH_ENTRY = UV_FS_EVENT_WATCH_ENTRY,
21  STAT = UV_FS_EVENT_STAT,
22  RECURSIVE = UV_FS_EVENT_RECURSIVE
23 };
24 
25 
26 enum class UVFsEvent: std::underlying_type_t<uv_fs_event> {
27  RENAME = UV_RENAME,
28  CHANGE = UV_CHANGE
29 };
30 
31 
32 }
33 
34 
40 struct FsEventEvent {
41  FsEventEvent(const char * pathname, Flags<details::UVFsEvent> events)
42  : filename{pathname}, flags{std::move(events)}
43  {}
44 
51  const char * filename;
52 
62 };
63 
64 
78 class FsEventHandle final: public Handle<FsEventHandle, uv_fs_event_t> {
79  static void startCallback(uv_fs_event_t *handle, const char *filename, int events, int status) {
80  FsEventHandle &fsEvent = *(static_cast<FsEventHandle*>(handle->data));
81  if(status) { fsEvent.publish(ErrorEvent{status}); }
82  else { fsEvent.publish(FsEventEvent{filename, static_cast<std::underlying_type_t<details::UVFsEvent>>(events)}); }
83  }
84 
85 public:
86  using Watch = details::UVFsEvent;
87  using Event = details::UVFsEventFlags;
88 
89  using Handle::Handle;
90 
95  bool init() {
96  return initialize(&uv_fs_event_init);
97  }
98 
116  void start(std::string path, Flags<Event> flags = Flags<Event>{}) {
117  invoke(&uv_fs_event_start, get(), &startCallback, path.data(), flags);
118  }
119 
137  void start(std::string path, Event flag) {
138  start(std::move(path), Flags<Event>{flag});
139  }
140 
144  void stop() {
145  invoke(&uv_fs_event_stop, get());
146  }
147 
152  std::string path() noexcept {
153  return details::tryRead(&uv_fs_event_getpath, get());
154  }
155 };
156 
157 
158 }
Flags< details::UVFsEvent > flags
Detected events all in one.
Definition: fs_event.hpp:61
bool init()
Initializes the handle.
Definition: fs_event.hpp:95
Handle base class.
Definition: handle.hpp:29
std::string path() noexcept
Gets the path being monitored.
Definition: fs_event.hpp:152
const char * filename
The path to the file being monitored.
Definition: fs_event.hpp:51
FsEventEvent event.
Definition: fs_event.hpp:40
void start(std::string path, Event flag)
Starts watching the specified path.
Definition: fs_event.hpp:137
The ErrorEvent event.
Definition: emitter.hpp:23
void stop()
Stops polling the file descriptor.
Definition: fs_event.hpp:144
The FsEventHandle handle.
Definition: fs_event.hpp:78
void start(std::string path, Flags< Event > flags=Flags< Event >{})
Starts watching the specified path.
Definition: fs_event.hpp:116
uvw default namespace.
Definition: async.hpp:11