blob: 7179398fd70737a521c28c6b07a96c0ed0e8d0f0 [file] [log] [blame]
Matt Spinler4e97ebe2017-02-28 10:02:16 -06001#pragma once
2
3#include <fcntl.h>
Patrick Venturef78d9042018-11-01 15:39:53 -07004
Matt Spinler4e97ebe2017-02-28 10:02:16 -06005#include <string>
6
7namespace openpower
8{
Matt Spinlerc3bffed2017-03-10 09:05:30 -06009namespace util
Matt Spinler4e97ebe2017-02-28 10:02:16 -060010{
11
12/**
13 * Class to wrap a file descriptor.
14 *
15 * Opens the descriptor in the constructor, and
16 * then closes it when destroyed.
17 */
18class FileDescriptor
19{
Patrick Venturef78d9042018-11-01 15:39:53 -070020 public:
21 FileDescriptor() = delete;
22 FileDescriptor(const FileDescriptor&) = delete;
23 FileDescriptor(FileDescriptor&&) = default;
24 FileDescriptor& operator=(const FileDescriptor) = delete;
25 FileDescriptor& operator=(FileDescriptor&&) = default;
Matt Spinler4e97ebe2017-02-28 10:02:16 -060026
Patrick Venturef78d9042018-11-01 15:39:53 -070027 /**
28 * Creates a file descriptor by opening the device
29 * path passed in.
30 *
31 * @param path[in] - the device path that will be open
32 */
33 FileDescriptor(const std::string& path);
Matt Spinler4e97ebe2017-02-28 10:02:16 -060034
Patrick Venturef78d9042018-11-01 15:39:53 -070035 /**
36 * Closes the file.
37 */
38 ~FileDescriptor();
Matt Spinler4e97ebe2017-02-28 10:02:16 -060039
Patrick Venturef78d9042018-11-01 15:39:53 -070040 /**
41 * The method to access the file descriptor value
42 */
43 inline auto get() const
44 {
45 return fd;
46 }
Matt Spinler4e97ebe2017-02-28 10:02:16 -060047
Patrick Venturef78d9042018-11-01 15:39:53 -070048 private:
49 /**
50 * The actual file descriptor
51 */
52 int fd;
Matt Spinler4e97ebe2017-02-28 10:02:16 -060053};
54
Patrick Venturef78d9042018-11-01 15:39:53 -070055} // namespace util
56} // namespace openpower