1 #ifndef SPONGE_LIBSPONGE_FILE_DESCRIPTOR_HH
2 #define SPONGE_LIBSPONGE_FILE_DESCRIPTOR_HH
A non-owning temporary view (similar to std::string_view) of a discontiguous string.
A handle on a kernel file descriptor.
FDWrapper(FDWrapper &&other)=delete
bool _eof
Flag indicating whether FDWrapper::_fd is at EOF.
bool _closed
Flag indicating whether FDWrapper::_fd has been closed.
FDWrapper(const int fd)
Construct from a file descriptor number returned by the kernel.
void close()
Calls close(2) on FDWrapper::_fd.
int _fd
The file descriptor number returned by the kernel.
unsigned _write_count
The numberof times FDWrapper::_fd has been written.
unsigned _read_count
The number of times FDWrapper::_fd has been read.
FDWrapper(const FDWrapper &other)=delete
~FDWrapper()
Closes the file descriptor upon destruction.
FDWrapper & operator=(const FDWrapper &other)=delete
FDWrapper & operator=(FDWrapper &&other)=delete
A reference-counted handle to a file descriptor.
void close()
Close the underlying file descriptor.
~FileDescriptor()=default
Free the std::shared_ptr; the FDWrapper destructor calls close() when the refcount goes to zero.
unsigned int read_count() const
number of reads
bool closed() const
closed flag state
FileDescriptor(FileDescriptor &&other)=default
move construction is allowed
FileDescriptor duplicate() const
Copy a FileDescriptor explicitly, increasing the FDWrapper refcount.
unsigned int write_count() const
number of writes
bool eof() const
EOF flag state.
int fd_num() const
underlying descriptor number
FileDescriptor & operator=(const FileDescriptor &other)=delete
copy assignment is forbidden
void register_read()
increment read count
void read(std::string &str, const size_t limit=std::numeric_limits< size_t >::max())
Read up to limit bytes into str (caller can allocate storage)
std::shared_ptr< FDWrapper > _internal_fd
A reference-counted handle to a shared FDWrapper.
void set_blocking(const bool blocking_state)
Set blocking(true) or non-blocking(false)
size_t write(const char *str, const bool write_all=true)
Write a string, possibly blocking until all is written.
std::string read(const size_t limit=std::numeric_limits< size_t >::max())
Read up to limit bytes.
FileDescriptor & operator=(FileDescriptor &&other)=default
move assignment is allowed
FileDescriptor(const FileDescriptor &other)=delete
copy construction is forbidden
FileDescriptor(std::shared_ptr< FDWrapper > other_shared_ptr)
Private constructor used by duplicate()
void register_write()
increment write count
size_t write(const std::string &str, const bool write_all=true)
Write a string, possibly blocking until all is written.