uvco 0.1
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
uvco::File Class Reference

A file descriptor. More...

#include <fs.h>

Public Member Functions

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.
 
Promise< void > close ()
 Close a file asynchronously.
 

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()

uvco::File::File ( uv_loop_t *  loop,
uv_file  file 
)
inlineprivate
99: loop_{loop}, file_(file) {}
uv_loop_t * loop_
Definition fs.h:101
uv_file file_
Definition fs.h:102
uv_file file() const
Access the libuv file handle.
Definition fs.cc:267

Member Function Documentation

◆ close()

Promise< void > uvco::File::close ( )

Close a file asynchronously.

272 {
273 FileOpAwaiter_ awaiter;
274 auto &req = awaiter.req();
275
276 uv_fs_close(loop_, &req, file(), FileOpAwaiter_::uvCallback());
277
278 file_ = -1;
279
280 co_await awaiter;
281}

◆ file()

uv_file uvco::File::file ( ) const

Access the libuv file handle.

267 {
268 BOOST_ASSERT(file_ >= 0);
269 return file_;
270}

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

208 {
209 FileOpAwaiter_ awaiter;
210
211 uv_fs_open(loop.uvloop(), &awaiter.req(), path.data(), flags, mode,
212 FileOpAwaiter_::uvCallback());
213
214 co_await awaiter;
215
216 const auto fileDesc = static_cast<uv_file>(awaiter.req().result);
217
218 co_return File{loop.uvloop(), fileDesc};
219}
File(uv_loop_t *loop, uv_file file)
Definition fs.h:99

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

buffer is resized to the number of bytes read, if it could not be filled completely; The number of bytes read is also returned.

TODO: generalize to any buffer type.

231 {
232 FileOpAwaiter_ awaiter;
233 size_t result = 0;
234
235 std::array<uv_buf_t, 1> bufs{};
236 bufs[0].base = buffer.data();
237 bufs[0].len = buffer.length();
238
239 uv_fs_read(loop_, &awaiter.req(), file(), bufs.data(), 1, offset,
240 FileOpAwaiter_::uvCallback());
241
242 co_await awaiter;
243
244 result = awaiter.req().result;
245 buffer.resize(result);
246 co_return result;
247}

◆ unlink()

Promise< void > uvco::File::unlink ( const Loop loop,
std::string_view  path 
)
static
221 {
222 FileOpAwaiter_ awaiter;
223
224 uv_fs_unlink(loop.uvloop(), &awaiter.req(), path.data(),
225 FileOpAwaiter_::uvCallback());
226
227 co_await awaiter;
228 co_return;
229}

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

249 {
250 FileOpAwaiter_ awaiter;
251 size_t result = 0;
252
253 std::array<uv_buf_t, 1> bufs{};
254 // Unfortunately necessary: const_cast
255 bufs[0].base = const_cast<char *>(buffer.data());
256 bufs[0].len = buffer.length();
257
258 uv_fs_write(loop_, &awaiter.req(), file(), bufs.data(), 1, offset,
259 FileOpAwaiter_::uvCallback());
260
261 co_await awaiter;
262
263 result = awaiter.req().result;
264 co_return result;
265}

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: