io/FileDescriptor: add [[nodiscard]]
This commit is contained in:
parent
3e862b85d4
commit
b63a794fbe
|
@ -23,7 +23,7 @@ static inline UniqueFileDescriptor
|
||||||
OpenFile(Path file, int flags, int mode)
|
OpenFile(Path file, int flags, int mode)
|
||||||
{
|
{
|
||||||
UniqueFileDescriptor fd;
|
UniqueFileDescriptor fd;
|
||||||
fd.Open(file.c_str(), flags, mode);
|
(void)fd.Open(file.c_str(), flags, mode);
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,10 @@ protected:
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
[[nodiscard]]
|
||||||
FileDescriptor() = default;
|
FileDescriptor() = default;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
explicit constexpr FileDescriptor(int _fd) noexcept:fd(_fd) {}
|
explicit constexpr FileDescriptor(int _fd) noexcept:fd(_fd) {}
|
||||||
|
|
||||||
constexpr bool operator==(FileDescriptor other) const noexcept {
|
constexpr bool operator==(FileDescriptor other) const noexcept {
|
||||||
|
@ -80,6 +83,7 @@ public:
|
||||||
fd = _fd;
|
fd = _fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
int Steal() noexcept {
|
int Steal() noexcept {
|
||||||
return std::exchange(fd, -1);
|
return std::exchange(fd, -1);
|
||||||
}
|
}
|
||||||
|
@ -88,37 +92,46 @@ public:
|
||||||
fd = -1;
|
fd = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
static constexpr FileDescriptor Undefined() noexcept {
|
static constexpr FileDescriptor Undefined() noexcept {
|
||||||
return FileDescriptor(-1);
|
return FileDescriptor(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
|
[[nodiscard]]
|
||||||
bool Open(FileDescriptor dir, const char *pathname,
|
bool Open(FileDescriptor dir, const char *pathname,
|
||||||
int flags, mode_t mode=0666) noexcept;
|
int flags, mode_t mode=0666) noexcept;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
bool Open(const char *pathname, int flags, mode_t mode=0666) noexcept;
|
bool Open(const char *pathname, int flags, mode_t mode=0666) noexcept;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
[[nodiscard]]
|
||||||
bool Open(const wchar_t *pathname, int flags, mode_t mode=0666) noexcept;
|
bool Open(const wchar_t *pathname, int flags, mode_t mode=0666) noexcept;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
bool OpenReadOnly(const char *pathname) noexcept;
|
bool OpenReadOnly(const char *pathname) noexcept;
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
|
[[nodiscard]]
|
||||||
bool OpenReadOnly(FileDescriptor dir,
|
bool OpenReadOnly(FileDescriptor dir,
|
||||||
const char *pathname) noexcept;
|
const char *pathname) noexcept;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
|
[[nodiscard]]
|
||||||
bool OpenNonBlocking(const char *pathname) noexcept;
|
bool OpenNonBlocking(const char *pathname) noexcept;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
|
[[nodiscard]]
|
||||||
static bool CreatePipe(FileDescriptor &r, FileDescriptor &w,
|
static bool CreatePipe(FileDescriptor &r, FileDescriptor &w,
|
||||||
int flags) noexcept;
|
int flags) noexcept;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
static bool CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept;
|
static bool CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
@ -126,6 +139,7 @@ public:
|
||||||
void DisableCloseOnExec() const noexcept {}
|
void DisableCloseOnExec() const noexcept {}
|
||||||
void SetBinaryMode() const noexcept;
|
void SetBinaryMode() const noexcept;
|
||||||
#else
|
#else
|
||||||
|
[[nodiscard]]
|
||||||
static bool CreatePipeNonBlock(FileDescriptor &r,
|
static bool CreatePipeNonBlock(FileDescriptor &r,
|
||||||
FileDescriptor &w) noexcept;
|
FileDescriptor &w) noexcept;
|
||||||
|
|
||||||
|
@ -159,11 +173,13 @@ public:
|
||||||
* @return the new file descriptor or UniqueFileDescriptor{}
|
* @return the new file descriptor or UniqueFileDescriptor{}
|
||||||
* on error
|
* on error
|
||||||
*/
|
*/
|
||||||
|
[[nodiscard]]
|
||||||
UniqueFileDescriptor Duplicate() const noexcept;
|
UniqueFileDescriptor Duplicate() const noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Duplicate the file descriptor onto the given file descriptor.
|
* Duplicate the file descriptor onto the given file descriptor.
|
||||||
*/
|
*/
|
||||||
|
[[nodiscard]]
|
||||||
bool Duplicate(FileDescriptor new_fd) const noexcept {
|
bool Duplicate(FileDescriptor new_fd) const noexcept {
|
||||||
return ::dup2(Get(), new_fd.Get()) != -1;
|
return ::dup2(Get(), new_fd.Get()) != -1;
|
||||||
}
|
}
|
||||||
|
@ -189,12 +205,15 @@ public:
|
||||||
/**
|
/**
|
||||||
* Rewind the pointer to the beginning of the file.
|
* Rewind the pointer to the beginning of the file.
|
||||||
*/
|
*/
|
||||||
|
[[nodiscard]]
|
||||||
bool Rewind() const noexcept;
|
bool Rewind() const noexcept;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
off_t Seek(off_t offset) const noexcept {
|
off_t Seek(off_t offset) const noexcept {
|
||||||
return lseek(Get(), offset, SEEK_SET);
|
return lseek(Get(), offset, SEEK_SET);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
off_t Skip(off_t offset) const noexcept {
|
off_t Skip(off_t offset) const noexcept {
|
||||||
return lseek(Get(), offset, SEEK_CUR);
|
return lseek(Get(), offset, SEEK_CUR);
|
||||||
}
|
}
|
||||||
|
@ -211,16 +230,19 @@ public:
|
||||||
off_t GetSize() const noexcept;
|
off_t GetSize() const noexcept;
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
|
[[nodiscard]]
|
||||||
ssize_t ReadAt(off_t offset,
|
ssize_t ReadAt(off_t offset,
|
||||||
void *buffer, std::size_t length) const noexcept {
|
void *buffer, std::size_t length) const noexcept {
|
||||||
return ::pread(fd, buffer, length, offset);
|
return ::pread(fd, buffer, length, offset);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
ssize_t Read(std::span<std::byte> dest) const noexcept {
|
ssize_t Read(std::span<std::byte> dest) const noexcept {
|
||||||
return ::read(fd, dest.data(), dest.size());
|
return ::read(fd, dest.data(), dest.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
ssize_t Read(void *buffer, std::size_t length) const noexcept {
|
ssize_t Read(void *buffer, std::size_t length) const noexcept {
|
||||||
return ::read(fd, buffer, length);
|
return ::read(fd, buffer, length);
|
||||||
}
|
}
|
||||||
|
@ -231,10 +253,12 @@ public:
|
||||||
*/
|
*/
|
||||||
void FullRead(std::span<std::byte> dest) const;
|
void FullRead(std::span<std::byte> dest) const;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
ssize_t Write(std::span<const std::byte> src) const noexcept {
|
ssize_t Write(std::span<const std::byte> src) const noexcept {
|
||||||
return ::write(fd, src.data(), src.size());
|
return ::write(fd, src.data(), src.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
ssize_t Write(const void *buffer, std::size_t length) const noexcept {
|
ssize_t Write(const void *buffer, std::size_t length) const noexcept {
|
||||||
return ::write(fd, buffer, length);
|
return ::write(fd, buffer, length);
|
||||||
}
|
}
|
||||||
|
@ -246,9 +270,13 @@ public:
|
||||||
void FullWrite(std::span<const std::byte> src) const;
|
void FullWrite(std::span<const std::byte> src) const;
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
|
[[nodiscard]]
|
||||||
int Poll(short events, int timeout) const noexcept;
|
int Poll(short events, int timeout) const noexcept;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
int WaitReadable(int timeout) const noexcept;
|
int WaitReadable(int timeout) const noexcept;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
int WaitWritable(int timeout) const noexcept;
|
int WaitWritable(int timeout) const noexcept;
|
||||||
|
|
||||||
[[gnu::pure]]
|
[[gnu::pure]]
|
||||||
|
|
|
@ -97,8 +97,7 @@ OssMixer::Close() noexcept
|
||||||
void
|
void
|
||||||
OssMixer::Open()
|
OssMixer::Open()
|
||||||
{
|
{
|
||||||
device_fd.OpenReadOnly(device);
|
if (!device_fd.OpenReadOnly(device))
|
||||||
if (!device_fd.IsDefined())
|
|
||||||
throw FmtErrno("failed to open {}", device);
|
throw FmtErrno("failed to open {}", device);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -48,7 +48,7 @@ EventPipe::Write() noexcept
|
||||||
assert(w.IsDefined());
|
assert(w.IsDefined());
|
||||||
|
|
||||||
static constexpr std::byte buffer[1]{};
|
static constexpr std::byte buffer[1]{};
|
||||||
w.Write(buffer);
|
(void)w.Write(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
Loading…
Reference in New Issue