Condy v1.8
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
11#include <cassert>
12#include <cstddef>
13#include <cstring>
14#include <utility>
15
16namespace condy {
17namespace detail {
18
19template <typename BufferPool> struct ManagedBuffer {
20public:
21 using CondyBuffer = void;
22
23 ManagedBuffer() = default;
24 ManagedBuffer(void *data, size_t size, BufferPool *pool)
25 : data_(data), size_(size), pool_(pool) {}
26 ManagedBuffer(ManagedBuffer &&other) noexcept
27 : data_(std::exchange(other.data_, nullptr)),
28 size_(std::exchange(other.size_, 0)),
29 pool_(std::exchange(other.pool_, nullptr)) {}
30 ManagedBuffer &operator=(ManagedBuffer &&other) noexcept {
31 if (this != &other) {
32 reset();
33 data_ = std::exchange(other.data_, nullptr);
34 size_ = std::exchange(other.size_, 0);
35 pool_ = std::exchange(other.pool_, nullptr);
36 }
37 return *this;
38 }
39
40 ~ManagedBuffer() { reset(); }
41
42 CONDY_DELETE_COPY(ManagedBuffer);
43
44public:
48 void *data() const noexcept { return data_; }
49
53 size_t size() const noexcept { return size_; }
54
58 void reset() noexcept {
59 if (pool_ != nullptr) {
60 pool_->add_buffer_back(data_, size_);
61 }
62 data_ = nullptr;
63 size_ = 0;
64 pool_ = nullptr;
65 }
66
70 bool owns_buffer() const noexcept { return pool_ != nullptr; }
71
72private:
73 void *data_ = nullptr;
74 size_t size_ = 0;
75 BufferPool *pool_ = nullptr;
76};
77
78} // namespace detail
79} // namespace condy
The main namespace for the Condy library.
Definition condy.hpp:37
Internal utility classes and functions used by Condy.