MIP_SDK  latest-2-g34f3e39
MicroStrain Communications Library for embedded systems
mip_structures.hpp
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include <stdint.h>
8 
9 
10 namespace mip::metadata
11 {
12  template<class T>
14 
15 struct EnumInfo;
16 struct BitfieldInfo;
17 struct UnionInfo;
18 struct StructInfo;
19 struct FieldInfo;
20 struct ParameterInfo;
21 
22 
23 enum class Type
24 {
25  NONE = 0,
26 
27  CHAR,
28  BOOL,
29  U8,
30  S8,
31  U16,
32  S16,
33  U32,
34  S32,
35  U64,
36  S64,
37  FLOAT,
38  DOUBLE,
39 
40  ENUM,
41  BITS,
42  STRUCT,
43  UNION,
44 };
45 
46 struct TypeInfo
47 {
48  //template<class Field, class T>
49  //TypeInfo(T Field::*member);
50 
52 
53  const void* infoPtr = nullptr;
54 
55  const EnumInfo* enumPointer() const { return (type == Type::ENUM ) ? static_cast<const EnumInfo* >(infoPtr) : nullptr; }
56  const BitfieldInfo* bitfieldPointer() const { return (type == Type::BITS ) ? static_cast<const BitfieldInfo*>(infoPtr) : nullptr; }
57  const UnionInfo* unionPointer() const { return (type == Type::UNION ) ? static_cast<const UnionInfo* >(infoPtr) : nullptr; }
58  const StructInfo* structPointer() const { return (type == Type::STRUCT) ? static_cast<const StructInfo* >(infoPtr) : nullptr; }
59 
60  bool isBasicType() const { return type <= Type::DOUBLE; }
61  bool isCustom() const { return type != Type::NONE && type >= Type::ENUM; }
62  bool isClass() const { return type != Type::NONE && type >= Type::STRUCT; }
63 };
64 
65 struct EnumInfo
66 {
67  struct Entry
68  {
69  uint32_t value = 0;
70  const char* name = nullptr;
71  const char* docs = nullptr;
72  };
73 
74  const char* name = nullptr;
75  const char* docs = nullptr;
77 
79 
80  const char* nameForValue(uint32_t value) const
81  {
82  for(const Entry& entry : entries)
83  {
84  if(entry.value == value)
85  return entry.name;
86  }
87  return nullptr;
88  }
89 };
90 
91 struct BitfieldInfo : public EnumInfo {};
92 
93 
94 // Basic attribute bitfield for fields and parameters.
96 {
97  constexpr FunctionBits() = default;
98  constexpr FunctionBits(bool w, bool r, bool s, bool l, bool d) : bits(0x00)
99  {
100  setCanWrite(w);
101  setCanRead(r);
102  setCanSave(s);
103  setCanLoad(l);
104  setCanReset(d);
105  }
106 
107  constexpr bool canWrite() const { return bits & 0b00000001; }
108  constexpr bool canRead() const { return bits & 0b00000010; }
109  constexpr bool canSave() const { return bits & 0b00000100; }
110  constexpr bool canLoad() const { return bits & 0b00001000; }
111  constexpr bool canReset() const { return bits & 0b00010000; }
112 
113  constexpr FunctionBits& setCanWrite (bool w) { bits = (bits & 0b11111110) | (uint8_t(w)<<0); return *this; }
114  constexpr FunctionBits& setCanRead (bool r) { bits = (bits & 0b11111101) | (uint8_t(r)<<1); return *this; }
115  constexpr FunctionBits& setCanSave (bool s) { bits = (bits & 0b11111011) | (uint8_t(s)<<2); return *this; }
116  constexpr FunctionBits& setCanLoad (bool l) { bits = (bits & 0b11110111) | (uint8_t(l)<<3); return *this; }
117  constexpr FunctionBits& setCanReset (bool d) { bits = (bits & 0b11101111) | (uint8_t(d)<<4); return *this; }
118 
119  constexpr bool has(mip::FunctionSelector function) const { return (bits >> (static_cast<uint8_t>(function) - static_cast<uint8_t>(mip::FunctionSelector::WRITE))) & 1; }
120 
121  uint8_t bits = 0x00;
122 };
123 
124 // Extended attributes for parameters.
126 {
127  constexpr ParamAttributes() = default;
129  constexpr ParamAttributes(bool w, bool r, bool s, bool l, bool d, bool e=false, bool x=false) : FunctionBits(w,r,s,l,d)
130  {
131  setNotSerialized(x);
132  setIsEchoed(e);
133  }
134 
135  constexpr bool isNotSerialized() const { return bits & 0b01000000; }
136  constexpr bool isEchoed() const { return bits & 0b10000000; }
137  constexpr bool isSerialized() const { return !isNotSerialized(); }
138 
139  constexpr ParamAttributes& setNotSerialized (bool x) { bits = (bits & 0b10111111) | (uint8_t(x)<<6); return *this; }
140  constexpr ParamAttributes& setIsEchoed (bool e) { bits = (bits & 0b01111111) | (uint8_t(e)<<7); return *this; }
141 };
142 
143 // Extended attributes for fields.
145 {
146  constexpr FieldAttributes() = default;
148  constexpr FieldAttributes(bool w, bool r, bool s, bool l, bool d, bool p=false) : FunctionBits(w,r,s,l,d) { setProprietary(p); }
149 
150  constexpr bool hasFunctionSelector() const { return (bits & 0b00011111) > 0; }
151 
152  constexpr bool isProprietary() const { return bits & 0b10000000; }
153 
154  constexpr FieldAttributes& setProprietary(bool p) { bits = (bits & 0b01111111) | (uint8_t(p)<<7); return *this; }
155 };
156 
157 static constexpr inline FunctionBits ALL_FUNCTIONS = {true, true, true, true, true};
158 static constexpr inline FunctionBits NO_FUNCTIONS = {false, false, false, false, false};
159 
160 
162 {
163  struct Count
164  {
165  constexpr Count() = default;
166  constexpr Count(uint8_t n) : count(n) {}
167  constexpr Count(uint8_t n, microstrain::Id id) : count(n), paramIdx(id) {}
168 
169  uint8_t count = 1;
171 
172  constexpr bool isFixed() const { return count > 0 && !paramIdx.isAssigned(); }
173  constexpr bool hasCounter() const { return paramIdx.isAssigned(); }
174  };
175 
176  struct Condition
177  {
178  enum class Type : uint8_t
179  {
180  NONE = 0,
181  ENUM = 1,
182  //PRODUCT = 2, ///< Depends on product variant (TBD)
183  //OPTIONAL = 2, ///< Parameter can be omitted (TBD)
184  };
185 
188  uint16_t value = 0;
189 
190  constexpr bool hasCondition() const { return type != Type::NONE; }
191  };
192 
193  using Accessor = void* (*)(void*);
194 
195  const char* name = nullptr;
196  const char* docs = nullptr;
198  Accessor accessor = nullptr;
202 };
203 
204 
206 {
207  const char* name = nullptr;
208  const char* title = nullptr;
209  const char* docs = nullptr;
210 
212 };
213 
214 struct UnionInfo : public StructInfo {};
215 
216 struct FieldInfo : public StructInfo
217 {
219  FieldAttributes functions = {false, false, false, false, false, false};
220  const FieldInfo* response = nullptr;
221 };
222 
224 {
225  uint8_t descriptor = mip::INVALID_DESCRIPTOR_SET;
226  const char* name = nullptr;
228 
229  //const FieldInfo* findField(uint8_t field_desc) const
230  //{
231  // // Binary search assumes fields are sorted by descriptor.
232  // auto it = std::lower_bound(fields.begin(), fields.end(), field_desc, [](const FieldInfo* a, uint8_t b){ return a->descriptor.fieldDescriptor < b; });
233  // return (it != fields.end()) && ((*it)->descriptor.fieldDescriptor==field_desc) ? *it : nullptr;
234  //}
235 };
236 
237 
238 
239 } // namespace mip::metadata
microstrain::Id::isAssigned
constexpr bool isAssigned() const
Definition: index.hpp:93
mip::metadata::ParameterInfo::Count::isFixed
constexpr bool isFixed() const
Definition: mip_structures.hpp:172
mip::metadata::EnumInfo::name
const char * name
Definition: mip_structures.hpp:74
mip::metadata::ParameterInfo::Count::paramIdx
microstrain::Id paramIdx
If assigned, specifies parameter that holds the actual runtime count.
Definition: mip_structures.hpp:170
mip::metadata::FieldAttributes::FieldAttributes
constexpr FieldAttributes(FunctionBits bits)
Definition: mip_structures.hpp:147
mip_descriptors.hpp
index.hpp
mip::metadata::EnumInfo::type
Type type
Definition: mip_structures.hpp:76
mip::metadata::Type::ENUM
@ ENUM
mip::metadata::ParameterInfo::accessor
Accessor accessor
Obtains a reference to the member variable.
Definition: mip_structures.hpp:198
mip::metadata::ParameterInfo::Condition
Definition: mip_structures.hpp:176
mip::metadata::FieldInfo::response
const FieldInfo * response
Definition: mip_structures.hpp:220
mip::metadata::ParameterInfo::Condition::Type::NONE
@ NONE
No condition, member always valid.
mip::metadata::Type::NONE
@ NONE
Invalid/unknown.
mip::metadata::FunctionBits::bits
uint8_t bits
Definition: mip_structures.hpp:121
mip::metadata::EnumInfo::docs
const char * docs
Definition: mip_structures.hpp:75
mip::metadata::BitfieldInfo
Definition: mip_structures.hpp:91
mip::metadata::ParameterInfo::Condition::hasCondition
constexpr bool hasCondition() const
Definition: mip_structures.hpp:190
mip::metadata::FieldAttributes::setProprietary
constexpr FieldAttributes & setProprietary(bool p)
Definition: mip_structures.hpp:154
mip::metadata
Definition: commands_3dm.hpp:10
mip::metadata::ParameterInfo::attributes
ParamAttributes attributes
This parameter is required for the specified function selectors.
Definition: mip_structures.hpp:199
mip::metadata::ParameterInfo::Count::Count
constexpr Count()=default
mip::metadata::ParameterInfo::Count::Count
constexpr Count(uint8_t n, microstrain::Id id)
Definition: mip_structures.hpp:167
mip::metadata::ParameterInfo::Count::count
uint8_t count
Fixed size if paramIdx unassigned.
Definition: mip_structures.hpp:169
mip::metadata::FunctionBits::canRead
constexpr bool canRead() const
Definition: mip_structures.hpp:108
mip::metadata::ParameterInfo::Count::Count
constexpr Count(uint8_t n)
Definition: mip_structures.hpp:166
mip::metadata::ParameterInfo::name
const char * name
Programmatic name (e.g. for printing or language bindings).
Definition: mip_structures.hpp:195
mip::metadata::ParamAttributes::isEchoed
constexpr bool isEchoed() const
Definition: mip_structures.hpp:136
mip::metadata::ParameterInfo::Accessor
void *(*)(void *) Accessor
Definition: mip_structures.hpp:193
mip::metadata::FieldAttributes
Definition: mip_structures.hpp:144
mip::metadata::Type::U16
@ U16
microstrain::Span
Implementation of std::span from C++20.
Definition: span.hpp:40
mip::metadata::Type::STRUCT
@ STRUCT
mip::metadata::FunctionBits::canReset
constexpr bool canReset() const
Definition: mip_structures.hpp:111
mip::metadata::StructInfo::title
const char * title
Definition: mip_structures.hpp:208
mip::metadata::Type::S32
@ S32
mip::metadata::ParameterInfo::Condition::type
Type type
Type of condition.
Definition: mip_structures.hpp:186
mip::metadata::FunctionBits::setCanReset
constexpr FunctionBits & setCanReset(bool d)
Definition: mip_structures.hpp:117
mip::metadata::Type::DOUBLE
@ DOUBLE
mip::metadata::FunctionBits::setCanWrite
constexpr FunctionBits & setCanWrite(bool w)
Definition: mip_structures.hpp:113
mip::metadata::Type
Type
Definition: mip_structures.hpp:23
mip::metadata::Type::CHAR
@ CHAR
mip::metadata::ParameterInfo
Definition: mip_structures.hpp:161
mip::metadata::TypeInfo::isCustom
bool isCustom() const
Definition: mip_structures.hpp:61
mip::metadata::FunctionBits::setCanSave
constexpr FunctionBits & setCanSave(bool s)
Definition: mip_structures.hpp:115
mip::metadata::ParameterInfo::type
TypeInfo type
Data type.
Definition: mip_structures.hpp:197
mip::metadata::ParameterInfo::count
Count count
Number of instances for arrays.
Definition: mip_structures.hpp:200
mip::metadata::EnumInfo::Entry
Definition: mip_structures.hpp:67
mip::metadata::EnumInfo::Entry::value
uint32_t value
Definition: mip_structures.hpp:69
mip::metadata::Type::UNION
@ UNION
mip::metadata::TypeInfo::isClass
bool isClass() const
Definition: mip_structures.hpp:62
mip::metadata::TypeInfo::structPointer
const StructInfo * structPointer() const
Definition: mip_structures.hpp:58
mip::metadata::FunctionBits::setCanRead
constexpr FunctionBits & setCanRead(bool r)
Definition: mip_structures.hpp:114
mip::metadata::Type::S8
@ S8
mip::FunctionSelector
FunctionSelector
Definition: mip_descriptors.hpp:66
mip::metadata::ParamAttributes::ParamAttributes
constexpr ParamAttributes(bool w, bool r, bool s, bool l, bool d, bool e=false, bool x=false)
Definition: mip_structures.hpp:129
mip::metadata::FunctionBits::canLoad
constexpr bool canLoad() const
Definition: mip_structures.hpp:110
mip::metadata::StructInfo::docs
const char * docs
Definition: mip_structures.hpp:209
mip::metadata::DescriptorSetInfo::name
const char * name
Definition: mip_structures.hpp:226
mip::metadata::ParameterInfo::condition
Condition condition
For conditionally-enabled parameters like those in unions.
Definition: mip_structures.hpp:201
mip::metadata::Type::FLOAT
@ FLOAT
mip::metadata::ParamAttributes::ParamAttributes
constexpr ParamAttributes()=default
mip::metadata::FunctionBits::has
constexpr bool has(mip::FunctionSelector function) const
Definition: mip_structures.hpp:119
mip::metadata::DescriptorSetInfo::descriptor
uint8_t descriptor
Definition: mip_structures.hpp:225
mip::metadata::Type::S16
@ S16
mip::metadata::EnumInfo::entries
Span< const Entry > entries
Definition: mip_structures.hpp:78
mip::metadata::Type::S64
@ S64
mip::metadata::TypeInfo::isBasicType
bool isBasicType() const
Definition: mip_structures.hpp:60
mip::metadata::FunctionBits::canWrite
constexpr bool canWrite() const
Definition: mip_structures.hpp:107
mip::metadata::FunctionBits
Definition: mip_structures.hpp:95
mip::metadata::EnumInfo::nameForValue
const char * nameForValue(uint32_t value) const
Definition: mip_structures.hpp:80
mip::metadata::Type::U64
@ U64
mip::metadata::ParameterInfo::Count
Definition: mip_structures.hpp:163
mip::metadata::ParamAttributes
Definition: mip_structures.hpp:125
mip::metadata::FieldAttributes::FieldAttributes
constexpr FieldAttributes(bool w, bool r, bool s, bool l, bool d, bool p=false)
Definition: mip_structures.hpp:148
mip::FunctionSelector::WRITE
@ WRITE
mip::metadata::FieldInfo::functions
FieldAttributes functions
Definition: mip_structures.hpp:219
mip::metadata::Type::BOOL
@ BOOL
mip::metadata::FieldInfo
Definition: mip_structures.hpp:216
mip::metadata::DescriptorSetInfo
Definition: mip_structures.hpp:223
mip::metadata::Type::U8
@ U8
mip::metadata::ParameterInfo::Condition::Type::ENUM
@ ENUM
Enum value selector (e.g. for parameters in unions)
mip::metadata::EnumInfo::Entry::docs
const char * docs
Definition: mip_structures.hpp:71
mip::metadata::FunctionBits::FunctionBits
constexpr FunctionBits()=default
mip::metadata::StructInfo::parameters
Span< const ParameterInfo > parameters
Definition: mip_structures.hpp:211
mip::metadata::TypeInfo::infoPtr
const void * infoPtr
Definition: mip_structures.hpp:53
mip::metadata::ParameterInfo::Condition::value
uint16_t value
Value of the enum parameter which activates this parameter.
Definition: mip_structures.hpp:188
mip::metadata::TypeInfo::bitfieldPointer
const BitfieldInfo * bitfieldPointer() const
Definition: mip_structures.hpp:56
mip::metadata::FieldAttributes::hasFunctionSelector
constexpr bool hasFunctionSelector() const
Definition: mip_structures.hpp:150
mip::metadata::ParamAttributes::isNotSerialized
constexpr bool isNotSerialized() const
Definition: mip_structures.hpp:135
mip::metadata::EnumInfo
Definition: mip_structures.hpp:65
mip::metadata::TypeInfo::unionPointer
const UnionInfo * unionPointer() const
Definition: mip_structures.hpp:57
mip::metadata::TypeInfo::type
Type type
Definition: mip_structures.hpp:51
mip::metadata::ParamAttributes::isSerialized
constexpr bool isSerialized() const
Definition: mip_structures.hpp:137
mip::metadata::TypeInfo
Definition: mip_structures.hpp:46
microstrain::Id
Representes an ID number ranging from 1..N including N.
Definition: index.hpp:75
mip::metadata::DescriptorSetInfo::fields
Span< const FieldInfo *const > fields
Definition: mip_structures.hpp:227
mip::CompositeDescriptor
Convenience struct holding both descriptor set and field descriptor.
Definition: mip_descriptors.hpp:21
mip::metadata::ParamAttributes::setIsEchoed
constexpr ParamAttributes & setIsEchoed(bool e)
Definition: mip_structures.hpp:140
mip::metadata::FieldInfo::descriptor
CompositeDescriptor descriptor
Definition: mip_structures.hpp:218
mip::metadata::ParameterInfo::Count::hasCounter
constexpr bool hasCounter() const
Definition: mip_structures.hpp:173
mip::metadata::FunctionBits::canSave
constexpr bool canSave() const
Definition: mip_structures.hpp:109
mip::metadata::Type::BITS
@ BITS
mip::metadata::StructInfo
Definition: mip_structures.hpp:205
mip::metadata::ParameterInfo::Condition::paramIdx
microstrain::Id paramIdx
Index of enum parameter identifying whether this parameter is enabled.
Definition: mip_structures.hpp:187
span.hpp
mip::metadata::TypeInfo::enumPointer
const EnumInfo * enumPointer() const
Definition: mip_structures.hpp:55
mip::metadata::ParamAttributes::ParamAttributes
constexpr ParamAttributes(FunctionBits bits)
Definition: mip_structures.hpp:128
mip::metadata::FunctionBits::setCanLoad
constexpr FunctionBits & setCanLoad(bool l)
Definition: mip_structures.hpp:116
mip::metadata::StructInfo::name
const char * name
Definition: mip_structures.hpp:207
mip::metadata::ParameterInfo::Condition::Type
Type
Definition: mip_structures.hpp:178
mip::metadata::Type::U32
@ U32
mip::metadata::FieldAttributes::isProprietary
constexpr bool isProprietary() const
Definition: mip_structures.hpp:152
mip::metadata::ParameterInfo::docs
const char * docs
Human-readable documentation.
Definition: mip_structures.hpp:196
mip::metadata::ParamAttributes::setNotSerialized
constexpr ParamAttributes & setNotSerialized(bool x)
Definition: mip_structures.hpp:139
mip::metadata::FunctionBits::FunctionBits
constexpr FunctionBits(bool w, bool r, bool s, bool l, bool d)
Definition: mip_structures.hpp:98
mip::metadata::EnumInfo::Entry::name
const char * name
Definition: mip_structures.hpp:70
mip::metadata::UnionInfo
Definition: mip_structures.hpp:214
mip::metadata::FieldAttributes::FieldAttributes
constexpr FieldAttributes()=default