Lely core libraries  2.2.5
io.hpp
Go to the documentation of this file.
1 
22 #ifndef LELY_IO_IO_HPP_
23 #define LELY_IO_IO_HPP_
24 
25 #ifndef __cplusplus
26 #error "include <lely/io/io.h> for the C interface"
27 #endif
28 
29 #include <lely/util/c_type.hpp>
30 #include <lely/io/io.h>
31 
32 namespace lely {
33 
35 class IOHandle {
36  public:
37  operator io_handle_t() const noexcept { return m_handle; }
38 
39  IOHandle() noexcept : m_handle(IO_HANDLE_ERROR) {}
40 
41  IOHandle(const IOHandle& handle) noexcept
42  : m_handle(io_handle_acquire(handle.m_handle)) {}
43 
44  IOHandle(IOHandle&& handle) noexcept : m_handle(handle.m_handle) {
45  handle.m_handle = IO_HANDLE_ERROR;
46  }
47 
48  virtual ~IOHandle() { io_handle_release(m_handle); }
49 
50  IOHandle&
51  operator=(const IOHandle& handle) noexcept {
52  if (this != &handle) {
53  io_handle_t tmp = io_handle_acquire(handle.m_handle);
54  io_handle_release(m_handle);
55  m_handle = tmp;
56  }
57  return *this;
58  }
59 
60  IOHandle&
61  operator=(IOHandle&& handle) noexcept {
62  io_handle_release(m_handle);
63  m_handle = handle.m_handle;
64  handle.m_handle = IO_HANDLE_ERROR;
65  return *this;
66  }
67 
68  operator bool() const noexcept { return m_handle != IO_HANDLE_ERROR; }
69 
70  bool
71  unique() const noexcept {
72  return !!io_handle_unique(m_handle);
73  }
74 
75  int
76  close() noexcept {
77  io_handle_t handle = m_handle;
78  m_handle = IO_HANDLE_ERROR;
79  return io_close(handle);
80  }
81 
82  int
83  getType() const noexcept {
84  return io_get_type(*this);
85  }
86 
87 #ifdef _WIN32
88  HANDLE
89  getFd() const noexcept { return io_get_fd(*this); }
90 #else
91  int
92  getFd() const noexcept {
93  return io_get_fd(*this);
94  }
95 #endif
96 
97  int
98  getFlags() const noexcept {
99  return io_get_flags(*this);
100  }
101 
102  int
103  setFlags(int flags) noexcept {
104  return io_set_flags(*this, flags);
105  }
106 
107  ssize_t
108  read(void* buf, size_t nbytes) noexcept {
109  return io_read(*this, buf, nbytes);
110  }
111 
112  ssize_t
113  write(const void* buf, size_t nbytes) noexcept {
114  return io_write(*this, buf, nbytes);
115  }
116 
117  int
118  flush() noexcept {
119  return io_flush(*this);
120  }
121 
122  protected:
123  IOHandle(io_handle_t handle) noexcept : m_handle(handle) {}
124 
125  private:
126  io_handle_t m_handle;
127 };
128 
129 } // namespace lely
130 
131 #endif // !LELY_IO_IO_HPP_
This header file is part of the utilities library; it contains the C to C++ interface declarations.
An I/O device handle.
Definition: io.hpp:35
This is the public header file of the I/O library.
int io_close(io_handle_t handle)
Closes an I/O device.
Definition: io.c:74
int io_get_type(io_handle_t handle)
Returns the type of an I/O device (one of IO_TYPE_CAN, IO_TYPE_FILE, IO_TYPE_PIPE,...
Definition: io.c:87
ssize_t io_read(io_handle_t handle, void *buf, size_t nbytes)
Performs a read operation.
Definition: io.c:150
io_handle_t io_handle_acquire(io_handle_t handle)
Increments the reference count of an I/O device handle.
Definition: handle.c:32
struct io_handle * io_handle_t
An opaque I/O device handle type.
Definition: io.h:31
int io_handle_unique(io_handle_t handle)
Returns 1 if there is only a single reference to the specified I/O device handle, and 0 otherwise.
Definition: handle.c:67
int io_get_fd(io_handle_t handle)
Returns the native file descriptor of an I/O device.
Definition: io.c:99
int io_flush(io_handle_t handle)
Flushes the write buffer of a an I/O device.
Definition: io.c:184
int io_set_flags(io_handle_t handle, int flags)
Sets the flags of an I/O device.
Definition: io.c:124
#define IO_HANDLE_ERROR
The value of an invalid I/O device handle.
Definition: io.h:34
int io_get_flags(io_handle_t handle)
Obtains the flags of an I/O device.
Definition: io.c:110
ssize_t io_write(io_handle_t handle, const void *buf, size_t nbytes)
Performs a write operation.
Definition: io.c:167
void io_handle_release(io_handle_t handle)
Decrements the reference count of an I/O device handle.
Definition: handle.c:47
An I/O device handle.
Definition: handle.h:41