mc_rtc  2.16.0
fmt_formatters.h
Go to the documentation of this file.
1 /*
2  * Copyright 2026 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 #include <fmt/format.h>
7 #include <fmt/ostream.h>
8 #include <fmt/ranges.h>
9 
10 #include <SpaceVecAlg/SpaceVecAlg>
11 #include <filesystem>
12 
27 #if defined(FMT_VERSION) && FMT_VERSION >= 100000
28 # define FMT_CONST_IF_REQUIRED const
29 #else
30 # define FMT_CONST_IF_REQUIRED
31 #endif
32 
36 template<>
37 struct fmt::formatter<std::filesystem::path> : fmt::formatter<std::string>
38 {
39  template<typename FormatContext>
40  auto format(const std::filesystem::path & p, FormatContext & ctx) FMT_CONST_IF_REQUIRED
41  { return fmt::formatter<std::string>::format(p.string(), ctx); }
42 };
43 
44 #if FMT_VERSION >= 9 * 10000
49 # define MC_FMT_STREAMED(X) fmt::streamed(X)
50 
51 // Formatter for any Eigen type derived from EigenBase (matrices, arrays,
52 // expressions, etc.) Tested for fmt_9, fmt_10, fmt_11 and fmt_12
53 template<typename T>
54 struct fmt::formatter<T, char, std::enable_if_t<std::is_base_of_v<Eigen::EigenBase<T>, T>>> : fmt::ostream_formatter
55 {
56 };
57 
58 // Prevent fmt/ranges from also treating Eigen types as ranges. Otherwise fmt
59 // finds two competing formatter specializations (ranges + Eigen ostream) and
60 // fails with an ambiguous formatter instantiation.
61 template<typename T, typename Char>
62 struct fmt::range_format_kind<T, Char, std::enable_if_t<std::is_base_of_v<Eigen::EigenBase<T>, T>>>
63 : std::integral_constant<fmt::range_format, fmt::range_format::disabled>
64 {
65 };
66 
67 # define MC_FMT_OSTREAM_FORMATTER(TYPE) \
68  template<> \
69  struct fmt::formatter<TYPE> : fmt::ostream_formatter \
70  { \
71  };
72 
73 // Automatic ostream formatter for SpaceVecAlg types
74 MC_FMT_OSTREAM_FORMATTER(sva::ABInertiad)
75 MC_FMT_OSTREAM_FORMATTER(sva::AdmittanceVecd)
76 MC_FMT_OSTREAM_FORMATTER(sva::ForceVecd)
77 MC_FMT_OSTREAM_FORMATTER(sva::MotionVecd)
78 MC_FMT_OSTREAM_FORMATTER(sva::RBInertiad)
79 MC_FMT_OSTREAM_FORMATTER(sva::PTransformd)
80 MC_FMT_OSTREAM_FORMATTER(sva::ImpedanceVecd)
81 #else // FMT_VERSION < 9 * 10000
82 /* std::ostream support was automatic in fmt versions < 9,
83  thus fmt::streamed did not exist and was not needed (which IMHO was a much
84  saner default) */
85 # define MC_FMT_STREAMED(X) X
86 #endif