utils.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 namespace mc_rtc
6 {
7 
8 namespace log
9 {
10 
12 enum class LogType
13 {
14  None = 0,
15  Bool,
16  Int8_t,
17  Int16_t,
18  Int32_t,
19  Int64_t,
20  Uint8_t,
21  Uint16_t,
22  Uint32_t,
23  Uint64_t,
24  Float,
25  Double,
26  String,
27  Vector2d,
28  Vector3d,
29  Vector6d,
30  VectorXd,
33  ForceVecd,
34  MotionVecd,
36 };
37 
38 inline const char ** LogTypeNames()
39 {
40  static const char * names[] = {"None", "Bool", "Int8_t", "Int16_t", "Int32_t", "Int64_t",
41  "Uint8_t", "Uint16_t", "Uint32_t", "Uint64_t", "Float", "Double",
42  "String", "Vector2d", "Vector3d", "Vector6d", "VectorXd", "Quaterniond",
43  "PTransformd", "ForceVecd", "MotionVecd", "VectorDouble", nullptr};
44  return names;
45 }
46 
47 inline const char * LogTypeName(LogType t)
48 {
49  const size_t idx = static_cast<size_t>(t);
50  return LogTypeNames()[idx];
51 }
52 
54 template<typename T>
55 struct GetLogType
56 {
58 };
59 
60 #define IMPL_MAPPING(CPPT, ENUMV) \
61  template<> \
62  struct GetLogType<CPPT> \
63  { \
64  static constexpr mc_rtc::log::LogType type = mc_rtc::log::LogType::ENUMV; \
65  }
66 
67 IMPL_MAPPING(bool, Bool);
68 IMPL_MAPPING(int8_t, Int8_t);
69 IMPL_MAPPING(int16_t, Int16_t);
70 IMPL_MAPPING(int32_t, Int32_t);
71 IMPL_MAPPING(int64_t, Int64_t);
72 IMPL_MAPPING(uint8_t, Uint8_t);
73 IMPL_MAPPING(uint16_t, Uint16_t);
74 IMPL_MAPPING(uint32_t, Uint32_t);
75 IMPL_MAPPING(uint64_t, Uint64_t);
76 IMPL_MAPPING(float, Float);
77 IMPL_MAPPING(double, Double);
78 IMPL_MAPPING(std::string, String);
79 IMPL_MAPPING(Eigen::Vector2d, Vector2d);
80 IMPL_MAPPING(Eigen::Vector3d, Vector3d);
81 IMPL_MAPPING(Eigen::Vector6d, Vector6d);
82 IMPL_MAPPING(Eigen::VectorXd, VectorXd);
83 IMPL_MAPPING(Eigen::Quaterniond, Quaterniond);
84 IMPL_MAPPING(sva::PTransformd, PTransformd);
85 IMPL_MAPPING(sva::ForceVecd, ForceVecd);
86 IMPL_MAPPING(sva::MotionVecd, MotionVecd);
87 IMPL_MAPPING(sva::ImpedanceVecd, MotionVecd);
91 
92 #undef IMPL_MAPPING
93 
94 template<int N, int _Options, int _MaxRows, int _MaxCols>
95 struct GetLogType<Eigen::Matrix<double, N, 1, _Options, _MaxRows, _MaxCols>>
96 {
98 };
99 
100 template<typename A>
101 struct GetLogType<std::vector<double, A>>
102 {
104 };
105 
106 template<std::size_t N>
107 struct GetLogType<std::array<double, N>>
108 {
110 };
111 
112 template<typename Type, int Options, typename StrideType>
113 struct GetLogType<Eigen::Ref<Type, Options, StrideType>>
114 {
115  // clang-format off
116  static constexpr mc_rtc::log::LogType type =
117  Type::ColsAtCompileTime != 1 ? mc_rtc::log::LogType::None :
118  Type::RowsAtCompileTime == 2 ? mc_rtc::log::LogType::Vector2d :
119  Type::RowsAtCompileTime == 3 ? mc_rtc::log::LogType::Vector3d :
120  Type::RowsAtCompileTime == 6 ? mc_rtc::log::LogType::Vector6d :
122  // clang-format on
123 };
124 
126 template<typename T>
128 {
129  static constexpr LogType type = GetLogType<T>::type;
131 };
132 
133 template<typename...>
134 using void_t = void;
135 
137 template<typename T>
139 {
140  static constexpr bool value = false;
141 };
142 
143 template<typename T, typename MemberT>
145 {
146  static constexpr bool value = is_serializable<typename std::decay<MemberT>::type>::value;
147 };
148 
149 template<typename T>
151 {
152  static constexpr bool value = false;
153 };
154 
155 template<typename T, typename MethodRetT>
156 struct is_serializable_getter<MethodRetT (T::*)() const>
157 {
159 };
160 
166 template<typename T, typename = void>
168 {
169  static constexpr bool value = false;
170 };
171 
172 template<typename T>
173 struct callback_is_serializable<T, void_t<typename std::result_of<T()>::type>>
174 {
175  using ret_type = typename std::result_of<T()>::type;
176  using base_type = typename std::decay<ret_type>::type;
177  static constexpr LogType log_type = is_serializable<base_type>::type;
178  static constexpr bool value = is_serializable<base_type>::value;
179 };
180 
182 template<typename T>
183 struct LogWriter
184 {
185  static void write(const T & data, mc_rtc::MessagePackBuilder & builder) { builder.write(data); }
186 };
187 
189 template<LogType type>
191 {
192  static_assert(static_cast<std::underlying_type_t<LogType>>(type)
193  == std::numeric_limits<std::underlying_type_t<LogType>>::max(),
194  "This must be specialized for the provided LogType value");
195 };
196 
197 #define IMPL_MAPPING(ENUMV, CPPT) \
198  template<> \
199  struct log_type_to_type<LogType::ENUMV> \
200  { \
201  using type = CPPT; \
202  }
203 
204 IMPL_MAPPING(Bool, bool);
205 IMPL_MAPPING(Int8_t, int8_t);
206 IMPL_MAPPING(Int16_t, int16_t);
207 IMPL_MAPPING(Int32_t, int32_t);
208 IMPL_MAPPING(Int64_t, int64_t);
209 IMPL_MAPPING(Uint8_t, uint8_t);
210 IMPL_MAPPING(Uint16_t, uint16_t);
211 IMPL_MAPPING(Uint32_t, uint32_t);
212 IMPL_MAPPING(Uint64_t, uint64_t);
213 IMPL_MAPPING(Float, float);
214 IMPL_MAPPING(Double, double);
215 IMPL_MAPPING(String, std::string);
216 IMPL_MAPPING(Vector2d, Eigen::Vector2d);
217 IMPL_MAPPING(Vector3d, Eigen::Vector3d);
218 IMPL_MAPPING(Vector6d, Eigen::Vector6d);
219 IMPL_MAPPING(VectorXd, Eigen::VectorXd);
220 IMPL_MAPPING(Quaterniond, Eigen::Quaterniond);
221 IMPL_MAPPING(PTransformd, sva::PTransformd);
222 IMPL_MAPPING(ForceVecd, sva::ForceVecd);
223 IMPL_MAPPING(MotionVecd, sva::MotionVecd);
224 IMPL_MAPPING(VectorDouble, std::vector<double>);
225 
226 #undef IMPL_MAPPING
227 
228 template<LogType type>
230 
231 } // namespace log
232 
233 } // namespace mc_rtc
mc_rtc::log::LogType::Uint32_t
@ Uint32_t
mc_rtc::log::LogType::Int64_t
@ Int64_t
mc_rtc::log::GetLogType::type
static constexpr mc_rtc::log::LogType type
Definition: utils.h:57
mc_rtc::MessagePackBuilder
Definition: MessagePackBuilder.h:86
mc_rtc::log::LogType::None
@ None
mc_rtc::log::LogType::Float
@ Float
mc_rtc::log::LogType::PTransformd
@ PTransformd
mc_rtc::log::LogType::Int8_t
@ Int8_t
mc_rtc::log::GetLogType
Definition: utils.h:55
mc_rtc::log::LogType::VectorXd
@ VectorXd
mc_rtc::log::log_type_to_type
Definition: utils.h:190
mc_rtc::log::log_type_to_type_t
typename log_type_to_type< type >::type log_type_to_type_t
Definition: utils.h:229
mc_rtc::log::LogWriter::write
static void write(const T &data, mc_rtc::MessagePackBuilder &builder)
Definition: utils.h:185
mc_rtc::log::LogType::Vector2d
@ Vector2d
mc_rtc::log::callback_is_serializable
Definition: utils.h:167
mc_rtc::log::LogType
LogType
Definition: utils.h:12
mc_rtc::log::callback_is_serializable< T, void_t< typename std::result_of< T()>::type > >::ret_type
typename std::result_of< T()>::type ret_type
Definition: utils.h:175
mc_rtc::log::LogTypeNames
const char ** LogTypeNames()
Definition: utils.h:38
mc_rtc::log::is_serializable::type
static constexpr LogType type
Definition: utils.h:129
mc_rtc::log::LogType::Int32_t
@ Int32_t
mc_rtc::log::LogType::String
@ String
mc_rtc::log::IMPL_MAPPING
IMPL_MAPPING(bool, Bool)
mc_rtc::log::LogType::Vector3d
@ Vector3d
mc_rtc::log::LogType::ForceVecd
@ ForceVecd
mc_rtc::log::LogType::Uint16_t
@ Uint16_t
mc_rtc::log::LogType::Bool
@ Bool
mc_rtc::log::LogWriter
Definition: utils.h:183
mc_rtc::log::LogType::Uint64_t
@ Uint64_t
mc_rtc::MessagePackBuilder::write
void write()
mc_rtc::log::LogType::Quaterniond
@ Quaterniond
mc_rtc::log::LogType::Int16_t
@ Int16_t
mc_rtc::log::callback_is_serializable::value
static constexpr bool value
Definition: utils.h:169
MessagePackBuilder.h
mc_rtc::log::is_serializable_member< MemberT T::* >
Definition: utils.h:144
mc_rtc::log::is_serializable_getter::value
static constexpr bool value
Definition: utils.h:152
mc_rtc::log::void_t
void void_t
Definition: utils.h:134
mc_rtc::log::LogType::Uint8_t
@ Uint8_t
mc_rtc::log::LogTypeName
const char * LogTypeName(LogType t)
Definition: utils.h:47
mc_rtc::log::LogType::Vector6d
@ Vector6d
std
Definition: Contact.h:66
mc_rtc::log::LogType::VectorDouble
@ VectorDouble
mc_rtc::log::is_serializable_getter
Definition: utils.h:150
mc_rbdyn::Gains< 2 >
mc_rtc::log::is_serializable::value
static constexpr bool value
Definition: utils.h:130
mc_rtc::log::LogType::Double
@ Double
mc_rtc::log::LogType::MotionVecd
@ MotionVecd
mc_rtc::log::is_serializable_member
Definition: utils.h:138
mc_rtc::log::callback_is_serializable< T, void_t< typename std::result_of< T()>::type > >::base_type
typename std::decay< ret_type >::type base_type
Definition: utils.h:176
mc_rtc
Definition: Contact.h:87
mc_rtc::log::is_serializable
Definition: utils.h:127