uvw  1.3.0
fs_poll.hpp
1 #pragma once
2 
3 
4 #include <utility>
5 #include <string>
6 #include <memory>
7 #include <chrono>
8 #include <uv.h>
9 #include "handle.hpp"
10 #include "util.hpp"
11 #include "loop.hpp"
12 
13 
14 namespace uvw {
15 
16 
22 struct FsPollEvent {
23  explicit FsPollEvent(Stat previous, Stat current) noexcept
24  : prev{std::move(previous)}, curr{std::move(current)}
25  {}
26 
29 };
30 
31 
41 class FsPollHandle final: public Handle<FsPollHandle, uv_fs_poll_t> {
42  static void startCallback(uv_fs_poll_t *handle, int status, const uv_stat_t *prev, const uv_stat_t *curr) {
43  FsPollHandle &fsPoll = *(static_cast<FsPollHandle*>(handle->data));
44  if(status) { fsPoll.publish(ErrorEvent{status}); }
45  else { fsPoll.publish(FsPollEvent{ *prev, *curr }); }
46  }
47 
48 public:
49  using Time = std::chrono::duration<unsigned int, std::milli>;
50 
51  using Handle::Handle;
52 
57  bool init() {
58  return initialize(&uv_fs_poll_init);
59  }
60 
69  void start(std::string file, Time interval) {
70  invoke(&uv_fs_poll_start, get(), &startCallback, file.data(), interval.count());
71  }
72 
76  void stop() {
77  invoke(&uv_fs_poll_stop, get());
78  }
79 
85  std::string path() noexcept {
86  return details::tryRead(&uv_fs_poll_getpath, get());
87  }
88 };
89 
90 
91 }
The FsPollHandle handle.
Definition: fs_poll.hpp:41
void start(std::string file, Time interval)
Starts the handle.
Definition: fs_poll.hpp:69
FsPollEvent event.
Definition: fs_poll.hpp:22
Handle base class.
Definition: handle.hpp:29
bool init()
Initializes the handle.
Definition: fs_poll.hpp:57
void stop()
Stops the handle.
Definition: fs_poll.hpp:76
The ErrorEvent event.
Definition: emitter.hpp:23
std::string path() noexcept
Gets the path being monitored by the handle.
Definition: fs_poll.hpp:85
uv_stat_t Stat
Definition: util.hpp:195
uvw default namespace.
Definition: async.hpp:11