cpp-msgpack-light 0.3.0
A light library to serialize MessagePack.
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
29#include "msgpack_light/details/total_size_of.h" // IWYU pragma: keep
31
32namespace msgpack_light::details {
33
39public:
48 : stream_(stream) {}
49
58
63
71
78 void write(const unsigned char* data, std::size_t size) {
79 if (size == 0U) {
80 return;
81 }
82
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);
93 }
94
100 void put(unsigned char data) {
102 flush();
103 }
104 *(buffer_.data() + current_position_in_buffer_) = data;
106 }
107
114 template <typename... T>
120
121private:
128 template <std::size_t N>
130 static_assert(N <= buffer_size);
131
133 flush();
134 }
135
138 }
139
145 template <std::size_t N>
146 void set_buffer_written() noexcept {
148 }
149
152
154 static constexpr std::size_t buffer_size =
156
158 std::array<unsigned char, buffer_size> buffer_{};
159
162};
163
164} // namespace msgpack_light::details
Class to implement internal implementation of serialization_buffer class using buffers.
details::mutable_static_binary_view< N > prepare_buffer()
Prepare a buffer.
void set_buffer_written() noexcept
Set the buffer returned by prepare_buffer function to be written.
static constexpr std::size_t buffer_size
Size of the internal buffer.
void write(const unsigned char *data, std::size_t size)
Write data.
std::array< unsigned char, buffer_size > buffer_
Internal buffer.
std::size_t current_position_in_buffer_
Current position in the internal buffer.
Class of views of mutable buffers with static sizes.
Interface of streams to write data.
Definition of mutable_static_binary_view class.
Namespace of internal implementations.
Definition binary.h:35
constexpr std::size_t static_memory_buffer_size
Size of buffers on memory defined using static arrays.
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.
constexpr std::size_t total_size_of
Get the total size of types in template arguments.
Definition of output_stream class.
Definition of pack_in_big_endian function.
Definition of static_memory_buffer_size constant.
Definition of total_size_of variable.