summaryrefslogtreecommitdiff
path: root/src/misc/FileDescriptor.hpp
blob: 75aeb738e89c21b3343c6a389eadd7fac204f91b (plain)
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
#pragma once

#include <fcntl.h>
class CFileDescriptor {
  public:
	CFileDescriptor() = default;
	explicit CFileDescriptor(int const fd);
	CFileDescriptor(CFileDescriptor &&) noexcept;
	CFileDescriptor &operator=(CFileDescriptor &&) noexcept;
	~CFileDescriptor();

	CFileDescriptor(const CFileDescriptor &)			= delete;
	CFileDescriptor &operator=(const CFileDescriptor &) = delete;

	bool operator==(const CFileDescriptor &rhs) const {
		return m_fd == rhs.m_fd;
	}

	bool			isValid() const;
	int				get() const;
	int				getFlags() const;
	bool			setFlags(int flags);
	int				take();
	void			reset();
	CFileDescriptor duplicate(int flags = F_DUPFD_CLOEXEC) const;

	bool isReadable() const;
	bool isClosed() const;

	static bool isReadable(int fd);
	static bool isClosed(int fd);

  private:
	int m_fd = -1;
};