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