cpp-msgpack-light 0.3.0
A light library to serialize MessagePack.
Loading...
Searching...
No Matches
basic_binary_buffer.h
Go to the documentation of this file.
1/*
2 * Copyright 2024 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cstddef> // IWYU pragma: keep
23#include <cstdlib>
24#include <cstring>
25#include <new>
26#include <utility>
27
28namespace msgpack_light::details {
29
39public:
45 explicit basic_binary_buffer(std::size_t size)
46 : buffer_(static_cast<unsigned char*>(
47 std::malloc(prevent_unsafe_size(size)))),
48 size_(size) {
49 if (buffer_ == nullptr) {
50 throw std::bad_alloc();
51 }
52 }
53
60 : basic_binary_buffer(other.size()) {
61 std::memcpy(buffer_, other.buffer_, other.size_);
62 }
63
70 : buffer_(std::exchange(other.buffer_, nullptr)), size_(other.size_) {}
71
79 if (this == &other) {
80 return *this;
81 }
82 if (size_ != other.size_) {
83 resize(other.size());
84 }
85 std::memcpy(buffer_, other.buffer_, other.size_);
86 return *this;
87 }
88
96 swap(other);
97 return *this;
98 }
99
104
113 void resize(std::size_t new_size) {
114 auto* new_buffer = static_cast<unsigned char*>(
115 std::realloc(buffer_, prevent_unsafe_size(new_size)));
116 if (new_buffer == nullptr) {
117 throw std::bad_alloc();
118 }
119 buffer_ = new_buffer;
120 size_ = new_size;
121 }
122
128 void swap(basic_binary_buffer& other) noexcept {
129 std::swap(buffer_, other.buffer_);
130 std::swap(size_, other.size_);
131 }
132
138 [[nodiscard]] unsigned char* data() noexcept { return buffer_; }
139
145 [[nodiscard]] const unsigned char* data() const noexcept { return buffer_; }
146
152 [[nodiscard]] std::size_t size() const noexcept { return size_; }
153
154private:
163 [[nodiscard]] static std::size_t prevent_unsafe_size(std::size_t size) {
164 if (size == 0U) {
165 return 1U;
166 }
167 return size;
168 }
169
171 unsigned char* buffer_;
172
174 std::size_t size_;
175};
176
177} // namespace msgpack_light::details
178
179namespace std {
180
188template <>
189inline void
190swap( // NOLINT(readability-inconsistent-declaration-parameter-name)
191 // It's impossible to fix this because different implementation of STL can
192 // have different argument names.
195 instance1.swap(instance2);
196}
197
198} // namespace std
Class of basic buffers for binary data.
void swap(basic_binary_buffer &other) noexcept
Swap with another instance.
std::size_t size() const noexcept
Get the size of the buffer.
void resize(std::size_t new_size)
Change the size of the buffer.
basic_binary_buffer(const basic_binary_buffer &other)
Copy constructor.
const unsigned char * data() const noexcept
Get the pointer to the buffer.
basic_binary_buffer(basic_binary_buffer &&other) noexcept
Move constructor.
basic_binary_buffer & operator=(basic_binary_buffer &&other) noexcept
Move assignment operator.
basic_binary_buffer(std::size_t size)
Constructor.
unsigned char * data() noexcept
Get the pointer to the buffer.
static std::size_t prevent_unsafe_size(std::size_t size)
Change the input to a size which won't cause implementation-defined behaviors of std::malloc and std:...
basic_binary_buffer & operator=(const basic_binary_buffer &other)
Copy assignment operator.
Namespace of internal implementations.
Definition binary.h:35
STL namespace.
void swap(msgpack_light::details::basic_binary_buffer &instance1, msgpack_light::details::basic_binary_buffer &instance2) noexcept
Implementation of std::swap for msgpack_light::details::basic_binary_buffer.