MIP_SDK  latest-2-g34f3e39
MicroStrain Communications Library for embedded systems
common.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 
17 
18 
19 namespace mip
20 {
24 
26 {
27  uint8_t descriptor;
28  uint16_t decimation;
29 
30  size_t insert(Serializer& buffer) const { return buffer.insert(descriptor, decimation); }
31  size_t extract(Serializer& buffer) { return buffer.extract(descriptor, decimation); }
32 };
33 
34 
41 template<typename T, size_t N>
42 struct Vector
43 {
45  Vector() {}
46 
49  template<typename U>
50  Vector(U value) { fill(value); }
51 
54  template<typename U>
55  Vector(const U (&ptr)[N]) { copyFrom(ptr, N); }
56 
59  template<typename U, size_t M>
60  explicit Vector(const U (&ptr)[M]) { static_assert(M>=N, "Input array is too small"); copyFrom(ptr, M); }
61 
65  template<typename U>
66  explicit Vector(const U* ptr, size_t n) { copyFrom(ptr, n); }
67 
72  template<typename U, typename V, typename... Rest>
73  Vector(U u, V v, Rest... rest) : m_data{u, v, rest...} {}
74 
76  Vector(const Vector&) = default;
77 
79  Vector& operator=(const Vector&) = default;
80 
82  template<typename U>
83  Vector& operator=(const Vector<U,N>& other) { copyFrom(other, N); return *this; }
84 
85 
86  typedef T Array[N];
87 
88 #if _MSC_VER < 1930
89  // MSVC 2017 has a bug which causes operator[] to be ambiguous.
90  // See https://stackoverflow.com/questions/48250560/msvc-error-c2593-when-overloading-const-and-non-const-conversion-operator-return
91  operator T*() { return m_data; }
92  operator const T*() const { return m_data; }
93 #else
94  operator Array&() { return m_data; }
96  operator const Array&() const { return m_data; }
97 #endif
98 
100  Array& data() { return m_data; }
101  const Array& data() const { return m_data; }
102 
104  template<typename U>
105  void fill(U value) { for(size_t i=0; i<N; i++) m_data[i]=value; }
106 
110  template<typename U>
111  void copyFrom(const U* ptr, size_t n) { if(n>N) n=N; for(size_t i=0; i<n; i++) m_data[i] = (T)ptr[i]; }
112 
114  size_t size() const { return N; }
115 
116 private:
117  T m_data[N];
118 };
119 
126 
127 using Quatf = Vector4f;
128 
129 template<typename T, size_t N>
130 size_t insert(Serializer& serializer, const Vector<T,N>& v) { return serializer.insert(v.data()); }
131 
132 template<typename T, size_t N>
133 size_t extract(Serializer& serializer, Vector<T,N>& v) { return serializer.extract(v.data()); }
134 
137 } // namespace mip
mip
A collection of C++ classes and functions covering the full mip api.
Definition: commands_3dm.c:11
microstrain::Serializer
Serializes or deserializes data to/from a byte buffer.
Definition: serializer.hpp:125
mip::Vector::Vector
Vector(const U *ptr, size_t n)
Definition: common.hpp:66
mip::Vector::Array
T Array[N]
Definition: common.hpp:86
mip::Vector
Vector is a wrapper around an array of some type T, usually float or double.
Definition: common.hpp:42
microstrain::Serializer::extract
bool extract(Ts &... values)
Deserializes one or more values.
Definition: serializer.hpp:949
mip::Vector::copyFrom
void copyFrom(const U *ptr, size_t n)
Definition: common.hpp:111
mip::DescriptorRate::decimation
uint16_t decimation
Definition: common.hpp:28
mip::DescriptorRate::descriptor
uint8_t descriptor
Definition: common.hpp:27
mip::insert
size_t insert(Serializer &serializer, const Vector< T, N > &v)
Definition: common.hpp:130
mip::Vector::operator=
Vector & operator=(const Vector &)=default
Assignment operator.
mip::Vector::size
size_t size() const
Get the size of the array.
Definition: common.hpp:114
mip::Vector::Vector
Vector(const U(&ptr)[N])
Definition: common.hpp:55
mip::DescriptorRate
Definition: common.hpp:25
mip::Vector4f
Vector< float, 4 > Vector4f
Definition: common.hpp:121
mip::Vector::data
const Array & data() const
Definition: common.hpp:101
mip::Vector::Vector
Vector(const U(&ptr)[M])
Definition: common.hpp:60
mip::DescriptorRate::insert
size_t insert(Serializer &buffer) const
Definition: common.hpp:30
mip::Vector::Vector
Vector()
Default constructor, no initialization.
Definition: common.hpp:45
mip::Vector::fill
void fill(U value)
Fill all elements with a given value.
Definition: common.hpp:105
mip::Vector::Vector
Vector(U u, V v, Rest... rest)
Definition: common.hpp:73
mip_serialization.hpp
mip::DescriptorRate::extract
size_t extract(Serializer &buffer)
Definition: common.hpp:31
mip::Vector::Vector
Vector(U value)
Definition: common.hpp:50
mip::Vector::operator=
Vector & operator=(const Vector< U, N > &other)
Assignment operator from different type (e.g. float to double).
Definition: common.hpp:83
microstrain::Serializer::insert
bool insert(const Ts &... values)
Serializes one or more values.
Definition: serializer.hpp:923
mip::extract
size_t extract(Serializer &serializer, Vector< T, N > &v)
Definition: common.hpp:133
mip::Vector::data
Array & data()
Explicitly convert to a C-style array.
Definition: common.hpp:100