COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
SizeToType.h
1//
2// Copyright 2013 - 2026 (C). Alex Robenko. All rights reserved.
3//
4// SPDX-License-Identifier: MPL-2.0
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#pragma once
11
12#include <array>
13#include <cstddef>
14#include <cstdint>
15
16namespace comms
17{
18
19namespace util
20{
21
22namespace details
23{
24
25template <std::size_t TSize>
26struct SizeToTypeHelper {
27 using Type = std::array<std::uint8_t, TSize>;
28};
29
30template <>
31struct SizeToTypeHelper<1>
32{
33 using Type = std::uint8_t;
34};
35
36template <>
37struct SizeToTypeHelper<2>
38{
39 using Type = std::uint16_t;
40};
41
42template <>
43struct SizeToTypeHelper<4>
44{
45 using Type = std::uint32_t;
46};
47
48template <>
49struct SizeToTypeHelper<8>
50{
51 using Type = std::uint64_t;
52};
53
54template <>
55struct SizeToTypeHelper<3>
56{
57 using Type = SizeToTypeHelper<4>::Type;
58};
59
60template <>
61struct SizeToTypeHelper<5>
62{
63 using Type = SizeToTypeHelper<8>::Type;
64};
65
66template <>
67struct SizeToTypeHelper<6>
68{
69 using Type = SizeToTypeHelper<8>::Type;
70};
71
72template <>
73struct SizeToTypeHelper<7>
74{
75 using Type = SizeToTypeHelper<8>::Type;
76};
77
78} // namespace details
79
81
82template <std::size_t TSize, bool TSigned = false>
83class SizeToType
84{
85 using ByteType = typename SizeToType<1, TSigned>::Type;
86
87public:
88 using Type = std::array<ByteType, TSize>;
89};
90
91template <std::size_t TSize>
92struct SizeToType<TSize, false>
93{
94 using Type = typename details::SizeToTypeHelper<TSize>::Type;
95};
96
97template <std::size_t TSize>
98struct SizeToType<TSize, true>
99{
100 using Type = typename
101 std::make_signed<
102 typename SizeToType<TSize, false>::Type
103 >::type;
104};
105
106template <std::size_t TSize, bool TSigned = false>
107using SizeToTypeT = typename SizeToType<TSize, TSigned>::Type;
108
110
111} // namespace util
112
113} // namespace comms
Main namespace for all classes / functions of COMMS library.