uvco 0.1
Loading...
Searching...
No Matches
uvco::File Class Reference

A file descriptor. More...

#include <fs.h>

Public Member Functions

 File (const File &)=delete
 File (File &&other) noexcept
Fileoperator= (const File &)=delete
Fileoperator= (File &&other) noexcept
 ~File ()
Promise< size_t > read (std::string &buffer, int64_t offset=-1)
Promise< size_t > write (std::string_view buffer, int64_t offset=-1)
uv_file file () const
 Access the libuv file handle.
void close ()
 Close a file.

Static Public Member Functions

static Promise< Fileopen (const Loop &loop, std::string_view path, int flags=0, int mode=0644)
static Promise< void > unlink (const Loop &loop, std::string_view path)

Private Member Functions

 File (uv_loop_t *loop, uv_file file)

Private Attributes

uv_loop_t * loop_
uv_file file_

Detailed Description

A file descriptor.

Constructor & Destructor Documentation

◆ File() [1/3]

uvco::File::File ( const File & )
delete

◆ File() [2/3]

uvco::File::File ( File && other)
noexcept
231 {
232 if (this == &other) {
233 return;
234 }
235 loop_ = other.loop_;
236 file_ = other.file_;
237 other.file_ = -1;
238}
uv_loop_t * loop_
Definition fs.h:113
uv_file file_
Definition fs.h:114

◆ ~File()

uvco::File::~File ( )
230{ close(); }
void close()
Close a file.
Definition fs.cc:316

◆ File() [3/3]

uvco::File::File ( uv_loop_t * loop,
uv_file file )
inlineprivate
111: loop_{loop}, file_(file) {}
uv_file file() const
Access the libuv file handle.
Definition fs.cc:311

Member Function Documentation

◆ close()

void uvco::File::close ( )

Close a file.

316 {
317 if (file_ != -1) {
318 uv_fs_close(loop_, new uv_fs_t{}, file(), FileOpAwaiter_::uvCallback());
319 }
320 file_ = -1;
321}

◆ file()

uv_file uvco::File::file ( ) const
nodiscard

Access the libuv file handle.

311 {
312 BOOST_ASSERT(file_ >= 0);
313 return file_;
314}

◆ open()

Promise< File > uvco::File::open ( const Loop & loop,
std::string_view path,
int flags = 0,
int mode = 0644 )
static

Open a file asynchronously; flags and mode are optional and analogous to open(2).

252 {
253 FileOpAwaiter_ awaiter;
254
255 const std::string path_str(path);
256 uv_fs_open(loop.uvloop(), &awaiter.req(), path_str.c_str(), flags, mode,
257 FileOpAwaiter_::uvCallback());
258
259 co_await awaiter;
260
261 const auto fileDesc = static_cast<uv_file>(awaiter.req().result);
262
263 co_return File{loop.uvloop(), fileDesc};
264}
File(const File &)=delete

◆ operator=() [1/2]

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

◆ operator=() [2/2]

File & uvco::File::operator= ( File && other)
noexcept
240 {
241 if (this == &other) {
242 return *this;
243 }
244 close();
245 loop_ = other.loop_;
246 file_ = other.file_;
247 other.file_ = -1;
248 return *this;
249}

◆ read()

Promise< size_t > uvco::File::read ( std::string & buffer,
int64_t offset = -1 )

Read up to buffer.size() bytes into that buffer, starting at offset (if offset >= 0) or at the current file position.

The provided buffer must be pre-sized to the desired number of bytes to read. The number of bytes read will be at most buffer.length(). After the operation, buffer is resized to the actual number of bytes read.

Parameters
bufferA pre-sized string to read data into.
offsetThe offset in the file to read from. Defaults to -1 (current file position).
Returns
The number of bytes read.

TODO: generalize to any buffer type.

277 {
278 FileOpAwaiter_ awaiter;
279 size_t result = 0;
280
281 std::array<uv_buf_t, 1> bufs{};
282 bufs[0] = uv_buf_init(buffer.data(), buffer.length());
283
284 uv_fs_read(loop_, &awaiter.req(), file(), bufs.data(), 1, offset,
285 FileOpAwaiter_::uvCallback());
286
287 co_await awaiter;
288
289 result = awaiter.req().result;
290 buffer.resize(result);
291 co_return result;
292}

◆ unlink()

Promise< void > uvco::File::unlink ( const Loop & loop,
std::string_view path )
static
266 {
267 FileOpAwaiter_ awaiter;
268
269 const std::string path_str(path);
270 uv_fs_unlink(loop.uvloop(), &awaiter.req(), path_str.c_str(),
271 FileOpAwaiter_::uvCallback());
272
273 co_await awaiter;
274 co_return;
275}

◆ write()

Promise< size_t > uvco::File::write ( std::string_view buffer,
int64_t offset = -1 )

Write contents of buffer to the underlying file at offset.

TODO: generalize to any buffer type.

294 {
295 FileOpAwaiter_ awaiter;
296 size_t result = 0;
297
298 std::array<uv_buf_t, 1> bufs{};
299 // Unfortunately necessary: const_cast
300 bufs[0] = uv_buf_init(const_cast<char *>(buffer.data()), buffer.length());
301
302 uv_fs_write(loop_, &awaiter.req(), file(), bufs.data(), 1, offset,
303 FileOpAwaiter_::uvCallback());
304
305 co_await awaiter;
306
307 result = awaiter.req().result;
308 co_return result;
309}

Member Data Documentation

◆ file_

uv_file uvco::File::file_
private

◆ loop_

uv_loop_t* uvco::File::loop_
private

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