Condy v1.7.0
C++ Asynchronous System Call Layer for Linux
Loading...
Searching...
No Matches
buffers.hpp
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <cassert>
11#include <cstddef>
12#include <cstring>
13#include <span>
14#include <string>
15#include <sys/mman.h>
16#include <sys/uio.h>
17#include <vector>
18
19namespace condy {
20
24class MutableBuffer {
25public:
26 using CondyBuffer = void;
27
28 MutableBuffer() = default;
29 MutableBuffer(void *data, size_t size) : data_(data), size_(size) {}
30
34 void *data() const noexcept { return data_; }
35
39 size_t size() const noexcept { return size_; }
40
41private:
42 void *data_ = nullptr;
43 size_t size_ = 0;
44};
45
49class ConstBuffer {
50public:
51 using CondyBuffer = void;
52
53 ConstBuffer() = default;
54 ConstBuffer(const void *data, size_t size) : data_(data), size_(size) {}
55
56 ConstBuffer(MutableBuffer buf) : data_(buf.data()), size_(buf.size()) {}
57 ConstBuffer &operator=(MutableBuffer buf) {
58 data_ = buf.data();
59 size_ = buf.size();
60 return *this;
61 }
62
66 const void *data() const noexcept { return data_; }
67
71 size_t size() const noexcept { return size_; }
72
73private:
74 const void *data_ = nullptr;
75 size_t size_ = 0;
76};
77
86inline MutableBuffer buffer(void *data, size_t size) noexcept {
87 return MutableBuffer(data, size);
88}
89
93inline ConstBuffer buffer(const void *data, size_t size) noexcept {
94 return ConstBuffer(data, size);
95}
96
100template <typename PodType, size_t N>
101inline MutableBuffer buffer(PodType (&arr)[N]) noexcept {
102 return MutableBuffer(static_cast<void *>(arr), sizeof(PodType) * N);
103}
104
108template <typename PodType, size_t N>
109inline ConstBuffer buffer(const PodType (&arr)[N]) noexcept {
110 return ConstBuffer(static_cast<const void *>(arr), sizeof(PodType) * N);
111}
112
116template <typename PodType, size_t N>
117inline MutableBuffer buffer(std::array<PodType, N> &arr) noexcept {
118 return MutableBuffer(static_cast<void *>(arr.data()), sizeof(PodType) * N);
119}
120
124template <typename PodType, size_t N>
125inline ConstBuffer buffer(const std::array<PodType, N> &arr) noexcept {
126 return ConstBuffer(static_cast<const void *>(arr.data()),
127 sizeof(PodType) * N);
128}
129
133template <typename PodType>
134inline MutableBuffer buffer(std::vector<PodType> &vec) noexcept {
135 return MutableBuffer(static_cast<void *>(vec.data()),
136 sizeof(PodType) * vec.size());
137}
138
142template <typename PodType>
143inline ConstBuffer buffer(const std::vector<PodType> &vec) noexcept {
144 return ConstBuffer(static_cast<const void *>(vec.data()),
145 sizeof(PodType) * vec.size());
146}
147
151inline MutableBuffer buffer(std::string &str) noexcept {
152 return MutableBuffer(static_cast<void *>(str.data()), str.size());
153}
154
158inline ConstBuffer buffer(const std::string &str) noexcept {
159 return ConstBuffer(static_cast<const void *>(str.data()), str.size());
160}
161
165inline ConstBuffer buffer(std::string_view strv) noexcept {
166 return ConstBuffer(static_cast<const void *>(strv.data()), strv.size());
167}
168
172inline MutableBuffer buffer(iovec &iov) noexcept {
173 return MutableBuffer(iov.iov_base, iov.iov_len);
174}
175
179template <typename PodType, size_t N>
180inline ConstBuffer buffer(std::span<const PodType, N> sp) noexcept {
181 return ConstBuffer(static_cast<const void *>(sp.data()),
182 sp.size() * sizeof(PodType));
183}
184
188template <typename PodType, size_t N>
189inline MutableBuffer buffer(std::span<PodType, N> sp) noexcept {
190 return MutableBuffer(static_cast<void *>(sp.data()),
191 sp.size() * sizeof(PodType));
192}
193
194} // namespace condy
Constant buffer.
Definition buffers.hpp:49
size_t size() const noexcept
Get the byte size of the buffer.
Definition buffers.hpp:71
const void * data() const noexcept
Get the data of the buffer.
Definition buffers.hpp:66
Mutable buffer.
Definition buffers.hpp:24
size_t size() const noexcept
Get the byte size of the buffer.
Definition buffers.hpp:39
void * data() const noexcept
Get the data of the buffer.
Definition buffers.hpp:34
The main namespace for the Condy library.
Definition condy.hpp:31
MutableBuffer buffer(void *data, size_t size) noexcept
Create a buffer object from various data sources.
Definition buffers.hpp:86