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
228 {
229 if (this == &other) {
230 return;
231 }
232 loop_ = other.loop_;
233 file_ = other.file_;
234 other.file_ = -1;
235}
uv_loop_t * loop_
Definition fs.h:113
uv_file file_
Definition fs.h:114

◆ ~File()

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

◆ 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:308

Member Function Documentation

◆ close()

void uvco::File::close ( )

Close a file.

313 {
314 if (file_ != -1) {
315 uv_fs_close(loop_, new uv_fs_t{}, file(), FileOpAwaiter_::uvCallback());
316 }
317 file_ = -1;
318}

◆ file()

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

Access the libuv file handle.

308 {
309 BOOST_ASSERT(file_ >= 0);
310 return file_;
311}

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

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

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

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

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

◆ unlink()

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

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

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

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: