MIP_SDK  latest-2-g34f3e39
MicroStrain Communications Library for embedded systems
mip_meta_utils.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "mip_structures.hpp"
4 
5 
7 {
8 
9 //
10 // Gets the "Type" enum value given a C++ template type.
11 //
12 template<class T, class=void> struct ParamType { static inline constexpr auto value = Type::NONE; };
13 
14 template<> struct ParamType<char, void> { static constexpr inline auto value = Type::CHAR; };
15 template<> struct ParamType<bool, void> { static constexpr inline auto value = Type::BOOL; };
16 template<> struct ParamType<uint8_t, void> { static constexpr inline auto value = Type::U8; };
17 template<> struct ParamType< int8_t, void> { static constexpr inline auto value = Type::S8; };
18 template<> struct ParamType<uint16_t, void> { static constexpr inline auto value = Type::U16; };
19 template<> struct ParamType< int16_t, void> { static constexpr inline auto value = Type::S16; };
20 template<> struct ParamType<uint32_t, void> { static constexpr inline auto value = Type::U32; };
21 template<> struct ParamType< int32_t, void> { static constexpr inline auto value = Type::S32; };
22 template<> struct ParamType<uint64_t, void> { static constexpr inline auto value = Type::U64; };
23 template<> struct ParamType< int64_t, void> { static constexpr inline auto value = Type::S64; };
24 template<> struct ParamType<float, void> { static constexpr inline auto value = Type::FLOAT; };
25 template<> struct ParamType<double, void> { static constexpr inline auto value = Type::DOUBLE; };
26 template<class T> struct ParamType<Bitfield<T>, void> { static constexpr inline auto value = Type::BITS; };
27 template<class T> struct ParamType<T, typename std::enable_if<std::is_enum<T>::value, T>::type> { static constexpr inline auto value = Type::ENUM; };
28 template<class T> struct ParamType<T, typename EnableForFieldTypes<T>::type> { static constexpr inline auto value = Type::STRUCT; };
29 template<class T> struct ParamType<T, typename std::enable_if<std::is_union<T>::value, T>::type> { static constexpr inline auto value = Type::UNION; };
30 
31 
32 //
33 // Gets the template type given a compile-time "Type" enum value.
34 // Note: this only works with basic/built-in/arithmetic types, not structs/enums/etc.
35 //
36 template<Type Kind>
37 struct ParamEnum { using type = void; };
38 
39 template<> struct ParamEnum<Type::CHAR > { using type = char; };
40 template<> struct ParamEnum<Type::BOOL > { using type = bool; };
41 template<> struct ParamEnum<Type::U8 > { using type = uint8_t; };
42 template<> struct ParamEnum<Type::S8 > { using type = int8_t; };
43 template<> struct ParamEnum<Type::U16 > { using type = uint16_t; };
44 template<> struct ParamEnum<Type::S16 > { using type = int16_t; };
45 template<> struct ParamEnum<Type::U32 > { using type = uint32_t; };
46 template<> struct ParamEnum<Type::S32 > { using type = int32_t; };
47 template<> struct ParamEnum<Type::U64 > { using type = uint64_t; };
48 template<> struct ParamEnum<Type::S64 > { using type = int64_t; };
49 template<> struct ParamEnum<Type::FLOAT > { using type = float; };
50 template<> struct ParamEnum<Type::DOUBLE> { using type = double; };
51 
52 
53 // Gets a void pointer to the member identified by Ptr, given an
54 // instance of field passed by void pointer.
55 /*template<class Field, class T, T Field::*Ptr>
56 void* access(void* p)
57 {
58  return &(static_cast<Field*>(p)->*Ptr);
59 }
60 
61 template<class Field, class T, auto Ptr>
62 void* access(void* p)
63 {
64  Field* f = static_cast<Field*>(p);
65  auto& param = f->*Ptr;
66  return &param;
67 }*/
68 
69 template<class Field, class T, auto Ptr>
70 void* access(void* p);
71 
72 
73 #ifdef MICROSTRAIN_HAS_OPTIONAL
74 inline std::optional<uint64_t> extractIntegralType(mip::metadata::Type type, microstrain::Span<const uint8_t> payload, size_t offset=0)
77 {
78  switch(type)
79  {
80  case mip::metadata::Type::BOOL: return microstrain::extract< bool, microstrain::serialization::Endian::big>(payload.data(), payload.size(), offset);
81  case mip::metadata::Type::U8: return microstrain::extract< uint8_t, microstrain::serialization::Endian::big>(payload.data(), payload.size(), offset);
82  case mip::metadata::Type::S8: return microstrain::extract< int8_t, microstrain::serialization::Endian::big>(payload.data(), payload.size(), offset);
83  case mip::metadata::Type::U16: return microstrain::extract<uint16_t, microstrain::serialization::Endian::big>(payload.data(), payload.size(), offset);
84  case mip::metadata::Type::S16: return microstrain::extract< int32_t, microstrain::serialization::Endian::big>(payload.data(), payload.size(), offset);
85  case mip::metadata::Type::U32: return microstrain::extract<uint32_t, microstrain::serialization::Endian::big>(payload.data(), payload.size(), offset);
86  case mip::metadata::Type::S32: return microstrain::extract< int64_t, microstrain::serialization::Endian::big>(payload.data(), payload.size(), offset);
87  case mip::metadata::Type::U64: return microstrain::extract<uint64_t, microstrain::serialization::Endian::big>(payload.data(), payload.size(), offset);
88  case mip::metadata::Type::S64: return microstrain::extract< int64_t, microstrain::serialization::Endian::big>(payload.data(), payload.size(), offset);
89  default: return std::nullopt;
90  }
91 }
92 #endif // MICROSTRAIN_HAS_OPTIONAL
93 
101 inline Type serializedType(const TypeInfo& type)
102 {
103  if (type.isBasicType())
104  return type.type;
105 
106  if (const EnumInfo *ptr = type.enumPointer())
107  return ptr->type;
108 
109  if (const BitfieldInfo *ptr = type.bitfieldPointer())
110  return ptr->type;
111 
112  return Type::NONE;
113 }
114 
117 constexpr size_t serializedSizeForBasicType(Type type, const void* info=nullptr)
118 {
119  switch(type)
120  {
121  case Type::CHAR:
122  case Type::BOOL:
123  case Type::U8:
124  case Type::S8:
125  return 1;
126  case Type::U16:
127  case Type::S16:
128  return 2;
129  case Type::U32:
130  case Type::S32:
131  case Type::FLOAT:
132  return 4;
133  case Type::U64:
134  case Type::S64:
135  case Type::DOUBLE:
136  return 8;
137 
138  case Type::ENUM:
139  if(!info)
140  return 0;
141  return serializedSizeForBasicType(static_cast<const EnumInfo *>(info)->type);
142 
143  case Type::BITS:
144  if(!info)
145  return 0;
146  return serializedSizeForBasicType(static_cast<const BitfieldInfo *>(info)->type);
147 
148  default:
149  return 0;
150  }
151 }
152 constexpr size_t serializedSizeForBasicType(const TypeInfo& type) { return serializedSizeForBasicType(type.type, type.infoPtr); }
153 
154 constexpr const char* nameForBasicType(Type type)
155 {
156  switch(type)
157  {
158  case Type::NONE: return "none";
159  case Type::BOOL: return "bool";
160  case Type::CHAR: return "char";
161  case Type::U8: return "u8";
162  case Type::S8: return "s8";
163  case Type::U16: return "u16";
164  case Type::S16: return "s16";
165  case Type::U32: return "u32";
166  case Type::S32: return "s32";
167  case Type::U64: return "u64";
168  case Type::S64: return "s64";
169  case Type::FLOAT: return "float";
170  case Type::DOUBLE: return "double";
171  case Type::ENUM: return "enum";
172  case Type::BITS: return "bitfield";
173  case Type::STRUCT: return "struct";
174  case Type::UNION: return "union";
175  default: return nullptr;
176  }
177 }
178 
179 constexpr const char* nameOfType(const TypeInfo& type)
180 {
181  switch(type.type)
182  {
183  default: return nameForBasicType(type.type);
184  case Type::ENUM: return type.infoPtr ? static_cast<const EnumInfo* >(type.infoPtr)->name : "enum";
185  case Type::BITS: return type.infoPtr ? static_cast<const BitfieldInfo*>(type.infoPtr)->name : "bitfield";
186  case Type::STRUCT: return type.infoPtr ? static_cast<const StructInfo* >(type.infoPtr)->name : "struct";
187  case Type::UNION: return type.infoPtr ? static_cast<const UnionInfo* >(type.infoPtr)->name : "union";
188  }
189 }
190 
191 } // namespace mip::metadata
mip::metadata::utils::nameOfType
constexpr const char * nameOfType(const TypeInfo &type)
Definition: mip_meta_utils.hpp:179
microstrain::Span::data
constexpr pointer data() const noexcept
Definition: span.hpp:63
mip::metadata::utils::ParamEnum
Definition: mip_meta_utils.hpp:37
mip::metadata::utils::ParamEnum< Type::CHAR >::type
char type
Definition: mip_meta_utils.hpp:39
mip::metadata::Type::ENUM
@ ENUM
mip::metadata::Type::NONE
@ NONE
Invalid/unknown.
mip::metadata::utils::ParamType::value
static constexpr auto value
Definition: mip_meta_utils.hpp:12
mip::metadata::utils::serializedType
Type serializedType(const TypeInfo &type)
Determines the type used for serialization purposes.
Definition: mip_meta_utils.hpp:101
mip::metadata::BitfieldInfo
Definition: mip_structures.hpp:91
mip::EnableForFieldTypes
std::enable_if< isField< T >::value, T > EnableForFieldTypes
Definition: mip_descriptors.hpp:56
microstrain::Span::size
constexpr size_t size() const noexcept
Definition: span.hpp:65
mip::metadata::utils::ParamEnum< Type::S16 >::type
int16_t type
Definition: mip_meta_utils.hpp:44
mip::metadata::Type::U16
@ U16
mip::metadata::utils::nameForBasicType
constexpr const char * nameForBasicType(Type type)
Definition: mip_meta_utils.hpp:154
microstrain::Span
Implementation of std::span from C++20.
Definition: span.hpp:40
mip::metadata::Type::STRUCT
@ STRUCT
mip::metadata::Type::S32
@ S32
mip::metadata::utils::ParamEnum< Type::FLOAT >::type
float type
Definition: mip_meta_utils.hpp:49
mip::metadata::Type::DOUBLE
@ DOUBLE
mip::metadata::Type
Type
Definition: mip_structures.hpp:23
mip::metadata::Type::CHAR
@ CHAR
mip::metadata::utils::ParamEnum::type
void type
Definition: mip_meta_utils.hpp:37
mip::metadata::utils::ParamEnum< Type::DOUBLE >::type
double type
Definition: mip_meta_utils.hpp:50
mip::metadata::utils::ParamType
Definition: mip_meta_utils.hpp:12
mip::metadata::Type::UNION
@ UNION
mip::metadata::Type::S8
@ S8
mip::metadata::Type::FLOAT
@ FLOAT
mip::metadata::Type::S16
@ S16
mip::metadata::Type::S64
@ S64
mip::metadata::TypeInfo::isBasicType
bool isBasicType() const
Definition: mip_structures.hpp:60
mip::metadata::utils::ParamEnum< Type::U8 >::type
uint8_t type
Definition: mip_meta_utils.hpp:41
mip::metadata::Type::U64
@ U64
mip::metadata::Type::BOOL
@ BOOL
mip::metadata::utils::ParamEnum< Type::U64 >::type
uint64_t type
Definition: mip_meta_utils.hpp:47
mip::metadata::Type::U8
@ U8
mip_structures.hpp
mip::metadata::TypeInfo::infoPtr
const void * infoPtr
Definition: mip_structures.hpp:53
mip::metadata::TypeInfo::bitfieldPointer
const BitfieldInfo * bitfieldPointer() const
Definition: mip_structures.hpp:56
mip::metadata::utils::ParamEnum< Type::S32 >::type
int32_t type
Definition: mip_meta_utils.hpp:46
mip::metadata::EnumInfo
Definition: mip_structures.hpp:65
mip::metadata::utils
Definition: mip_meta_utils.hpp:6
mip::metadata::utils::ParamEnum< Type::U16 >::type
uint16_t type
Definition: mip_meta_utils.hpp:43
mip::metadata::TypeInfo::type
Type type
Definition: mip_structures.hpp:51
mip::metadata::TypeInfo
Definition: mip_structures.hpp:46
mip::metadata::utils::ParamEnum< Type::S8 >::type
int8_t type
Definition: mip_meta_utils.hpp:42
mip::metadata::utils::access
void * access(void *p)
mip::metadata::Type::BITS
@ BITS
mip::metadata::StructInfo
Definition: mip_structures.hpp:205
mip::metadata::utils::ParamEnum< Type::U32 >::type
uint32_t type
Definition: mip_meta_utils.hpp:45
mip::Bitfield
A dummy struct which is used to mark bitfield objects.
Definition: mip_descriptors.hpp:61
mip::metadata::TypeInfo::enumPointer
const EnumInfo * enumPointer() const
Definition: mip_structures.hpp:55
mip::metadata::utils::serializedSizeForBasicType
constexpr size_t serializedSizeForBasicType(Type type, const void *info=nullptr)
Gets the size of a basic type (including bitfields and enums if class_ is not NULL).
Definition: mip_meta_utils.hpp:117
mip::metadata::Type::U32
@ U32
mip::metadata::UnionInfo
Definition: mip_structures.hpp:214
mip::metadata::utils::ParamEnum< Type::S64 >::type
int64_t type
Definition: mip_meta_utils.hpp:48
mip::metadata::utils::ParamEnum< Type::BOOL >::type
bool type
Definition: mip_meta_utils.hpp:40