cpp-msgpack-light
0.3.0
A light library to serialize MessagePack.
Toggle main menu visibility
Loading...
Searching...
No Matches
buffered_serialization_buffer_impl.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 <array>
23
#include <cstddef>
// IWYU pragma: keep
24
#include <cstring>
25
26
#include "
msgpack_light/details/mutable_static_binary_view.h
"
27
#include "
msgpack_light/details/pack_in_big_endian.h
"
28
#include "
msgpack_light/details/static_memory_buffer_size.h
"
29
#include "
msgpack_light/details/total_size_of.h
"
// IWYU pragma: keep
30
#include "
msgpack_light/output_stream.h
"
31
32
namespace
msgpack_light::details
{
33
38
class
buffered_serialization_buffer_impl
{
39
public
:
47
explicit
buffered_serialization_buffer_impl
(
output_stream
& stream)
48
:
stream_
(stream) {}
49
50
buffered_serialization_buffer_impl
(
51
const
buffered_serialization_buffer_impl
&) =
delete
;
52
buffered_serialization_buffer_impl
(
53
buffered_serialization_buffer_impl
&&) =
delete
;
54
buffered_serialization_buffer_impl
& operator=(
55
const
buffered_serialization_buffer_impl
&) =
delete
;
56
buffered_serialization_buffer_impl
& operator=(
57
buffered_serialization_buffer_impl
&&) =
delete
;
58
62
~buffered_serialization_buffer_impl
() noexcept {
flush
(); }
63
67
void
flush
() {
68
stream_
.
write
(
buffer_
.data(),
current_position_in_buffer_
);
69
current_position_in_buffer_
= 0U;
70
}
71
78
void
write
(
const
unsigned
char
* data, std::size_t size) {
79
if
(size == 0U) {
80
return
;
81
}
82
83
if
(
buffer_size
-
current_position_in_buffer_
< size) {
84
flush
();
85
if
(
buffer_size
< size) {
86
stream_
.
write
(data, size);
87
return
;
88
}
89
}
90
91
std::memcpy(
buffer_
.data() +
current_position_in_buffer_
, data, size);
92
current_position_in_buffer_
+= size;
93
}
94
100
void
put
(
unsigned
char
data) {
101
if
(
buffer_size
==
current_position_in_buffer_
) {
102
flush
();
103
}
104
*(
buffer_
.data() +
current_position_in_buffer_
) = data;
105
++
current_position_in_buffer_
;
106
}
107
114
template
<
typename
... T>
115
void
write_in_big_endian
(T... values) {
116
details::pack_in_big_endian
(
117
prepare_buffer
<
total_size_of<T...>
>(), values...);
118
set_buffer_written
<
total_size_of
<T...>>();
119
}
120
121
private
:
128
template
<std::
size_t
N>
129
[[nodiscard]]
details::mutable_static_binary_view<N>
prepare_buffer
() {
130
static_assert
(N <=
buffer_size
);
131
132
if
(
buffer_size
-
current_position_in_buffer_
< N) {
133
flush
();
134
}
135
136
return
details::mutable_static_binary_view<N>
(
137
buffer_
.data() +
current_position_in_buffer_
);
138
}
139
145
template
<std::
size_t
N>
146
void
set_buffer_written
() noexcept {
147
current_position_in_buffer_
+= N;
148
}
149
151
output_stream
&
stream_
;
152
154
static
constexpr
std::size_t
buffer_size
=
155
details::static_memory_buffer_size
;
156
158
std::array<unsigned char, buffer_size>
buffer_
{};
159
161
std::size_t
current_position_in_buffer_
{0U};
162
};
163
164
}
// namespace msgpack_light::details
msgpack_light::details::buffered_serialization_buffer_impl
Class to implement internal implementation of serialization_buffer class using buffers.
Definition
buffered_serialization_buffer_impl.h:38
msgpack_light::details::buffered_serialization_buffer_impl::prepare_buffer
details::mutable_static_binary_view< N > prepare_buffer()
Prepare a buffer.
Definition
buffered_serialization_buffer_impl.h:129
msgpack_light::details::buffered_serialization_buffer_impl::set_buffer_written
void set_buffer_written() noexcept
Set the buffer returned by prepare_buffer function to be written.
Definition
buffered_serialization_buffer_impl.h:146
msgpack_light::details::buffered_serialization_buffer_impl::buffer_size
static constexpr std::size_t buffer_size
Size of the internal buffer.
Definition
buffered_serialization_buffer_impl.h:154
msgpack_light::details::buffered_serialization_buffer_impl::write_in_big_endian
void write_in_big_endian(T... values)
Write values in big endian.
Definition
buffered_serialization_buffer_impl.h:115
msgpack_light::details::buffered_serialization_buffer_impl::buffered_serialization_buffer_impl
buffered_serialization_buffer_impl(output_stream &stream)
Constructor.
Definition
buffered_serialization_buffer_impl.h:47
msgpack_light::details::buffered_serialization_buffer_impl::write
void write(const unsigned char *data, std::size_t size)
Write data.
Definition
buffered_serialization_buffer_impl.h:78
msgpack_light::details::buffered_serialization_buffer_impl::buffer_
std::array< unsigned char, buffer_size > buffer_
Internal buffer.
Definition
buffered_serialization_buffer_impl.h:158
msgpack_light::details::buffered_serialization_buffer_impl::~buffered_serialization_buffer_impl
~buffered_serialization_buffer_impl() noexcept
Destructor.
Definition
buffered_serialization_buffer_impl.h:62
msgpack_light::details::buffered_serialization_buffer_impl::current_position_in_buffer_
std::size_t current_position_in_buffer_
Current position in the internal buffer.
Definition
buffered_serialization_buffer_impl.h:161
msgpack_light::details::buffered_serialization_buffer_impl::stream_
output_stream & stream_
Stream to write output to.
Definition
buffered_serialization_buffer_impl.h:151
msgpack_light::details::buffered_serialization_buffer_impl::put
void put(unsigned char data)
Write a byte of data.
Definition
buffered_serialization_buffer_impl.h:100
msgpack_light::details::buffered_serialization_buffer_impl::flush
void flush()
Flush the internal buffer in this instance.
Definition
buffered_serialization_buffer_impl.h:67
msgpack_light::details::mutable_static_binary_view
Class of views of mutable buffers with static sizes.
Definition
mutable_static_binary_view.h:32
msgpack_light::output_stream
Interface of streams to write data.
Definition
output_stream.h:32
msgpack_light::output_stream::write
virtual void write(const unsigned char *data, std::size_t size)=0
Write data.
mutable_static_binary_view.h
Definition of mutable_static_binary_view class.
msgpack_light::details
Namespace of internal implementations.
Definition
binary.h:35
msgpack_light::details::static_memory_buffer_size
constexpr std::size_t static_memory_buffer_size
Size of buffers on memory defined using static arrays.
Definition
static_memory_buffer_size.h:29
msgpack_light::details::pack_in_big_endian
void pack_in_big_endian(mutable_static_binary_view< total_size_of< T, Remaining... > > buffer, T value, Remaining... remaining_values) noexcept
Pack some values in big endian.
Definition
pack_in_big_endian.h:38
msgpack_light::details::total_size_of
constexpr std::size_t total_size_of
Get the total size of types in template arguments.
Definition
total_size_of.h:64
output_stream.h
Definition of output_stream class.
pack_in_big_endian.h
Definition of pack_in_big_endian function.
static_memory_buffer_size.h
Definition of static_memory_buffer_size constant.
total_size_of.h
Definition of total_size_of variable.
include
msgpack_light
details
buffered_serialization_buffer_impl.h
Generated on
for cpp-msgpack-light by
1.18.0