distortos  v0.7.0
object-oriented C++ RTOS for microcontrollers
TypeFromSize.hpp
Go to the documentation of this file.
1 
12 #ifndef ESTD_TYPEFROMSIZE_HPP_
13 #define ESTD_TYPEFROMSIZE_HPP_
14 
15 #include <type_traits>
16 
17 #include <cstddef>
18 #include <cstdint>
19 
26 #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
27 
28 namespace estd
29 {
30 
31 namespace internal
32 {
33 
40 template<size_t size>
42 {
43  static_assert(size <= sizeof(uint64_t), "Max supported type is uint64_t!");
44 
46  using Type = typename std::conditional<size <= sizeof(uint8_t), uint8_t,
47  typename std::conditional<size <= sizeof(uint16_t), uint16_t,
48  typename std::conditional<size <= sizeof(uint32_t), uint32_t, uint64_t>::type>::type>::type;
49 };
50 
51 }
52 
59 template<size_t size>
61 
62 STATIC_ASSERT(std::is_same<TypeFromSize<0>, uint8_t>::value == true);
63 STATIC_ASSERT(std::is_same<TypeFromSize<1>, uint8_t>::value == true);
64 STATIC_ASSERT(std::is_same<TypeFromSize<2>, uint16_t>::value == true);
65 STATIC_ASSERT(std::is_same<TypeFromSize<3>, uint32_t>::value == true);
66 STATIC_ASSERT(std::is_same<TypeFromSize<4>, uint32_t>::value == true);
67 STATIC_ASSERT(std::is_same<TypeFromSize<5>, uint64_t>::value == true);
68 STATIC_ASSERT(std::is_same<TypeFromSize<6>, uint64_t>::value == true);
69 STATIC_ASSERT(std::is_same<TypeFromSize<7>, uint64_t>::value == true);
70 STATIC_ASSERT(std::is_same<TypeFromSize<8>, uint64_t>::value == true);
71 
72 } // namespace estd
73 
74 #endif // ESTD_TYPEFROMSIZE_HPP_
Collection of useful templates.
Definition: DmaChannel.hpp:121
Implementation of TypeFromSize.
Definition: TypeFromSize.hpp:41
typename internal::TypeFromSize< size >::Type TypeFromSize
Selects fixed width type from requested byte size.
Definition: TypeFromSize.hpp:60
#define STATIC_ASSERT(...)
Produces a static_assert() where the expression is also used as the message.
Definition: log2u.hpp:23
typename std::conditional< size<=sizeof(uint8_t), uint8_t, typename std::conditional< size<=sizeof(uint16_t), uint16_t, typename std::conditional< size<=sizeof(uint32_t), uint32_t, uint64_t >::type >::type >::type Type
one of fixed width types which is at least size bytes
Definition: TypeFromSize.hpp:48