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.

387 {
388 if (!uv_handle_) {
389 return;
390 }
391 BOOST_ASSERT_MSG(dataIsNull(uv_handle_.get()),
392 "don't drop an FsWatch while a watch() generator is "
393 "running (use stopWatch()!)");
394 uv_fs_event_stop(uv_handle_.get());
395}
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
385: uv_handle_{std::make_unique<uv_fs_event_t>()} {}

Member Function Documentation

◆ close()

void uvco::FsWatch::close ( )
453{ 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.

397 {
398 return createWithFlag(loop, path, {});
399}
static Promise< FsWatch > createWithFlag(const Loop &loop, std::string_view path, uv_fs_event_flags flags)
Definition fs.cc:406

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

402 {
403 return createWithFlag(loop, path, UV_FS_EVENT_RECURSIVE);
404}

◆ createWithFlag()

Promise< FsWatch > uvco::FsWatch::createWithFlag ( const Loop & loop,
std::string_view path,
uv_fs_event_flags flags )
staticprivate
408 {
409 FsWatch fsWatch;
410 std::unique_ptr<uv_fs_event_t> &uv_handle = fsWatch.uv_handle_;
411 const uv_status initStatus = uv_fs_event_init(loop.uvloop(), uv_handle.get());
412 if (initStatus != 0) {
413 throw UvcoException{
414 initStatus,
415 "uv_fs_event_init returned error while initializing FsWatch"};
416 }
417 const std::string path_str(path);
418 const int startStatus = uv_fs_event_start(uv_handle.get(), onFsWatcherEvent,
419 path_str.c_str(), flags);
420 if (startStatus != 0) {
421 uv_fs_event_stop(uv_handle.get());
422 // This works with the current Loop::~Loop implementation.
423 closeHandle(uv_handle.release());
424 throw UvcoException{
425 startStatus, "uv_fs_event_start returned error while starting FsWatch"};
426 }
427 co_return fsWatch;
428}
FsWatch(const FsWatch &)=delete
static void onFsWatcherEvent(uv_fs_event_t *handle, const char *path, int events, uv_status status)
Definition fs.cc:455
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
456 {
457 BOOST_ASSERT(events < 4);
458 if (dataIsNull(handle)) {
459 // No watcher generator set up yet.
460 return;
461 }
462 auto *awaiter = getData<FsWatchAwaiter_>(handle);
463 if (status == 0) {
464 awaiter->addEvent(FileEvent{std::string{path}, (uv_fs_event)events});
465 } else {
466 awaiter->addError(status);
467 }
468 awaiter->schedule();
469}
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)
447 {
448 auto *awaiter = getData<FsWatchAwaiter_>(uv_handle_.get());
449 awaiter->stop();
450 co_await watcher;
451}

◆ watch()

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

◆ watch_()

MultiPromise< FsWatch::FileEvent > uvco::FsWatch::watch_ ( )
private
437 {
438 FsWatchAwaiter_ awaiter;
439 setData(uv_handle_.get(), &awaiter);
440 ZeroAtExit<void> zeroAtExit{&uv_handle_->data};
441
442 while (co_await awaiter) {
443 co_yield awaiter.events_.get();
444 }
445}
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:320

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: