cpp-msgpack-light 0.3.0
A light library to serialize MessagePack.
Loading...
Searching...
No Matches
to_big_endian.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 <cstdint>
25#include <cstring>
26
28
29namespace msgpack_light::details {
30
38template <std::size_t N>
39inline void to_big_endian(
40 const void* from, mutable_static_binary_view<N> to) noexcept;
41
48template <>
50 const void* from, mutable_static_binary_view<1U> to) noexcept {
51 std::memcpy(to.data(), from, 1U);
52}
53
60template <>
62 const void* from, mutable_static_binary_view<2U> to) noexcept {
63 std::uint16_t value{};
64 std::memcpy(&value, from, sizeof(value));
65 to[0U] = static_cast<unsigned char>(value >> 8U); // NOLINT
66 to[1U] = static_cast<unsigned char>(value); // NOLINT
67}
68
75template <>
77 const void* from, mutable_static_binary_view<4U> to) noexcept {
78 std::uint32_t value{};
79 std::memcpy(&value, from, sizeof(value));
80 to[0U] = static_cast<unsigned char>(value >> 24U); // NOLINT
81 to[1U] = static_cast<unsigned char>(value >> 16U); // NOLINT
82 to[2U] = static_cast<unsigned char>(value >> 8U); // NOLINT
83 to[3U] = static_cast<unsigned char>(value); // NOLINT
84}
85
92template <>
93// NOLINTNEXTLINE(readability-magic-numbers)
95 // NOLINTNEXTLINE(readability-magic-numbers)
96 const void* from, mutable_static_binary_view<8U> to) noexcept {
97 std::uint64_t value{};
98 std::memcpy(&value, from, sizeof(value));
99 to[0U] = static_cast<unsigned char>(value >> 56U); // NOLINT
100 to[1U] = static_cast<unsigned char>(value >> 48U); // NOLINT
101 to[2U] = static_cast<unsigned char>(value >> 40U); // NOLINT
102 to[3U] = static_cast<unsigned char>(value >> 32U); // NOLINT
103 to[4U] = static_cast<unsigned char>(value >> 24U); // NOLINT
104 to[5U] = static_cast<unsigned char>(value >> 16U); // NOLINT
105 to[6U] = static_cast<unsigned char>(value >> 8U); // NOLINT
106 to[7U] = static_cast<unsigned char>(value); // NOLINT
107}
108
116template <std::size_t N>
117inline void to_big_endian(
118 const void* from, std::array<unsigned char, N>* to) noexcept {
120}
121
122} // namespace msgpack_light::details
Class of views of mutable buffers with static sizes.
Definition of mutable_static_binary_view class.
Namespace of internal implementations.
Definition binary.h:35
void to_big_endian< 2U >(const void *from, mutable_static_binary_view< 2U > to) noexcept
Convert to big endian.
void to_big_endian< 1U >(const void *from, mutable_static_binary_view< 1U > to) noexcept
Convert to big endian.
void to_big_endian< 4U >(const void *from, mutable_static_binary_view< 4U > to) noexcept
Convert to big endian.
void to_big_endian(const void *from, mutable_static_binary_view< N > to) noexcept
Convert to big endian.
void to_big_endian< 8U >(const void *from, mutable_static_binary_view< 8U > to) noexcept
Convert to big endian.