Prusa-MMU-Private
PrusaMultiMaterialUpgradev3firmwareforMK3SMK4
unit.h
Go to the documentation of this file.
1 #pragma once
3 #include <stdint.h>
4 
25 namespace unit {
26 
28 enum UnitBase : uint8_t {
29  Millimeter,
30  Degree,
31 };
32 
34 enum UnitType : uint8_t {
35  Lenght,
36  Speed,
37  Accel,
38 };
39 
41 template <typename T, UnitBase B, UnitType U>
42 struct Unit {
43  T v;
44 
45  static constexpr UnitBase base = B;
46  static constexpr UnitType unit = U;
47 
48  typedef T type_t;
49  typedef Unit<T, B, U> self_t;
50 
51  // same-type operations
52  constexpr self_t operator+(const self_t r) const { return { v + r.v }; }
53  constexpr self_t operator-(const self_t r) const { return { v - r.v }; }
54  constexpr self_t operator-() const { return { -v }; }
55  constexpr self_t operator*(const self_t r) const { return { v * r.v }; }
56  constexpr self_t operator/(const self_t r) const { return { v / r.v }; }
57 
58  // allow an unitless multiplier to scale the quantity: U * f => U
59  constexpr self_t operator*(const long double f) const { return { (T)(v * f) }; }
60  constexpr self_t operator/(const long double f) const { return { (T)(v / f) }; }
61 };
62 
63 // complementary f * U => U * f
64 template <typename T, UnitBase B, UnitType U>
65 constexpr Unit<T, B, U> operator*(const long double f, const Unit<T, B, U> u) { return u * f; }
66 
67 // Millimiters
68 typedef Unit<long double, Millimeter, Lenght> U_mm;
69 typedef Unit<long double, Millimeter, Speed> U_mm_s;
70 typedef Unit<long double, Millimeter, Accel> U_mm_s2;
71 
72 static constexpr U_mm operator"" _mm(long double mm) {
73  return { mm };
74 }
75 
76 static constexpr U_mm_s operator"" _mm_s(long double mm_s) {
77  return { mm_s };
78 }
79 
80 static constexpr U_mm_s2 operator"" _mm_s2(long double mm_s2) {
81  return { mm_s2 };
82 }
83 
84 // Degrees
85 typedef Unit<long double, Degree, Lenght> U_deg;
86 typedef Unit<long double, Degree, Speed> U_deg_s;
87 typedef Unit<long double, Degree, Accel> U_deg_s2;
88 
89 static constexpr U_deg operator"" _deg(long double deg) {
90  return { deg };
91 }
92 
93 static constexpr U_deg_s operator"" _deg_s(long double deg_s) {
94  return { deg_s };
95 }
96 
97 static constexpr U_deg_s2 operator"" _deg_s2(long double deg_s2) {
98  return { deg_s2 };
99 }
100 
101 } // namespace unit
102 
103 // Inject literal operators into the global namespace for convenience
104 using unit::operator"" _mm;
105 using unit::operator"" _mm_s;
106 using unit::operator"" _mm_s2;
107 using unit::operator"" _deg;
108 using unit::operator"" _deg_s;
109 using unit::operator"" _deg_s2;
Definition: command_base.h:9
Definition: unit.h:25
UnitType
Unit types for conformability testing.
Definition: unit.h:34
UnitBase
Base units for conformability testing.
Definition: unit.h:28
Generic unit type for compile-time conformability testing.
Definition: unit.h:42