uvco 0.1
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
uvco::FsWatch Class Reference

#include <fs.h>

Classes

struct  FileEvent
 
struct  FsWatchAwaiter_
 

Public Member Functions

 FsWatch (const FsWatch &)=delete
 
 FsWatch (FsWatch &&)=default
 
FsWatchoperator= (const FsWatch &)=delete
 
FsWatchoperator= (FsWatch &&)=default
 
 ~FsWatch ()
 
MultiPromise< FileEventwatch ()
 
Promise< void > stopWatch (MultiPromise< FileEvent > watcher)
 
Promise< void > close ()
 

Static Public Member Functions

static Promise< FsWatchcreate (const Loop &loop, std::string_view path)
 
static Promise< FsWatchcreateRecursive (const Loop &loop, std::string_view path)
 

Private Member Functions

 FsWatch ()
 
MultiPromise< FileEventwatch_ ()
 

Static Private Member Functions

static Promise< FsWatchcreateWithFlag (const Loop &loop, std::string_view path, uv_fs_event_flags flags)
 
static void onFsWatcherEvent (uv_fs_event_t *handle, const char *path, int events, uv_status status)
 

Private Attributes

std::unique_ptr< uv_fs_event_t > uv_handle_ {}
 

Constructor & Destructor Documentation

◆ FsWatch() [1/3]

uvco::FsWatch::FsWatch ( const FsWatch )
delete

◆ FsWatch() [2/3]

uvco::FsWatch::FsWatch ( FsWatch &&  )
default

◆ ~FsWatch()

uvco::FsWatch::~FsWatch ( )

Destructor. If the watch is still active, it is stopped. However, it's still required to call close() before dropping the FsWatch.

285 {
286 if (!uv_handle_) {
287 return;
288 }
289 BOOST_ASSERT_MSG(dataIsNull(uv_handle_.get()),
290 "don't drop an FsWatch while a watch() generator is "
291 "running (use stopWatch()!)");
292 uv_fs_event_stop(uv_handle_.get());
293}
std::unique_ptr< uv_fs_event_t > uv_handle_
Definition fs.h:190
bool dataIsNull(Handle *handle)
Definition internal_utils.h:66

◆ FsWatch() [3/3]

uvco::FsWatch::FsWatch ( )
private
283: uv_handle_{std::make_unique<uv_fs_event_t>()} {}

Member Function Documentation

◆ close()

Promise< void > uvco::FsWatch::close ( )
354 {
355 co_await closeHandle(uv_handle_.get());
356 uv_handle_.reset();
357}
Promise< void > closeHandle(T *handle, C closer)
Definition close.h:28

◆ create()

Promise< FsWatch > uvco::FsWatch::create ( const Loop loop,
std::string_view  path 
)
static

Create aa new FsWatch instance. The path is the file or directory to watch.

295 {
296 return createWithFlag(loop, path, {});
297}
static Promise< FsWatch > createWithFlag(const Loop &loop, std::string_view path, uv_fs_event_flags flags)
Definition fs.cc:304

◆ createRecursive()

Promise< FsWatch > uvco::FsWatch::createRecursive ( const Loop loop,
std::string_view  path 
)
static

Create a recursive watch. NOTE! This is only supported on macOS and Windows; see https://docs.libuv.org/en/v1.x/fs_event.html#c.uv_fs_event_start.

300 {
301 return createWithFlag(loop, path, UV_FS_EVENT_RECURSIVE);
302}

◆ createWithFlag()

Promise< FsWatch > uvco::FsWatch::createWithFlag ( const Loop loop,
std::string_view  path,
uv_fs_event_flags  flags 
)
staticprivate
306 {
307 FsWatch fsWatch;
308 uv_fs_event_t &uv_handle = *fsWatch.uv_handle_;
309 const uv_status initStatus = uv_fs_event_init(loop.uvloop(), &uv_handle);
310 if (initStatus != 0) {
311 throw UvcoException{
312 initStatus,
313 "uv_fs_event_init returned error while initializing FsWatch"};
314 }
315 const auto startStatus =
316 callWithNullTerminated<uv_status>(path, [&](std::string_view safePath) {
317 return uv_fs_event_start(&uv_handle, onFsWatcherEvent, safePath.data(),
318 flags);
319 });
320 if (startStatus != 0) {
321 uv_fs_event_stop(&uv_handle);
322 // This works with the current Loop::~Loop implementation.
323 co_await closeHandle(&uv_handle);
324 fsWatch.uv_handle_.reset();
325 throw UvcoException{
326 startStatus, "uv_fs_event_start returned error while starting FsWatch"};
327 }
328 co_return fsWatch;
329}
static void onFsWatcherEvent(uv_fs_event_t *handle, const char *path, int events, uv_status status)
Definition fs.cc:359
FsWatch()
Definition fs.cc:283
int uv_status
Result of a libuv operation, an errno error code.
Definition internal_utils.h:22

◆ onFsWatcherEvent()

void uvco::FsWatch::onFsWatcherEvent ( uv_fs_event_t *  handle,
const char *  path,
int  events,
uv_status  status 
)
staticprivate
360 {
361 BOOST_ASSERT(events < 4);
362 if (dataIsNull(handle)) {
363 // No watcher generator set up yet.
364 return;
365 }
366 auto *awaiter = getData<FsWatchAwaiter_>(handle);
367 if (status == 0) {
368 awaiter->addEvent(FileEvent{std::string{path}, (uv_fs_event)events});
369 } else {
370 awaiter->addError(status);
371 }
372 awaiter->schedule();
373}

◆ operator=() [1/2]

FsWatch & uvco::FsWatch::operator= ( const FsWatch )
delete

◆ operator=() [2/2]

FsWatch & uvco::FsWatch::operator= ( FsWatch &&  )
default

◆ stopWatch()

Promise< void > uvco::FsWatch::stopWatch ( MultiPromise< FileEvent watcher)
348 {
349 auto *awaiter = getData<FsWatchAwaiter_>(uv_handle_.get());
350 awaiter->stop();
351 co_await watcher;
352}

◆ watch()

MultiPromise< FsWatch::FileEvent > uvco::FsWatch::watch ( )
331 {
332 if (!dataIsNull(uv_handle_.get())) {
333 throw UvcoException(UV_EBUSY, "FsWatch::watch() is already active!");
334 }
335 return watch_();
336}
MultiPromise< FileEvent > watch_()
Definition fs.cc:338

◆ watch_()

MultiPromise< FsWatch::FileEvent > uvco::FsWatch::watch_ ( )
private
338 {
339 FsWatchAwaiter_ awaiter;
340 setData(uv_handle_.get(), &awaiter);
341 ZeroAtExit<void> zeroAtExit{&uv_handle_->data};
342
343 while (co_await awaiter) {
344 co_yield awaiter.events_.get();
345 }
346}
void setData(Handle *handle, Data *data)
Definition internal_utils.h:55

Member Data Documentation

◆ uv_handle_

std::unique_ptr<uv_fs_event_t> uvco::FsWatch::uv_handle_ {}
private
190{};

The documentation for this class was generated from the following files: