COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
SizeToType.h
1//
2// Copyright 2013 - 2024 (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
76} // namespace details
77
79
80template <std::size_t TSize, bool TSigned = false>
81class SizeToType
82{
83 using ByteType = typename SizeToType<1, TSigned>::Type;
84
85public:
86 using Type = std::array<ByteType, TSize>;
87};
88
89template <std::size_t TSize>
90struct SizeToType<TSize, false>
91{
92 using Type = typename details::SizeToTypeHelper<TSize>::Type;
93};
94
95template <std::size_t TSize>
96struct SizeToType<TSize, true>
97{
98 using Type = typename
99 std::make_signed<
100 typename SizeToType<TSize, false>::Type
101 >::type;
102};
103
104template <std::size_t TSize, bool TSigned = false>
105using SizeToTypeT = typename SizeToType<TSize, TSigned>::Type;
106
108
109} // namespace util
110
111} // namespace comms
Main namespace for all classes / functions of COMMS library.