MIP_SDK  v3.0.0
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  //union
55  //{
56  // const StructInfo *si;
57  // const EnumInfo *ei;
58  // const BitfieldInfo *bi;
59  // const UnionInfo *ui;
60  //};
61 
62  bool isBasicType() const { return type <= Type::DOUBLE; }
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 
81 struct BitfieldInfo : public EnumInfo {};
82 
83 
84 //struct ParameterInfo; // Defined below
85 
86 struct Attributes
87 {
88  constexpr Attributes() = default;
89  constexpr Attributes(bool w, bool r, bool s, bool l, bool d, bool e=false, bool x=false) : bits(0x00)
90  {
91  setCanWrite(w);
92  setCanRead(r);
93  setCanSave(s);
94  setCanLoad(l);
95  setCanReset(d);
97  setIsEchoed(e);
98  }
99 
100  constexpr bool any() const { return bits > 0; }
101 
102  constexpr bool canWrite() const { return bits & 0b00000001; }
103  constexpr bool canRead() const { return bits & 0b00000010; }
104  constexpr bool canSave() const { return bits & 0b00000100; }
105  constexpr bool canLoad() const { return bits & 0b00001000; }
106  constexpr bool canReset() const { return bits & 0b00010000; }
107  constexpr bool isNotSerialized() const { return bits & 0b01000000; }
108  constexpr bool isEchoed() const { return bits & 0b10000000; }
109  constexpr bool isSerialized() const { return !isNotSerialized(); }
110 
111  constexpr Attributes& setCanWrite (bool w) { bits = (bits & 0b11111110) | (uint8_t(w)<<0); return *this; }
112  constexpr Attributes& setCanRead (bool r) { bits = (bits & 0b11111101) | (uint8_t(r)<<1); return *this; }
113  constexpr Attributes& setCanSave (bool s) { bits = (bits & 0b11111011) | (uint8_t(s)<<2); return *this; }
114  constexpr Attributes& setCanLoad (bool l) { bits = (bits & 0b11110111) | (uint8_t(l)<<3); return *this; }
115  constexpr Attributes& setCanReset (bool d) { bits = (bits & 0b11101111) | (uint8_t(d)<<4); return *this; }
116  constexpr Attributes& setNotSerialized (bool x) { bits = (bits & 0b10111111) | (uint8_t(x)<<6); return *this; }
117  constexpr Attributes& setIsEchoed (bool e) { bits = (bits & 0b01111111) | (uint8_t(e)<<7); 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 static constexpr inline Attributes ALL_FUNCTIONS = {true, true, true, true, true};
124 static constexpr inline Attributes NO_FUNCTIONS = {false, false, false, false, false};
125 
126 
128 {
129  struct Count
130  {
131  constexpr Count() = default;
132  constexpr Count(uint8_t n) : count(n) {}
133  constexpr Count(uint8_t n, microstrain::Id id) : count(n), paramIdx(id) {}
134 
135  uint8_t count = 1;
137 
138  constexpr bool isFixed() const { return count > 0 && !paramIdx.isAssigned(); }
139  constexpr bool hasCounter() const { return paramIdx.isAssigned(); }
140  };
141 
142  struct Condition
143  {
144  enum class Type : uint8_t
145  {
146  NONE = 0,
147  ENUM = 1,
148  //PRODUCT = 2, ///< Depends on product variant (TBD)
149  //OPTIONAL = 2, ///< Parameter can be omitted (TBD)
150  };
151 
154  uint16_t value = 0;
155 
156  constexpr bool hasCondition() const { return type != Type::NONE; }
157  };
158 
159  using Accessor = void* (*)(void*);
160 
161  const char* name = nullptr;
162  const char* docs = nullptr;
164  Accessor accessor = nullptr;
168 };
169 
170 
172 {
173  const char* name = nullptr;
174  const char* title = nullptr;
175  const char* docs = nullptr;
176 
178 };
179 
180 struct UnionInfo : public StructInfo {};
181 
182 struct FieldInfo : public StructInfo
183 {
185  Attributes functions = {false, false, false, false, false};
186  bool proprietary = false;
187  const FieldInfo* response = nullptr;
188 };
189 
190 
193 constexpr size_t sizeForBasicType(Type type, const void* info=nullptr)
194 {
195  switch(type)
196  {
197  case Type::CHAR:
198  case Type::BOOL:
199  case Type::U8:
200  case Type::S8:
201  return 1;
202  case Type::U16:
203  case Type::S16:
204  return 2;
205  case Type::U32:
206  case Type::S32:
207  case Type::FLOAT:
208  return 4;
209  case Type::U64:
210  case Type::S64:
211  case Type::DOUBLE:
212  return 8;
213 
214  case Type::ENUM:
215  if(!info)
216  return 0;
217  return sizeForBasicType(static_cast<const EnumInfo *>(info)->type);
218 
219  case Type::BITS:
220  if(!info)
221  return 0;
222  return sizeForBasicType(static_cast<const BitfieldInfo *>(info)->type);
223 
224  default:
225  return 0;
226  }
227 }
228 constexpr size_t sizeForBasicType(const TypeInfo& type) { return sizeForBasicType(type.type, type.infoPtr); }
229 
230 
231 } // 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:138
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:136
mip::metadata::Attributes::setIsEchoed
constexpr Attributes & setIsEchoed(bool e)
Definition: mip_structures.hpp:117
mip_descriptors.hpp
mip::metadata::Attributes::isEchoed
constexpr bool isEchoed() const
Definition: mip_structures.hpp:108
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:164
mip::metadata::ParameterInfo::Condition
Definition: mip_structures.hpp:142
mip::metadata::FieldInfo::response
const FieldInfo * response
Definition: mip_structures.hpp:187
mip::metadata::ParameterInfo::Condition::Type::NONE
@ NONE
No condition, member always valid.
mip::metadata::Type::NONE
@ NONE
Invalid/unknown.
mip::metadata::EnumInfo::docs
const char * docs
Definition: mip_structures.hpp:75
mip::metadata::BitfieldInfo
Definition: mip_structures.hpp:81
mip::metadata::ParameterInfo::Condition::hasCondition
constexpr bool hasCondition() const
Definition: mip_structures.hpp:156
mip::metadata::Attributes::isNotSerialized
constexpr bool isNotSerialized() const
Definition: mip_structures.hpp:107
mip::metadata
Definition: commands_3dm.hpp:9
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:133
mip::metadata::Attributes::Attributes
constexpr Attributes()=default
mip::metadata::sizeForBasicType
constexpr size_t sizeForBasicType(Type type, const void *info=nullptr)
Gets the size of a basic type (including bitfields and enums if class_ is not NULL).
Definition: mip_structures.hpp:193
mip::metadata::Attributes::isSerialized
constexpr bool isSerialized() const
Definition: mip_structures.hpp:109
mip::metadata::ParameterInfo::Count::count
uint8_t count
Fixed size if paramIdx unassigned.
Definition: mip_structures.hpp:135
mip::metadata::ParameterInfo::Count::Count
constexpr Count(uint8_t n)
Definition: mip_structures.hpp:132
mip::metadata::ParameterInfo::name
const char * name
Programmatic name (e.g. for printing or language bindings).
Definition: mip_structures.hpp:161
mip::metadata::Attributes::any
constexpr bool any() const
Definition: mip_structures.hpp:100
mip::metadata::Attributes::setCanWrite
constexpr Attributes & setCanWrite(bool w)
Definition: mip_structures.hpp:111
mip::metadata::ParameterInfo::Accessor
void *(*)(void *) Accessor
Definition: mip_structures.hpp:159
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::Attributes::setCanReset
constexpr Attributes & setCanReset(bool d)
Definition: mip_structures.hpp:115
mip::metadata::StructInfo::title
const char * title
Definition: mip_structures.hpp:174
mip::metadata::Type::S32
@ S32
mip::metadata::ParameterInfo::Condition::type
Type type
Type of condition.
Definition: mip_structures.hpp:152
mip::metadata::Type::DOUBLE
@ DOUBLE
mip::metadata::Type
Type
Definition: mip_structures.hpp:23
mip::metadata::Type::CHAR
@ CHAR
mip::metadata::ParameterInfo
Definition: mip_structures.hpp:127
mip::metadata::ParameterInfo::type
TypeInfo type
Data type.
Definition: mip_structures.hpp:163
mip::metadata::ParameterInfo::count
Count count
Number of instances for arrays.
Definition: mip_structures.hpp:166
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::Attributes::bits
uint8_t bits
Definition: mip_structures.hpp:121
mip::metadata::Type::S8
@ S8
mip::FunctionSelector
FunctionSelector
Definition: mip_descriptors.hpp:68
mip::metadata::StructInfo::docs
const char * docs
Definition: mip_structures.hpp:175
mip::metadata::ParameterInfo::condition
Condition condition
For conditionally-enabled parameters like those in unions.
Definition: mip_structures.hpp:167
mip::metadata::Type::FLOAT
@ FLOAT
mip::metadata::Type::S16
@ S16
mip::metadata::EnumInfo::entries
Span< const Entry > entries
Definition: mip_structures.hpp:78
mip::metadata::Attributes::setNotSerialized
constexpr Attributes & setNotSerialized(bool x)
Definition: mip_structures.hpp:116
mip::metadata::Attributes::canRead
constexpr bool canRead() const
Definition: mip_structures.hpp:103
mip::metadata::Type::S64
@ S64
mip::metadata::TypeInfo::isBasicType
bool isBasicType() const
Definition: mip_structures.hpp:62
mip::metadata::Attributes::canLoad
constexpr bool canLoad() const
Definition: mip_structures.hpp:105
mip::metadata::FieldInfo::functions
Attributes functions
Definition: mip_structures.hpp:185
mip::metadata::Type::U64
@ U64
mip::metadata::ParameterInfo::Count
Definition: mip_structures.hpp:129
mip::FunctionSelector::WRITE
@ WRITE
mip::metadata::Type::BOOL
@ BOOL
mip::metadata::FieldInfo
Definition: mip_structures.hpp:182
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::StructInfo::parameters
Span< const ParameterInfo > parameters
Definition: mip_structures.hpp:177
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:154
mip::metadata::Attributes::has
constexpr bool has(mip::FunctionSelector function) const
Definition: mip_structures.hpp:119
mip::metadata::Attributes
Definition: mip_structures.hpp:86
mip::metadata::Attributes::setCanRead
constexpr Attributes & setCanRead(bool r)
Definition: mip_structures.hpp:112
mip::metadata::EnumInfo
Definition: mip_structures.hpp:65
mip::metadata::ParameterInfo::attributes
Attributes attributes
This parameter is required for the specified function selectors.
Definition: mip_structures.hpp:165
mip::metadata::Attributes::canReset
constexpr bool canReset() const
Definition: mip_structures.hpp:106
mip::metadata::Attributes::Attributes
constexpr Attributes(bool w, bool r, bool s, bool l, bool d, bool e=false, bool x=false)
Definition: mip_structures.hpp:89
mip::metadata::TypeInfo::type
Type type
Definition: mip_structures.hpp:51
mip::metadata::TypeInfo
Definition: mip_structures.hpp:46
mip::metadata::Attributes::canSave
constexpr bool canSave() const
Definition: mip_structures.hpp:104
microstrain::Id
Representes an ID number ranging from 1..N including N.
Definition: index.hpp:75
mip::CompositeDescriptor
Convenience struct holding both descriptor set and field descriptor.
Definition: mip_descriptors.hpp:22
mip::metadata::FieldInfo::descriptor
CompositeDescriptor descriptor
Definition: mip_structures.hpp:184
mip::metadata::ParameterInfo::Count::hasCounter
constexpr bool hasCounter() const
Definition: mip_structures.hpp:139
mip::metadata::Type::BITS
@ BITS
mip::metadata::StructInfo
Definition: mip_structures.hpp:171
mip::metadata::ParameterInfo::Condition::paramIdx
microstrain::Id paramIdx
Index of enum parameter identifying whether this parameter is enabled.
Definition: mip_structures.hpp:153
mip::metadata::Attributes::canWrite
constexpr bool canWrite() const
Definition: mip_structures.hpp:102
mip::metadata::Attributes::setCanSave
constexpr Attributes & setCanSave(bool s)
Definition: mip_structures.hpp:113
span.hpp
mip::metadata::StructInfo::name
const char * name
Definition: mip_structures.hpp:173
mip::metadata::Attributes::setCanLoad
constexpr Attributes & setCanLoad(bool l)
Definition: mip_structures.hpp:114
mip::metadata::ParameterInfo::Condition::Type
Type
Definition: mip_structures.hpp:144
mip::metadata::Type::U32
@ U32
mip::metadata::ParameterInfo::docs
const char * docs
Human-readable documentation.
Definition: mip_structures.hpp:162
mip::metadata::FieldInfo::proprietary
bool proprietary
Definition: mip_structures.hpp:186
mip::metadata::EnumInfo::Entry::name
const char * name
Definition: mip_structures.hpp:70
mip::metadata::UnionInfo
Definition: mip_structures.hpp:180