qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
qb::io::async::event::file Struct Reference

Event for file and directory attribute change monitoring. More...

#include <file.h>

Inheritance diagram for qb::io::async::event::file:
Collaboration diagram for qb::io::async::event::file:

Public Types

using base_t = base<ev::stat>
 Base type alias for base<ev::stat>.
Public Types inherited from qb::io::async::event::base< ev::stat >
using ev_t
 Alias for the underlying libev event watcher type.

Public Member Functions

 file (ev::loop_ref loop)
 Constructor.
Public Member Functions inherited from qb::io::async::event::base< ev::stat >
 base (ev::loop_ref loop)
 Constructor.

Additional Inherited Members

Public Attributes inherited from qb::io::async::event::base< ev::stat >
IRegisteredKernelEvent_interface
 Pointer to the kernel event interface responsible for handling this event.
int _revents
 Stores the event flags (e.g., EV_READ, EV_WRITE) received from libev when the event triggers.

Detailed Description

Event for file and directory attribute change monitoring.

This event extends qb::io::async::event::base<ev::stat> and thus wraps an ev::stat watcher from libev. It is used by components like qb::io::async::file_watcher and qb::io::async::directory_watcher to notify when attributes of a monitored file or directory (e.g., size, modification time) have changed.

The ev::stat member (accessible as this->attr or event.attr in the handler) contains the current stat attributes, and event.prev contains the attributes from the previous check.

Usage Example:

class MyFileMonitor : public qb::io::async::file_watcher<MyFileMonitor> {
public:
MyFileMonitor(const std::string& path_to_watch, double interval = 1.0) {
this->start(path_to_watch, interval);
}
void on(qb::io::async::event::file &&event) {
if (event.attr.st_nlink == 0) {
LOG_INFO("File " << this->path() << " deleted or moved.");
this->disconnect(); // Stop watching
return;
}
if (event.attr.st_mtime != event.prev.st_mtime) {
LOG_INFO("File " << this->path() << " modified. New size: " << event.attr.st_size);
// Process file content if needed (e.g., using this->read_all())
}
// Access other stat information through event.attr and event.prev (ev_statdata)
}
};
CRTP base class for watching a single file for attribute changes and processing its contents.
Definition io.h:256
void start(std::string const &fpath, ev_tstamp ts=0.1) noexcept
Starts watching a file for attribute changes.
Definition io.h:329
void disconnect() noexcept
Stops watching the file.
Definition io.h:339
#define LOG_INFO(X)
Info-level log macro (no-op if QB_STDOUT_LOG is not defined)
Definition io.h:229

Constructor & Destructor Documentation

◆ file()

qb::io::async::event::file::file ( ev::loop_ref loop)
inlineexplicit

Constructor.

Parameters
loopReference to the libev event loop (ev::loop_ref) this event watcher will be associated with.