uvco 0.1
Loading...
Searching...
No Matches
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)
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.

390 {
391 if (!uv_handle_) {
392 return;
393 }
394 BOOST_ASSERT_MSG(dataIsNull(uv_handle_.get()),
395 "don't drop an FsWatch while a watch() generator is "
396 "running (use stopWatch()!)");
397 uv_fs_event_stop(uv_handle_.get());
398}
std::unique_ptr< uv_fs_event_t > uv_handle_
Definition fs.h:185
bool dataIsNull(Handle *handle)
Check if handle data is null.
Definition internal_utils.h:101

◆ FsWatch() [3/3]

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

Member Function Documentation

◆ close()

void uvco::FsWatch::close ( )
456{ closeHandle(uv_handle_.release()); }
void closeHandle(Handle *handle, void(*closer)(CloserArg *, void(*)(uv_handle_t *)))
Definition close.h:37

◆ 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.

400 {
401 return createWithFlag(loop, path, {});
402}
static Promise< FsWatch > createWithFlag(const Loop &loop, std::string_view path, uv_fs_event_flags flags)
Definition fs.cc:409

◆ 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.

405 {
406 return createWithFlag(loop, path, UV_FS_EVENT_RECURSIVE);
407}

◆ createWithFlag()

Promise< FsWatch > uvco::FsWatch::createWithFlag ( const Loop & loop,
std::string_view path,
uv_fs_event_flags flags )
staticprivate
411 {
412 FsWatch fsWatch;
413 std::unique_ptr<uv_fs_event_t> &uv_handle = fsWatch.uv_handle_;
414 const uv_status initStatus = uv_fs_event_init(loop.uvloop(), uv_handle.get());
415 if (initStatus != 0) {
416 throw UvcoException{
417 initStatus,
418 "uv_fs_event_init returned error while initializing FsWatch"};
419 }
420 const std::string path_str(path);
421 const int startStatus = uv_fs_event_start(uv_handle.get(), onFsWatcherEvent,
422 path_str.c_str(), flags);
423 if (startStatus != 0) {
424 uv_fs_event_stop(uv_handle.get());
425 // This works with the current Loop::~Loop implementation.
426 closeHandle(uv_handle.release());
427 throw UvcoException{
428 startStatus, "uv_fs_event_start returned error while starting FsWatch"};
429 }
430 co_return fsWatch;
431}
FsWatch(const FsWatch &)=delete
static void onFsWatcherEvent(uv_fs_event_t *handle, const char *path, int events, uv_status status)
Definition fs.cc:458
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
459 {
460 BOOST_ASSERT(events < 4);
461 if (dataIsNull(handle)) {
462 // No watcher generator set up yet.
463 return;
464 }
465 auto *awaiter = getData<FsWatchAwaiter_>(handle);
466 if (status == 0) {
467 awaiter->addEvent(FileEvent{std::string{path}, (uv_fs_event)events});
468 } else {
469 awaiter->addError(status);
470 }
471 awaiter->schedule();
472}
Into * getData(const Handle *handle)
Obtain data pointer set on handle with nullptr check and type cast.
Definition internal_utils.h:42
Definition fs.h:142

◆ 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)
450 {
451 auto *awaiter = getData<FsWatchAwaiter_>(uv_handle_.get());
452 awaiter->stop();
453 co_await watcher;
454}

◆ watch()

MultiPromise< FsWatch::FileEvent > uvco::FsWatch::watch ( )
433 {
434 if (!dataIsNull(uv_handle_.get())) {
435 throw UvcoException(UV_EBUSY, "FsWatch::watch() is already active!");
436 }
437 return watch_();
438}
MultiPromise< FileEvent > watch_()
Definition fs.cc:440

◆ watch_()

MultiPromise< FsWatch::FileEvent > uvco::FsWatch::watch_ ( )
private
440 {
441 FsWatchAwaiter_ awaiter;
442 setData(uv_handle_.get(), &awaiter);
443 ZeroAtExit<void> zeroAtExit{&uv_handle_->data};
444
445 while (co_await awaiter) {
446 co_yield awaiter.events_.get();
447 }
448}
void setData(Handle *handle, Data *data)
Obtain data pointer set on request with type cast. Data may be nullptr.
Definition internal_utils.h:77
Definition fs.cc:323

Member Data Documentation

◆ uv_handle_

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

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