Cbeam
Loading...
Searching...
No Matches
buffer.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
28#include <cbeam/error/runtime_error.hpp> // for cbeam::error:runtime_error
29#include <cbeam/logging/log_manager.hpp> // for CBEAM_LOG
30
31#include <stdlib.h> // for realloc, malloc, free
32
33#include <cstdint> // for uint8_t
34#include <cstring> // for std::memcpy, std::size_t
35#include <new> // for std::bad_alloc
36#include <string> // for std::operator+, std::allocator, std::to_string, std::char_traits
37#include <utility> // for std::swap
38
39#ifdef DBG
40 #include <string>
41#endif
42
44{
49 class buffer
50 {
51 public:
52 buffer() = default;
53
57 buffer(const std::size_t size, const std::size_t size_of_type = 1)
58 : _size{size * size_of_type}
59 , _buffer{(uint8_t*)malloc(_size)}
60 {
61 if (!_buffer)
62 {
63 CBEAM_LOG("cbeam::container::buffer: Out of RAM (" + std::to_string(_size) + ")");
64 throw std::bad_alloc();
65 }
66 }
67
69 virtual ~buffer() noexcept
70 {
72 }
73
75 explicit buffer(const void* address, const std::size_t length_of_buffer)
76 {
77 uint8_t* new_buffer = (uint8_t*)realloc(_buffer, length_of_buffer);
78 if (!new_buffer)
79 {
80 CBEAM_LOG("cbeam::container::buffer::append: Out of RAM (" + std::to_string(_size) + "+" + std::to_string(length_of_buffer) + ")");
81 throw std::bad_alloc();
82 }
83
84 _buffer = new_buffer;
85 _size = length_of_buffer;
86 std::memcpy(_buffer + _size, address, length_of_buffer);
87 }
88
90 buffer(const buffer& other)
91 {
92 *this = other;
93 }
94
96 virtual void append(const void* buffer_to_append, const std::size_t length_of_buffer)
97 {
98 uint8_t* new_buffer = (uint8_t*)realloc(_buffer, _size + length_of_buffer);
99 if (!new_buffer)
100 {
101 CBEAM_LOG("cbeam::container::buffer::append: Out of RAM (" + std::to_string(_size) + "+" + std::to_string(length_of_buffer) + ")");
102 throw std::bad_alloc();
103 }
104
105 _buffer = new_buffer;
106 std::memcpy(_buffer + _size, buffer_to_append, length_of_buffer);
107
108 _size += length_of_buffer;
109 }
110
112 virtual std::size_t size() const noexcept
113 {
114 return _size;
115 }
116
118 virtual buffer& operator=(const buffer& other)
119 {
120 if (this != &other)
121 {
122 if (other._buffer == nullptr)
123 {
124 throw cbeam::error::runtime_error("cbeam::container::buffer copy assignment operator has been passed a default constructed (therefore invalid) instance");
125 }
126
127 uint8_t* new_buffer = (uint8_t*)realloc(_buffer, other._size);
128 if (!new_buffer)
129 {
130 CBEAM_LOG("cbeam::container::buffer::append: Out of RAM (" + std::to_string(other._size) + ")");
131 throw std::bad_alloc();
132 }
133 _buffer = new_buffer;
134 std::memcpy(_buffer, other._buffer, other._size);
135 _size = other._size;
136 }
137 return *this;
138 }
139
141 virtual void* get() const noexcept
142 {
143 return _buffer;
144 }
145
147 virtual void reset() noexcept
148 {
149 free(_buffer);
150 _buffer = nullptr;
151 _size = 0;
152 }
153
156 virtual void swap(buffer& other) noexcept
157 {
158 std::swap(_buffer, other._buffer);
159 std::swap(_size, other._size);
160 }
161
162 protected:
163 std::size_t _size{0};
164 uint8_t* _buffer{nullptr};
165 };
166}
virtual void * get() const noexcept
return a pointer to the managed memory block
Definition buffer.hpp:141
virtual void swap(buffer &other) noexcept
Swaps the contents of this shared_buffer with another shared_buffer.
Definition buffer.hpp:156
virtual ~buffer() noexcept
Deallocate the memory block.
Definition buffer.hpp:69
virtual void append(const void *buffer_to_append, const std::size_t length_of_buffer)
append the given buffer to the end of the current buffer. If there is no current buffer yet,...
Definition buffer.hpp:96
buffer()=default
Will not create any memory block. Use append to create one or append bytes to an existing one.
buffer(const void *address, const std::size_t length_of_buffer)
Create an instance from a given memory block from address.
Definition buffer.hpp:75
std::size_t _size
Definition buffer.hpp:163
virtual void reset() noexcept
Resets the shared_buffer instance, deallocating the managed memory block.
Definition buffer.hpp:147
uint8_t * _buffer
Definition buffer.hpp:164
buffer(const buffer &other)
Copy construction means that the other buffer is deep copied to construct this instance.
Definition buffer.hpp:90
buffer(const std::size_t size, const std::size_t size_of_type=1)
Create a managed memory block with optional element size.
Definition buffer.hpp:57
virtual buffer & operator=(const buffer &other)
make a deep copy of the other buffer, overwriting the content of this buffer
Definition buffer.hpp:118
virtual std::size_t size() const noexcept
returns the size of the buffer in bytes
Definition buffer.hpp:112
A Cbeam-specific runtime error that also acts like std::runtime_error.
Definition runtime_error.hpp:46
#define CBEAM_LOG(s)
Logs a message using cbeam::logging::log_manager.
Definition log_manager.hpp:124
Offers advanced container types with unique approaches to stability and interprocess sharing....
Definition buffer.hpp:44