-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathfile_descriptor.hh
50 lines (38 loc) · 1.39 KB
/
file_descriptor.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef FILE_DESCRIPTOR_HH
#define FILE_DESCRIPTOR_HH
#include <limits>
#include <string>
/* Unix file descriptors (sockets, files, etc.) */
class FileDescriptor
{
private:
int fd_;
bool eof_;
unsigned int read_count_, write_count_;
/* attempt to write a portion of a string */
std::string::const_iterator write( const std::string::const_iterator & begin,
const std::string::const_iterator & end );
protected:
void register_read() { read_count_++; }
void register_write() { write_count_++; }
void set_eof() { eof_ = true; }
public:
/* construct from fd number */
FileDescriptor( const int fd );
/* move constructor */
FileDescriptor( FileDescriptor && other );
/* destructor */
virtual ~FileDescriptor();
/* accessors */
const int & fd_num() const { return fd_; }
const bool & eof() const { return eof_; }
unsigned int read_count() const { return read_count_; }
unsigned int write_count() const { return write_count_; }
/* read and write methods */
std::string read( const size_t limit = std::numeric_limits<size_t>::max() );
std::string::const_iterator write( const std::string & buffer, const bool write_all = true );
/* forbid copying FileDescriptor objects or assigning them */
FileDescriptor( const FileDescriptor & other ) = delete;
const FileDescriptor & operator=( const FileDescriptor & other ) = delete;
};
#endif /* FILE_DESCRIPTOR_HH */