summaryrefslogtreecommitdiff
path: root/include/libcamera/file_descriptor.h
blob: d514aac7697b74cff5438810178b8aa7bb51d718 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * file_descriptor.h - File descriptor wrapper
 */
#ifndef __LIBCAMERA_FILE_DESCRIPTOR_H__
#define __LIBCAMERA_FILE_DESCRIPTOR_H__

#include <memory>

namespace libcamera {

class FileDescriptor final
{
public:
	explicit FileDescriptor(const int &fd = -1);
	explicit FileDescriptor(int &&fd);
	FileDescriptor(const FileDescriptor &other);
	FileDescriptor(FileDescriptor &&other);
	~FileDescriptor();

	FileDescriptor &operator=(const FileDescriptor &other);
	FileDescriptor &operator=(FileDescriptor &&other);

	bool isValid() const { return fd_ != nullptr; }
	int fd() const { return fd_ ? fd_->fd() : -1; }
	FileDescriptor dup() const;

private:
	class Descriptor
	{
	public:
		Descriptor(int fd, bool duplicate);
		~Descriptor();

		int fd() const { return fd_; }

	private:
		int fd_;
	};

	std::shared_ptr<Descriptor> fd_;
};

} /* namespace libcamera */

#endif /* __LIBCAMERA_FILE_DESCRIPTOR_H__ */