libcppwrap
A collection of C++ wrappers for native APIs
Loading...
Searching...
No Matches
posix.hpp
Go to the documentation of this file.
1//
2// libcppwrap - A collection of C++ wrappers for native APIs
3// Copyright (C) 2021-2023 David A. Norris <danorris@gmail.com>
4// Published under the MIT license - https://opensource.org/licenses/MIT
5//
6
7#pragma once
8
9#include <cstddef>
10#include <utility>
11
12#include <w/assert.hpp>
13#include <w/handle.hpp>
14
15#include <fcntl.h>
16#include <unistd.h>
17#include <sys/mman.h>
18#include <sys/uio.h>
19
20namespace w
21{
25 typedef w::handle<int, -1, ::close> fd;
26
32 {
33 void *address;
41 constexpr memory_region(void *address, std::size_t length) noexcept : address(address), length(length) { }
42
46 constexpr memory_region() noexcept : memory_region(nullptr, 0) { }
47
53 constexpr bool operator!=(const memory_region& rhs) const noexcept { return rhs.address != address || rhs.length != length; }
54 };
55
56 namespace detail
57 {
58 inline void munmap(const memory_region& region)
59 {
60 ::munmap(region.address, region.length);
61 }
62 }
63
64#if (__cplusplus >= 201709L)
68 typedef w::handle<memory_region, memory_region { }, detail::munmap> mmap_handle;
69#endif
70
79 int fcntl(int fd, int cmd);
80
91 template <typename Argument>
92 int fcntl(int fd, int cmd, const Argument& arg)
93 {
94 return w::throw_if_eq(
95 ::fcntl(fd, cmd, arg),
96 -1,
97 "file descriptor control failed");
98 }
99
109 int ioctl(int fd, unsigned long request, void *arg);
110
123 int ioctl(int fd, unsigned long request, const void *arg);
124
135 template <typename Argument>
136 int ioctl(int fd, unsigned long request, Argument& arg)
137 {
138 return w::ioctl(fd, request, reinterpret_cast<void *>(&arg));
139 }
140
154 template <typename Argument>
155 int ioctl(int fd, unsigned long request, const Argument& arg)
156 {
157 return w::ioctl(fd, request, reinterpret_cast<const void *>(&arg));
158 }
159
169 template <typename Argument>
170 Argument ioctl(int fd, unsigned long request)
171 {
172 Argument arg;
173 w::ioctl(fd, request, reinterpret_cast<void *>(&arg));
174 return arg;
175 }
176
186 std::size_t lseek(int fd, off_t offset, int whence);
187
188#if (__cplusplus >= 201709L)
201 w::mmap_handle mmap(void *address, std::size_t length, int prot, int flags, int fd, off_t offset);
202#endif
203
212 w::fd open(const char *pathname, int flags);
213
223 w::fd open(const char *pathname, int flags, mode_t mode);
224
232
242 std::size_t read(int fd, void *buf, std::size_t count);
243
253 std::size_t readv(int fd, const struct iovec *iov, int iovcnt);
254
264 std::size_t write(int fd, const void *buf, std::size_t count);
265
275 std::size_t writev(int fd, const struct iovec *iov, int iovcnt);
276}
An RAII handle type which owns an opaque resource of a specified type, and calls a designated functio...
Definition: handle.hpp:40
Definition: assert.hpp:13
w::handle< int, -1, ::close > fd
An RAII util::handle type for POSIX file descriptors.
Definition: posix.hpp:25
std::size_t read(int fd, void *buf, std::size_t count)
Reads data from a file descriptor.
Definition: posix.cpp:89
T throw_if_eq(T value, T x, const char *message)
Throw a std::system_error with errno if value == x, otherwise return value.
Definition: assert.hpp:25
std::pair< w::fd, w::fd > pipe()
Creates a pipe.
Definition: posix.cpp:77
w::fd open(const char *pathname, int flags)
Opens and possibly creates a file.
Definition: posix.cpp:61
std::size_t write(int fd, const void *buf, std::size_t count)
Writes data to a file descriptor.
Definition: posix.cpp:107
int ioctl(int fd, unsigned long request, void *arg)
Controls a device.
Definition: posix.cpp:26
int fcntl(int fd, int cmd)
Controls a file descriptor.
Definition: posix.cpp:18
std::size_t readv(int fd, const struct iovec *iov, int iovcnt)
Reads data from a file descriptor.
Definition: posix.cpp:98
std::size_t writev(int fd, const struct iovec *iov, int iovcnt)
Writes data to a file descriptor.
Definition: posix.cpp:116
std::size_t lseek(int fd, off_t offset, int whence)
Sets the position of a file pointer for a file descriptor.
Definition: posix.cpp:42
A structure holding details about a memory-mapped file or device.
Definition: posix.hpp:32
constexpr memory_region() noexcept
Constructs an empty memory region.
Definition: posix.hpp:46
constexpr bool operator!=(const memory_region &rhs) const noexcept
Compares two memory regions.
Definition: posix.hpp:53
std::size_t length
The length of the mapping in bytes.
Definition: posix.hpp:34
constexpr memory_region(void *address, std::size_t length) noexcept
Constructs a memory region.
Definition: posix.hpp:41
void * address
The (actual, not requested) base address of the mapping.
Definition: posix.hpp:33