cpp-msgpack-light
0.3.0
A light library to serialize MessagePack.
Toggle main menu visibility
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
28
namespace
msgpack_light::details
{
29
38
class
basic_binary_buffer
{
39
public
:
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
59
basic_binary_buffer
(
const
basic_binary_buffer
& other)
60
:
basic_binary_buffer
(other.
size
()) {
61
std::memcpy(
buffer_
, other.
buffer_
, other.
size_
);
62
}
63
69
basic_binary_buffer
(
basic_binary_buffer
&& other) noexcept
70
:
buffer_
(std::exchange(other.buffer_,
nullptr
)),
size_
(other.size_) {}
71
78
basic_binary_buffer
&
operator=
(
const
basic_binary_buffer
& other) {
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
95
basic_binary_buffer
&
operator=
(
basic_binary_buffer
&& other)
noexcept
{
96
swap
(other);
97
return
*
this
;
98
}
99
103
~basic_binary_buffer
() { std::free(
buffer_
); }
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
154
private
:
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
179
namespace
std
{
180
188
template
<>
189
inline
void
190
swap
(
// NOLINT(readability-inconsistent-declaration-parameter-name)
191
// It's impossible to fix this because different implementation of STL can
192
// have different argument names.
193
msgpack_light::details::basic_binary_buffer
& instance1,
194
msgpack_light::details::basic_binary_buffer
& instance2)
noexcept
{
195
instance1.swap(instance2);
196
}
197
198
}
// namespace std
msgpack_light::details::basic_binary_buffer
Class of basic buffers for binary data.
Definition
basic_binary_buffer.h:38
msgpack_light::details::basic_binary_buffer::~basic_binary_buffer
~basic_binary_buffer()
Destructor.
Definition
basic_binary_buffer.h:103
msgpack_light::details::basic_binary_buffer::swap
void swap(basic_binary_buffer &other) noexcept
Swap with another instance.
Definition
basic_binary_buffer.h:128
msgpack_light::details::basic_binary_buffer::size
std::size_t size() const noexcept
Get the size of the buffer.
Definition
basic_binary_buffer.h:152
msgpack_light::details::basic_binary_buffer::resize
void resize(std::size_t new_size)
Change the size of the buffer.
Definition
basic_binary_buffer.h:113
msgpack_light::details::basic_binary_buffer::basic_binary_buffer
basic_binary_buffer(const basic_binary_buffer &other)
Copy constructor.
Definition
basic_binary_buffer.h:59
msgpack_light::details::basic_binary_buffer::data
const unsigned char * data() const noexcept
Get the pointer to the buffer.
Definition
basic_binary_buffer.h:145
msgpack_light::details::basic_binary_buffer::basic_binary_buffer
basic_binary_buffer(basic_binary_buffer &&other) noexcept
Move constructor.
Definition
basic_binary_buffer.h:69
msgpack_light::details::basic_binary_buffer::size_
std::size_t size_
Size of the buffer.
Definition
basic_binary_buffer.h:174
msgpack_light::details::basic_binary_buffer::buffer_
unsigned char * buffer_
Buffer.
Definition
basic_binary_buffer.h:171
msgpack_light::details::basic_binary_buffer::operator=
basic_binary_buffer & operator=(basic_binary_buffer &&other) noexcept
Move assignment operator.
Definition
basic_binary_buffer.h:95
msgpack_light::details::basic_binary_buffer::basic_binary_buffer
basic_binary_buffer(std::size_t size)
Constructor.
Definition
basic_binary_buffer.h:45
msgpack_light::details::basic_binary_buffer::data
unsigned char * data() noexcept
Get the pointer to the buffer.
Definition
basic_binary_buffer.h:138
msgpack_light::details::basic_binary_buffer::prevent_unsafe_size
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:...
Definition
basic_binary_buffer.h:163
msgpack_light::details::basic_binary_buffer::operator=
basic_binary_buffer & operator=(const basic_binary_buffer &other)
Copy assignment operator.
Definition
basic_binary_buffer.h:78
msgpack_light::details
Namespace of internal implementations.
Definition
binary.h:35
std
STL namespace.
std::swap
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.
Definition
basic_binary_buffer.h:190
include
msgpack_light
details
basic_binary_buffer.h
Generated on
for cpp-msgpack-light by
1.18.0